Exemplo n.º 1
0
int main(int argc, char *argv[]) {
  int     nelem, nimages;

  float  *tagon[MAXELEM], *tagoff[MAXELEM], *tagdiff[MAXELEM];  // Images
  double *tagctrl;            // tag control variable values
  float  *mean_on, *mean_off, // mean of tag on/off images
         *mean_diff,          // mean of pairwise difference images
	 *diff_mean;          // difference of mean on/off images
  double *image_arr;
  
  fdf_header      fdf_hdr;
  char    filename[MAXSTR];


  float noise_thr = 0;

  // Loop variables
  int slice, image, images, ndiff;
  int pixel, datapts;
  int i;
  int debug_pixel, debug;
  
  /* input arguments */
  user_input input = {
    0,          // debug
    "asltag",   // tagvar
    -1,         // noise
    -1,         // pixel 
    "ASL",      // outdir 
    ".",        // indir 
    0,          // write all pairs of subtracted images
    0,          // subtraction scheme (integer), default strict pairs
    "pair"      // subtraction scheme (string), default strict pairs
  };

  
  /*******************************************************************/  
  /*** Calculations **************************************************/  
  /*******************************************************************/  
  // Get arguments from commandline
  get_args(&input, argc, argv);
  debug = input.debug;
  if (debug) {
    if (input.pixel < 0) 
      debug_pixel = fdf_hdr.ro_size/2*fdf_hdr.pe_size + fdf_hdr.pe_size/2;
    else
      debug_pixel = input.pixel;
  }
  else debug_pixel = -1;

  /* Set up string for procpar; 
     this is for future expansion, to allow 
       - data to be in arbitrary directory
       - S(0) and S(non-zero)to be in different directories 
  */
  strcpy(procpar,input.indir);
  strcat(procpar,"/procpar");
  if (debug) printf("indir = %s, procpar = %s\n",input.indir,procpar);


  // Determine the number of images
  nelem = getstatus(input.tagvar);
  
  if (nelem == 0) {
    exit(0);  // error message is printed by getstatus
  }
    

  // Read tag control variable
  if ((tagctrl = (double *) malloc(sizeof(double)*nelem)) == NULL) nomem();
  getarray(input.tagvar,tagctrl);

  /* Is this an epi scan with reference scan(s)? */
  images = getstatus("image");
  if (images == nelem) { // assume image is arrayed in parallel with the variable
    //find out how many ref scans
    if ((image_arr = (double *) malloc(sizeof(double)*images)) == NULL) nomem();
    getarray("image",image_arr);
    nimages = 0;
    for (i = 0; i < images; i++)
      if (image_arr[i] == 1) 
        nimages++;
  }
  else nimages = nelem;

  if (debug) printf("nelem, nimages, images = %d, %d, %d\n",nelem, nimages, images);


/* XXX Check if we have an equal number of tag on/off ??? */

  
  /* Initialize fdf header */
  init_fdf_header(&fdf_hdr);


  /* Allocate memory for all images */
  datapts = fdf_hdr.ro_size*fdf_hdr.pe_size;
  switch(input.subtr) {
    case 0:  ndiff = nimages/2;   break; //strict pairwise
    case 1:  ndiff = nimages - 1; break; // adjacent pairs
    case 2:  ndiff = nimages - 2; break; // "surround" subtraction
    default: printf("Invalid subtraction scheme\n");exit(0);
  }
  
  
  for (image = 0; image < ndiff; image++) {
/* XXX CHECK if tagctrl = -1, 0 , 1 */
    if ((tagon[image]   = (float *)malloc(datapts*sizeof(float))) == NULL) nomem();
    if ((tagoff[image]  = (float *)malloc(datapts*sizeof(float))) == NULL) nomem();
    if ((tagdiff[image] = (float *)malloc(datapts*sizeof(float))) == NULL) nomem();
  }
  if ((mean_on   = (float *)malloc(datapts*sizeof(float))) == NULL) nomem();
  if ((mean_off  = (float *)malloc(datapts*sizeof(float))) == NULL) nomem();
  if ((mean_diff = (float *)malloc(datapts*sizeof(float))) == NULL) nomem();
  if ((diff_mean = (float *)malloc(datapts*sizeof(float))) == NULL) nomem();


  /* Create directory to put output in */
  if (strcmp(input.outdir, "./")) {
    if (debug) printf("Making directory %s\n",input.outdir);
    mkdir(input.outdir,0777);
  }

  
  /*******************************************************************/  
  /*** Slice by slice calculations ***********************************/  
  /*******************************************************************/  
  for (slice = 0; slice < fdf_hdr.slices; slice++) {
    i = 0;
    for (image = 0; image < nimages; image+=2) {
/* XXX check control variable */
      /* Read tag on */
      sprintf(filename,"%s/slice%03dimage%03decho001.fdf",
              input.indir,slice+1,image+1);
      if (debug) printf("tag on  [%d]: %s\n",i,filename);
      read_fdf_data(filename,tagon[i],&fdf_hdr);

      /* Read tag off */
      sprintf(filename,"%s/slice%03dimage%03decho001.fdf",
              input.indir,slice+1,image+2);
      if (debug) printf("tag off [%d]: %s\n",i,filename);
      read_fdf_data(filename,tagoff[i],&fdf_hdr);
      
      i++;
    } // end read image loop
    if (debug) printf("\n");    

    // Noise threshold - user input or based on histogram
    if (input.noise > 0) noise_thr = input.noise;
    else // Get noise from the first image
      noise_thr = threshold(tagon[0],&fdf_hdr);

    if (debug) printf("noise level %f\n",noise_thr);

    /*******************************************************************/  
    /*** Pixel loop ****************************************************/  
    /*******************************************************************/  
    for (pixel = 0; pixel < datapts; pixel++) {
      mean_diff[pixel] = 0;
     
      if (tagon[0][pixel] > noise_thr) {
        switch (input.subtr) {
	  case 0: // strictly pairwise: 0-0, 1-1, 2-2, ...
            for (image = 0; image < nimages/2; image++) {
	      tagdiff[image][pixel]  = (tagon[image][pixel] - tagoff[image][pixel]);
	      mean_diff[pixel]      += tagdiff[image][pixel];
            } 
	    break;

	  case 1:   // adjacent pairs: 0-0, 1-0, 1-1, 2-1, ...
	    i = 0;
            for (image = 0; image < nimages/2-1; image++) {
	      tagdiff[i][pixel] = (tagon[image][pixel] - tagoff[image][pixel]);
	      mean_diff[pixel] +=  tagdiff[i][pixel];
	      i++;
              if (pixel == debug_pixel) printf("subtract %d - %d\n",image,image);
	      
	      tagdiff[i][pixel] = (tagon[image+1][pixel] - tagoff[image][pixel]);
	      mean_diff[pixel] +=  tagdiff[i][pixel];
              if (pixel == debug_pixel) printf("subtract %d - %d\n",image+1,image);

	      i++;
            }
   	    tagdiff[i][pixel] = (tagon[nimages/2-1][pixel] - tagoff[nimages/2-1][pixel]);
            if (pixel == debug_pixel) printf("subtract %d - %d\n",nimages/2,nimages/2);

            mean_diff[pixel] +=  tagdiff[i][pixel];
	    break;

	  case 2: // surrounding pairs
	    i = 0;
            for (image = 0; image < nimages/2-1; image++) {
	      tagdiff[i][pixel] = ((tagon[image][pixel]+tagon[image+1][pixel])/2 - tagoff[image][pixel]);
	      mean_diff[pixel] +=  tagdiff[i][pixel];
	      i++;
              if (pixel == debug_pixel) printf("subtract %d+%d - %d\n",image,image+1,image);

	      tagdiff[i][pixel] = (tagon[image+1][pixel] - (tagoff[image][pixel]+tagoff[image+1][pixel])/2);
	      mean_diff[pixel] +=  tagdiff[i][pixel];
	      i++;
              if (pixel == debug_pixel) printf("subtract %d - %d+%d\n",image+1,image,image+1);
            }
	    break;
	  }
	  // divide by # pairs of images
	  mean_diff[pixel] /= ndiff;

      } // end check noise threshold 
    } // end pixel loop    
    

    /* Write Images maps: */
    fdf_hdr.array_dim     = 1 + input.write_all*ndiff;
    fdf_hdr.slice_no      = slice + 1;
    fdf_hdr.array_index   = 1;
    fdf_hdr.display_order = 1;

    if (input.write_all) {
      for (image = 0; image < ndiff; image++) {
	// write tag difference image
	sprintf(filename,"%s/tagdiff_%03d_%03d.fdf",input.outdir,image+1,slice+1);
	write_fdf(filename,tagdiff[image],&fdf_hdr);
	fdf_hdr.array_index++;
	fdf_hdr.display_order++;
      }
    }

    sprintf(filename,"%s/diff_%s_%03d.fdf",input.outdir,input.method,slice+1);
    write_fdf(filename,mean_diff,&fdf_hdr);
    fdf_hdr.array_index++;
    fdf_hdr.display_order++;

  } // end slice loop
  
}
Exemplo n.º 2
0
int main(int argc, char *argv[]) {
  char filename[MAXSTR], fname1[MAXSTR];
  char dirname[MAXSTR];
  fdf_header fdf_hdr;

  float *data;

  // Loop variables
  int iv, ifv;
  int sl;
  int slice, echo, element;
  int datapts, total_images, image_inx;
  int debug_pixel, debug;

  fileorg infiles;

  // input arguments
  user_input input = {
    "."           // indir
    "./AVW",      // outdir 
    0,            // debug
    -1,           // pixel 
    1,            // avw
    1,            // ME, all echoes in one volume
    0             // array, all array elements in one volume
  };

  
  /*******************************************************************/  
  /*** Initializations and mallocs ***********************************/  
  /*******************************************************************/  
  /* initialize input struct */
  input.debug = 0;
  input.avw   = 0;
  input.ME    = 1;
  input.array = 0;

  // Get arguments from commandline
  get_args(&input, argc, argv);
  debug = input.debug;
  
  if ((input.ME == 0) && (input.array == 1)) {
    printf("Sorry, does not support collecting array elements for multi-echo experiments\n");
    exit(0);
  }


  // Set up string for procpar
  strcpy(procpar,input.indir);
  strcat(procpar,"/procpar");
  if (debug) printf("indir = %s, procpar = %s\n",input.indir,procpar);

  if(get_file_lists(input.indir, &infiles))
  {
	   printf("Error organizing input files \n");
	   exit(0);
  }


  // Initialize fdf header
  init_fdf_header(&fdf_hdr);

  if (debug) {
    if (input.pixel < 0) 
      debug_pixel = fdf_hdr.datasize/2;
    else
      debug_pixel = input.pixel;
  }
  else debug_pixel = -1;
  if (debug) printf("Debugging for pixel %d (total %d)\n",debug_pixel,fdf_hdr.datasize);

  
  /* Allocate memory for all slices */
  datapts      = fdf_hdr.datasize;
  total_images = fdf_hdr.slices;
  
  if (input.ME)    total_images *= fdf_hdr.echoes;
  if (input.array) total_images *= fdf_hdr.array_dim;
  
  if ((data = (float *)malloc(datapts*total_images*sizeof(float))) == NULL) nomem();

  
  
  fdf_hdr.Smax = -999;
  fdf_hdr.Smin = 1e6;

  if (fdf_hdr.rank == 2) strcpy(fname1,"slice");
  if (fdf_hdr.rank == 3) strcpy(fname1,"img_slab");

  if (debug) printf("%d elements, %d echoes, %d slices\n",fdf_hdr.array_dim,fdf_hdr.echoes,fdf_hdr.slices);
 
  if (strcmp(input.outdir, "./")) {
    if (debug) printf("Making directory %s\n",input.outdir);
    mkdir(input.outdir,0777);
  }

  /*******************************************************************/  
  /* Loop through images *********************************************/  
  /*******************************************************************/  

	// loop on volumes
	for (iv = 0; iv < infiles.nvols; iv++) {
		for (ifv = 0; ifv < infiles.vlists[iv].nfiles; ifv++) {

			strcpy(filename, input.indir);
			strcat(filename,"/");
			strcat(filename, infiles.vlists[iv].names[ifv]);

			if (debug)
				printf("data[%d] = %s\n", ifv + 1, filename);

			image_inx = ifv;

			read_fdf_data(filename, &data[image_inx * datapts], &fdf_hdr);

		}

		// If all images are in a single file, write it here
		fdf_hdr.slices = infiles.vlists[iv].nfiles;
		strcpy(filename, input.outdir);
		strcat(filename, "/");
		strcat(filename, infiles.vlists[iv].volname);

		write_avw(filename, data, &fdf_hdr); // only have brute force now

	} /* End element loop */// end of volume loop


	// loop on groups
	for (iv = 0; iv < infiles.ngrps; iv++) {
		for (ifv = 0; ifv < infiles.glists[iv].nfiles; ifv++) {

			strcpy(filename, input.indir);
			strcat(filename,"/");
			strcat(filename, infiles.glists[iv].names[ifv]);

			if (debug)
				printf("data[%d] = %s\n", ifv + 1, filename);

			image_inx = ifv;

			read_fdf_data(filename, &data[image_inx * datapts], &fdf_hdr);

		}

		// If all images are in a single file, write it here
		fdf_hdr.slices = infiles.glists[iv].nfiles;
		strcpy(filename, input.outdir);
		strcat(filename, "/");
		strcat(filename, infiles.glists[iv].volname);


		write_avw(filename, data, &fdf_hdr); // only have brute force now

	}

	// convert miscellaneous fdfs

	for (ifv = 0; ifv < infiles.misc->nfiles; ifv++) {

			strcpy(filename, input.indir);
			strcat(filename,"/");
			strcat(filename, infiles.misc->names[ifv]);

			if (debug)
				printf("data[%d] = %s\n", ifv + 1, filename);

			image_inx = 0;

			read_fdf_data(filename, &data[image_inx * datapts], &fdf_hdr);

			// write each fdf to a nifti file
			fdf_hdr.slices = 1;


			strcpy(filename, input.outdir);
			strcat(filename, "/");
			sl=strlen(infiles.misc->names[ifv]);
			sl = sl - 4;
			strncat(filename, infiles.misc->names[ifv], sl);


			write_avw(filename, data, &fdf_hdr); // only have brute force now

		}

  
  

}