コード例 #1
0
int
main(int argc, char *argv[]) {
  char   **av ;
  int    ac, nargs ;
  MRI    *mri_src, *mri_ref, *mri_tmp ;
  double accuracy ;
  MRI_REGION box ;

  /* rkt: check for and handle version tag */
  nargs = handle_version_option (argc, argv, "$Id: mri_label_accuracy.c,v 1.2 2011/03/02 00:04:22 nicks Exp $", "$Name: stable5 $");
  if (nargs && argc - nargs == 1)
    exit (0);
  argc -= nargs;

  Progname = argv[0] ;
  ErrorInit(NULL, NULL, NULL) ;
  DiagInit(NULL, NULL, NULL) ;

  ac = argc ;
  av = argv ;
  for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) {
    nargs = get_option(argc, argv) ;
    argc -= nargs ;
    argv += nargs ;
  }

  if (argc < 3)
    usage_exit(1) ;

  mri_src = MRIread(argv[1]) ;
  if (mri_src == NULL)
    ErrorExit(ERROR_BADPARM, "%s: could not read input volume %s\n", Progname,argv[1]);
  MRIboundingBox(mri_src, 0, &box) ;
  mri_tmp = MRIextractRegionAndPad(mri_src, NULL, &box, PAD) ;
  MRIfree(&mri_src) ; mri_src = mri_tmp ;
  if (mri_src->type == MRI_SHORT)
  {
    mri_tmp = MRIchangeType(mri_src, MRI_FLOAT, 0, 0, 0) ;
    MRIfree(&mri_src) ; mri_src = mri_tmp ;
  }

  mri_ref = MRIread(argv[2]) ;
  if (mri_ref == NULL)
    ErrorExit(ERROR_BADPARM, "%s: could not read reference volume %s\n", Progname,argv[1]);
  MRIboundingBox(mri_ref, 0, &box) ;
  mri_tmp = MRIextractRegionAndPad(mri_ref, NULL, &box, PAD) ;
  MRIfree(&mri_ref) ; mri_ref = mri_tmp ;

  accuracy = MRIcomputeLabelAccuracy(mri_src, mri_ref, MRI_MEAN_MIN_DISTANCE, stdout) ;

  if (Gdiag_fp)
    fclose(Gdiag_fp) ;
  exit(0) ;
  return(0) ;
}
コード例 #2
0
static int
init_scaling(MRI *mri_in, MRI *mri_ref, MATRIX *m_L) {
  MATRIX      *m_scaling ;
  float       sx, sy, sz, dx, dy, dz ;
  MRI_REGION  in_bbox, ref_bbox ;

  m_scaling = MatrixIdentity(4, NULL) ;

  MRIboundingBox(mri_in, 60, &in_bbox) ;
  MRIboundingBox(mri_ref, 60, &ref_bbox) ;
  sx = (float)ref_bbox.dx / (float)in_bbox.dx ;
  sy = (float)ref_bbox.dy / (float)in_bbox.dy ;
  sz = (float)ref_bbox.dz / (float)in_bbox.dz ;
  dx = (ref_bbox.x+ref_bbox.dx-1)/2 - (in_bbox.x+in_bbox.dx-1)/2 ;
  dy = (ref_bbox.y+ref_bbox.dy-1)/2 - (in_bbox.y+in_bbox.dy-1)/2 ;
  dz = (ref_bbox.z+ref_bbox.dz-1)/2 - (in_bbox.z+in_bbox.dz-1)/2 ;

  if (sx > MAX_DX)
    sx = MAX_DX ;
  if (sx < MIN_DX)
    sx = MIN_DX ;
  if (sy > MAX_DY)
    sy = MAX_DY ;
  if (sy < MIN_DY)
    sy = MIN_DY ;
  if (sz > MAX_DZ)
    sz = MAX_DZ ;
  if (sz < MIN_DZ)
    sz = MIN_DZ ;
  if (Gdiag & DIAG_SHOW)
    fprintf(stderr, "initial scaling: (%2.2f, %2.2f, %2.2f) <-- "
            "(%d/%d,%d/%d,%d/%d)\n",
            sx,sy,sz, ref_bbox.dx, in_bbox.dx, ref_bbox.dy, in_bbox.dy,
            ref_bbox.dz, in_bbox.dz) ;
  *MATRIX_RELT(m_scaling, 1, 1) = sx ;
  *MATRIX_RELT(m_scaling, 2, 2) = sy ;
  *MATRIX_RELT(m_scaling, 3, 3) = sz ;

#if 0
  *MATRIX_RELT(m_L, 1, 4) = dx ;
  *MATRIX_RELT(m_L, 2, 4) = dy ;
  *MATRIX_RELT(m_L, 3, 4) = dz ;
#endif
  MatrixMultiply(m_scaling, m_L, m_L) ;
  return(NO_ERROR) ;
}
コード例 #3
0
ファイル: mri_nl_align.c プロジェクト: guo2004131/freesurfer
int
main(int argc, char *argv[])
{
  char         **av, *source_fname, *target_fname, *out_fname, fname[STRLEN] ;
  int          ac, nargs, new_transform = 0, pad ;
  MRI          *mri_target, *mri_source, *mri_orig_source ;
  MRI_REGION   box ;
  struct timeb start ;
  int          msec, minutes, seconds ;
  GCA_MORPH    *gcam ;
  MATRIX       *m_L/*, *m_I*/ ;
  LTA          *lta ;


  /* initialize the morph params */
  memset(&mp, 0, sizeof(GCA_MORPH_PARMS));
  /* for nonlinear morph */
  mp.l_jacobian = 1 ;
  mp.min_sigma = 0.4 ;
  mp.l_distance = 0 ;
  mp.l_log_likelihood = .025 ;
  mp.dt = 0.005 ;
  mp.noneg = True ;
  mp.exp_k = 20 ;
  mp.diag_write_snapshots = 1 ;
  mp.momentum = 0.9 ;
  if (FZERO(mp.l_smoothness))
    mp.l_smoothness = 2 ;
  mp.sigma = 8 ;
  mp.relabel_avgs = -1 ;
  mp.navgs = 256 ;
  mp.levels = 6 ;
  mp.integration_type = GCAM_INTEGRATE_BOTH ;
  mp.nsmall = 1 ;
  mp.reset_avgs = -1 ;
  mp.npasses = 3 ;
  mp.regrid = regrid? True : False ;
  mp.tol = 0.1 ;
  mp.niterations = 1000 ;
	
  TimerStart(&start) ;
  setRandomSeed(-1L) ;
  DiagInit(NULL, NULL, NULL) ;
  ErrorInit(NULL, NULL, NULL) ;

  Progname = argv[0] ;
  ac = argc ;
  av = argv ;
  for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++)
    {
      nargs = get_option(argc, argv) ;
      argc -= nargs ;
      argv += nargs ;
    }

  if (argc < 4)
    usage_exit(1) ;

  source_fname = argv[1] ;
  target_fname = argv[2] ;
  out_fname = argv[3] ;
  FileNameOnly(out_fname, fname) ;
  FileNameRemoveExtension(fname, fname) ;
  strcpy(mp.base_name, fname) ;
  mri_source = MRIread(source_fname) ;
  if (!mri_source)
    ErrorExit(ERROR_NOFILE, "%s: could not read source label volume %s",
	      Progname, source_fname) ;

  if (mri_source->type == MRI_INT)
    {
      MRI *mri_tmp = MRIchangeType(mri_source, MRI_FLOAT, 0, 1, 1) ;
      MRIfree(&mri_source); mri_source = mri_tmp ;
    }
  mri_target = MRIread(target_fname) ;
  if (!mri_target)
    ErrorExit(ERROR_NOFILE, "%s: could not read target label volume %s",
	      Progname, target_fname) ;
  if (mri_target->type == MRI_INT)
    {
      MRI *mri_tmp = MRIchangeType(mri_target, MRI_FLOAT, 0, 1, 1) ;
      MRIfree(&mri_target); mri_target = mri_tmp ;
    }
  if (erosions > 0)
    {
      int n ;
      for (n = 0 ; n < erosions ; n++)
	{
	  MRIerodeZero(mri_target, mri_target) ;
	  MRIerodeZero(mri_source, mri_source) ;
	}
    }
  if (scale_values > 0)
    {
      MRIscalarMul(mri_source, mri_source, scale_values) ;
      MRIscalarMul(mri_target, mri_target, scale_values) ;
    }
  if (transform && transform->type == MORPH_3D_TYPE)
    TransformRas2Vox(transform, mri_source,NULL) ;
  if (use_aseg == 0)
    {
      if (match_peak_intensity_ratio)
	MRImatchIntensityRatio(mri_source, mri_target, mri_source, .8, 1.2, 
			       100, 125) ;
      else if (match_mean_intensity)
	MRImatchMeanIntensity(mri_source, mri_target, mri_source) ;
      MRIboundingBox(mri_source, 0, &box) ;
      pad = (int)ceil(PADVOX * 
		      MAX(mri_target->xsize,MAX(mri_target->ysize,mri_target->zsize)) / 
		      MIN(mri_source->xsize,MIN(mri_source->ysize,mri_source->zsize))); 
#if 0
      { MRI *mri_tmp ;
	if (pad < 1)
	  pad = 1 ;
	printf("padding source with %d voxels...\n", pad) ;
	mri_tmp = MRIextractRegionAndPad(mri_source, NULL, &box, pad) ;
	if ((Gdiag & DIAG_WRITE) && DIAG_VERBOSE_ON)
	  MRIwrite(mri_tmp, "t.mgz") ;
	MRIfree(&mri_source) ;
	mri_source = mri_tmp ;
      }
#endif
    }
  mri_orig_source = MRIcopy(mri_source, NULL) ;

  mp.max_grad = 0.3*mri_source->xsize ;

  if (transform == NULL)
    transform = TransformAlloc(LINEAR_VOXEL_TO_VOXEL, NULL) ;

  if (transform->type != MORPH_3D_TYPE)  // initializing m3d from a linear transform
    {
      new_transform = 1 ;
      lta = ((LTA *)(transform->xform)) ;
      if (lta->type != LINEAR_VOX_TO_VOX)
	{
	  printf("converting ras xform to voxel xform\n") ;
	  m_L = MRIrasXformToVoxelXform(mri_source, mri_target, lta->xforms[0].m_L, NULL) ;
	  MatrixFree(&lta->xforms[0].m_L) ;
	  lta->type = LINEAR_VOX_TO_VOX ;
	}
      else
	{
	  printf("using voxel xform\n") ;
	  m_L = lta->xforms[0].m_L ;
	}
#if 0
      if (Gsx >= 0)   // update debugging coords
	{
	  VECTOR *v1, *v2 ;

	  v1 = VectorAlloc(4, MATRIX_REAL) ;
	  Gsx -= (box.x-pad) ;
	  Gsy -= (box.y-pad) ;
	  Gsz -= (box.z-pad) ;
	  V3_X(v1) = Gsx ; V3_Y(v1) = Gsy ; V3_Z(v1) = Gsz ;
	  VECTOR_ELT(v1,4) = 1.0 ;
	  v2 = MatrixMultiply(m_L, v1, NULL) ;
      
	  Gsx = nint(V3_X(v2)) ; Gsy = nint(V3_Y(v2)) ; Gsz = nint(V3_Z(v2)) ;
	  MatrixFree(&v2) ; MatrixFree(&v1) ;
	  printf("mapping by transform (%d, %d, %d) --> (%d, %d, %d) for rgb writing\n",
		 Gx, Gy, Gz, Gsx, Gsy, Gsz) ;
	}
#endif
      if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON)
	write_snapshot(mri_target, mri_source, m_L, &mp, 0, 1, "linear_init");

      lta->xforms[0].m_L = m_L ;
      printf("initializing GCAM with vox->vox matrix:\n") ;
      MatrixPrint(stdout, m_L) ;
      gcam = GCAMcreateFromIntensityImage(mri_source, mri_target, transform) ;
#if 0
      gcam->gca = gcaAllocMax(1, 1, 1, 
			      mri_target->width, mri_target->height, 
			      mri_target->depth,
			      0, 0) ;
#endif
      GCAMinitVolGeom(gcam, mri_source, mri_target) ;
      if (use_aseg)
	{
	  if (ribbon_name)
	    {
	      char fname[STRLEN], path[STRLEN], *str, *hemi ;
	      int  h, s, label ;
	      MRI_SURFACE *mris_white, *mris_pial ;
	      MRI         *mri ;

	      for (s = 0 ; s <= 1 ; s++) // source and target
		{
		  if (s == 0)
		    {
		      str = source_surf ;
		      mri = mri_source ;
		      FileNamePath(mri->fname, path) ;
		      strcat(path, "/../surf") ;
		    }
		  else
		    {
		      mri = mri_target ;
		      FileNamePath(mri->fname, path) ;
		      strcat(path, "/../elastic") ;
		      str = target_surf ;
		    }
		  // sorry - these values come from FreeSurferColorLUT.txt
		  MRIreplaceValueRange(mri, mri, 1000, 1034, Left_Cerebral_Cortex) ;
		  MRIreplaceValueRange(mri, mri, 1100, 1180, Left_Cerebral_Cortex) ;
		  MRIreplaceValueRange(mri, mri, 2000, 2034, Right_Cerebral_Cortex) ;
		  MRIreplaceValueRange(mri, mri, 2100, 2180, Right_Cerebral_Cortex) ;
		  for (h = LEFT_HEMISPHERE ; h <= RIGHT_HEMISPHERE ; h++)  
		    {
		      if (h == LEFT_HEMISPHERE)
			{
			  hemi = "lh" ;
			  label = Left_Cerebral_Cortex ;
			}
		      else
			{
			  label = Right_Cerebral_Cortex ;
			  hemi = "rh" ;
			}
		      sprintf(fname, "%s/%s%s.white", path, hemi, str) ;
		      mris_white = MRISread(fname) ;
		      if (mris_white == NULL)
			ErrorExit(ERROR_NOFILE, "%s: could not read surface %s", Progname, fname) ;
		      MRISsaveVertexPositions(mris_white, WHITE_VERTICES) ;
		      sprintf(fname, "%s/%s%s.pial", path, hemi, str) ;
		      mris_pial = MRISread(fname) ;
		      if (mris_pial == NULL)
			ErrorExit(ERROR_NOFILE, "%s: could not read surface %s", Progname, fname) ;
		      MRISsaveVertexPositions(mris_pial, PIAL_VERTICES) ;
		      if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON)
			{
			  sprintf(fname, "sb.mgz") ;
			  MRIwrite(mri_source, fname) ; 
			  sprintf(fname, "tb.mgz") ;
			  MRIwrite(mri_target, fname) ;
			}

		      insert_ribbon_into_aseg(mri, mri, mris_white, mris_pial, h) ;
		      if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON)
			{
			  sprintf(fname, "sa.mgz") ;
			  MRIwrite(mri_source, fname) ; 
			  sprintf(fname, "ta.mgz") ;
			  MRIwrite(mri_target, fname) ;
			}
		      MRISfree(&mris_white) ; MRISfree(&mris_pial) ;
		    }
		}
	      if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON)
		{
		  sprintf(fname, "s.mgz") ;
		  MRIwrite(mri_source, fname) ; 
		  sprintf(fname, "t.mgz") ;
		  MRIwrite(mri_target, fname) ;
		}
	    }
	  GCAMinitLabels(gcam, mri_target) ;
	  GCAMsetVariances(gcam, 1.0) ;
	  mp.mri_dist_map = create_distance_transforms(mri_source, mri_target, NULL, 40.0, gcam) ;
	}
    }
  else  /* use a previously create morph and integrate it some more */
    {
      printf("using previously create gcam...\n") ;
      gcam = (GCA_MORPH *)(transform->xform) ;
      GCAMrasToVox(gcam, mri_source) ;
      if (use_aseg)
	{
	  GCAMinitLabels(gcam, mri_target) ;
	  GCAMsetVariances(gcam, 1.0) ;
	  mp.mri_dist_map = create_distance_transforms(mri_source, mri_target, NULL, 40.0, gcam) ;
	}
      else
	GCAMaddIntensitiesFromImage(gcam, mri_target) ;
    }
  if (gcam->width != mri_source->width ||
      gcam->height != mri_source->height ||
      gcam->depth != mri_source->depth)
    ErrorExit(ERROR_BADPARM, "%s: warning gcam (%d, %d, %d), doesn't match source vol (%d, %d, %d)",
	      Progname, gcam->width, gcam->height, gcam->depth,
	      mri_source->width, mri_source->height, mri_source->depth) ;
	
  mp.mri_diag = mri_source ;
  mp.diag_morph_from_atlas = 0 ;
  mp.diag_write_snapshots = 1 ;
  mp.diag_sample_type = use_aseg ? SAMPLE_NEAREST : SAMPLE_TRILINEAR ;
  mp.diag_volume = use_aseg ? GCAM_LABEL : GCAM_MEANS ;

  if (renormalize)
    GCAMnormalizeIntensities(gcam, mri_target) ;
  if (mp.write_iterations != 0)
    {
      char fname[STRLEN] ;
      MRI  *mri_gca ;
		
      if (getenv("DONT_COMPRESS"))
        sprintf(fname, "%s_target.mgh", mp.base_name) ;
      else
        sprintf(fname, "%s_target.mgz", mp.base_name) ;
      if (mp.diag_morph_from_atlas == 0)
      {
        printf("writing target volume to %s...\n", fname) ;
        MRIwrite(mri_target, fname) ;
        sprintf(fname, "%s_target", mp.base_name) ;
        MRIwriteImageViews(mri_target, fname, IMAGE_SIZE) ;
      }
      else
      {
        if (use_aseg)
          mri_gca = GCAMwriteMRI(gcam, NULL, GCAM_LABEL) ;
        else
        {
          mri_gca = MRIclone(mri_source, NULL) ;
          GCAMbuildMostLikelyVolume(gcam, mri_gca) ;
        }
	  printf("writing target volume to %s...\n", fname) ;
	  MRIwrite(mri_gca, fname) ;
	  sprintf(fname, "%s_target", mp.base_name) ;
	  MRIwriteImageViews(mri_gca, fname, IMAGE_SIZE) ;
	  MRIfree(&mri_gca) ;
	}
    }

  if (nozero)
    {
      printf("disabling zero nodes\n") ;
      GCAMignoreZero(gcam, mri_target) ;
    }
  mp.mri = mri_target ;
  if (mp.regrid == True && new_transform == 0)
    GCAMregrid(gcam, mri_target, PAD, &mp, &mri_source) ;

  mp.write_fname = out_fname ;
  GCAMregister(gcam, mri_source, &mp) ; // atlas is target, morph target into register with it
  if (apply_transform)
    {
      MRI *mri_aligned ;
      char   fname[STRLEN] ;
		
      FileNameRemoveExtension(out_fname, fname) ;
      strcat(fname, ".mgz") ;
      mri_aligned = GCAMmorphToAtlas(mp.mri, gcam, NULL, -1, mp.diag_sample_type) ;
      printf("writing transformed output volume to %s...\n", fname) ;
      MRIwrite(mri_aligned, fname) ;
      MRIfree(&mri_aligned) ;
    }
  printf("writing warp vector field to %s\n", out_fname) ;
  GCAMvoxToRas(gcam) ;
  GCAMwrite(gcam, out_fname) ;
  GCAMrasToVox(gcam, mri_source) ;

  msec = TimerStop(&start) ;
  seconds = nint((float)msec/1000.0f) ;
  minutes = seconds / 60 ;
  seconds = seconds % 60 ;
  printf("registration took %d minutes and %d seconds.\n", 
	 minutes, seconds) ;
  exit(0) ;
  return(0) ;
}
コード例 #4
0
ファイル: mri_nlfilter.c プロジェクト: ewong718/freesurfer
int
main(int argc, char *argv[]) {
  char   **av ;
  int    ac, nargs, width, height, depth, x, y, z, xborder, yborder, zborder,
  xrborder, yrborder, zrborder ;
  char   *in_fname, *out_fname ;
  MRI    *mri_smooth, *mri_grad, *mri_filter_src, *mri_filter_dst, *mri_dst,
  *mri_tmp, *mri_blur, *mri_src, *mri_filtered, *mri_direction,
  *mri_offset, *mri_up, *mri_polv, *mri_dir, *mri_clip, *mri_full ;
  MRI_REGION  region, clip_region ;

  /* rkt: check for and handle version tag */
  nargs = handle_version_option (argc, argv, "$Id: mri_nlfilter.c,v 1.14 2011/03/02 00:04:23 nicks Exp $", "$Name:  $");
  if (nargs && argc - nargs == 1)
    exit (0);
  argc -= nargs;

  Progname = argv[0] ;
  ErrorInit(NULL, NULL, NULL) ;
  DiagInit(NULL, NULL, NULL) ;

  ac = argc ;
  av = argv ;
  for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) {
    nargs = get_option(argc, argv) ;
    argc -= nargs ;
    argv += nargs ;
  }

  if (argc < 3)
    usage_exit() ;

  in_fname = argv[1] ;
  out_fname = argv[2] ;

  mri_full = mri_src = MRIread(in_fname) ;
  if (!mri_src)
    ErrorExit(ERROR_NOFILE, "%s: could not read '%s'", Progname, in_fname) ;
  if (!FZERO(blur_sigma))   /* allocate a blurring kernel */
  {
    mri_blur = MRIgaussian1d(blur_sigma, 0) ;
    if (!mri_blur)
      ErrorExit(ERROR_BADPARM,
                "%s: could not allocate blurring kernel with sigma=%2.3f",
                Progname, blur_sigma) ;
  } else
    mri_blur = NULL ;

  MRIboundingBox(mri_full, 0, &clip_region) ;
  REGIONexpand(&clip_region, &clip_region, (filter_window_size+1)/2) ;
  mri_src = MRIextractRegion(mri_full, NULL, &clip_region) ;
  width = mri_src->width ;
  height = mri_src->height ;
  depth = mri_src->depth ;
  mri_dst = MRIclone(mri_src, NULL) ;
  if (!mri_dst)
    ErrorExit(ERROR_NOFILE, "%s: could allocate space for destination image",
              Progname) ;

  for (z = 0 ; z < depth ; z += region_size) {
    for (y = 0 ; y < height ; y += region_size) {
      DiagHeartbeat((float)(z*height+y) / (float)(height*(depth-1))) ;
      for (x = 0 ; x < width ; x += region_size) {
        region.x = x ;
        region.y = y ;
        region.z = z ;
        region.dx = region.dy = region.dz = region_size ;
        if (region.x == 142)
          DiagBreak() ;
        REGIONexpand(&region, &region, (filter_window_size+1)/2) ;
        MRIclipRegion(mri_src, &region, &region) ;
        if (region.x == 142)
          DiagBreak() ;

        /* check for < 0 width regions */
        xborder = x-region.x ;
        yborder = y-region.y ;
        zborder = z-region.z ;
        xrborder = MAX(0, (region.dx-xborder) - region_size) ;
        yrborder = MAX(0, (region.dy-yborder) - region_size) ;
        zrborder = MAX(0, (region.dz-zborder) - region_size) ;
#if 0
        if (region.dx < 2*xborder || region.dy<2*yborder||region.dz<2*zborder)
          continue ;
#endif

        if (DIAG_VERBOSE_ON && (Gdiag & DIAG_SHOW))
          fprintf(stderr, "extracting region (%d, %d, %d) --> (%d, %d, %d)...",
                  region.x,region.y,region.z, region.x+region.dx-1,
                  region.y+region.dy-1,region.z+region.dz-1) ;
        mri_clip = MRIextractRegion(mri_src, NULL, &region) ;
        if (DIAG_VERBOSE_ON && (Gdiag & DIAG_SHOW))
          fprintf(stderr, "done.\nsmoothing region and up-sampling...") ;

        if (mri_blur)   /* smooth the input image to generate offset field */
          mri_smooth = MRIconvolveGaussian(mri_clip, NULL, mri_blur) ;
        else
          mri_smooth = MRIcopy(mri_clip, NULL) ;  /* no smoothing */
        if (!mri_smooth)
          ErrorExit(ERROR_BADPARM, "%s: image smoothing failed", Progname) ;

        /* now up-sample the smoothed image, and compute offset field in
           up-sampled domain */
        mri_up = MRIupsample2(mri_smooth, NULL) ;
        if (!mri_up)
          ErrorExit(ERROR_BADPARM, "%s: up sampling failed", Progname) ;
        MRIfree(&mri_smooth) ;
        if (DIAG_VERBOSE_ON && (Gdiag & DIAG_SHOW))
          fprintf(stderr, "done.\n") ;
        mri_smooth = mri_up ;
        mri_grad = MRIsobel(mri_smooth, NULL, NULL) ;
        mri_dir = MRIclone(mri_smooth, NULL) ;
        MRIfree(&mri_smooth) ;
        if (DIAG_VERBOSE_ON && (Gdiag & DIAG_SHOW))
          fprintf(stderr, "computing direction map...") ;
        mri_direction =
          MRIoffsetDirection(mri_grad, offset_window_size, NULL, mri_dir) ;

        if (DIAG_VERBOSE_ON && (Gdiag & DIAG_SHOW))
          fprintf(stderr, "computing offset magnitudes...") ;
        MRIfree(&mri_grad) ;
        mri_offset =
          MRIoffsetMagnitude(mri_direction, NULL, offset_search_len);
        MRIfree(&mri_direction) ;
        if (!mri_offset)
          ErrorExit(ERROR_NOMEMORY,
                    "%s: offset calculation failed", Progname) ;
        if (DIAG_VERBOSE_ON && (Gdiag & DIAG_SHOW))
          fprintf(stderr, "done.\nfiltering image...") ;
        mri_filter_src = MRIupsample2(mri_clip, NULL) ;
        MRIfree(&mri_clip) ;
        switch (filter_type) {
        case FILTER_CPOLV_MEDIAN:
          mri_polv = MRIplaneOfLeastVarianceNormal(mri_filter_src,NULL,5);
          mri_filter_dst =
            MRIpolvMedian(mri_filter_src, NULL, mri_polv,filter_window_size);
          MRIfree(&mri_polv) ;
          break ;
        case FILTER_GAUSSIAN:
          mri_filter_dst =
            MRIconvolveGaussian(mri_filter_src, NULL, mri_gaussian) ;
          if (!mri_filter_dst)
            ErrorExit(ERROR_NOMEMORY,
                      "%s: could not allocate temporary buffer space",Progname);
          break ;
        case FILTER_MEDIAN:
          mri_filter_dst = MRImedian(mri_filter_src,NULL,filter_window_size, NULL);
          if (!mri_filter_dst)
            ErrorExit(ERROR_NOMEMORY,
                      "%s: could not allocate temporary buffer space",Progname);
          break ;
        case FILTER_MEAN:
          mri_filter_dst = MRImean(mri_filter_src, NULL, filter_window_size) ;
          if (!mri_filter_dst)
            ErrorExit(ERROR_NOMEMORY,
                      "%s: could not allocate temporary buffer space",Progname);
          break ;
        case FILTER_MINMAX:
          mri_filter_dst =
            MRIminmax(mri_filter_src, NULL, mri_dir, filter_window_size) ;
          if (!mri_filter_dst)
            ErrorExit(ERROR_NOMEMORY,
                      "%s: could not allocate space for filtered image",
                      Progname) ;
          break ;
        default:
          mri_filter_dst = MRIcopy(mri_filter_src, NULL) ; /* no filtering */
          break ;
        }
        MRIfree(&mri_dir) ;
        MRIfree(&mri_filter_src) ;

        if (no_offset)
        {
          mri_filtered = MRIcopy(mri_filter_dst, NULL) ;
        }
        else
        {
          if (DIAG_VERBOSE_ON && (Gdiag & DIAG_SHOW))
            fprintf(stderr, "applying offset field...") ;
          if (Gdiag & DIAG_WRITE)
            MRIwrite(mri_filter_dst, "minmax.mgz") ;
          mri_filtered = MRIapplyOffset(mri_filter_dst, NULL, mri_offset) ;
          if (!mri_filtered)
            ErrorExit(ERROR_NOMEMORY,
                      "%s: could not allocate filtered image", Progname) ;
          if (DIAG_VERBOSE_ON && (Gdiag & DIAG_SHOW))
            fprintf(stderr, "done.\n") ;
          if (region.x == 142)
          DiagBreak() ;
          MRIfree(&mri_offset) ;
        }
        MRIfree(&mri_filter_dst) ;
        if (Gdiag & DIAG_WRITE)
          MRIwrite(mri_filtered, "upfilt.mgz") ;
        mri_tmp = MRIdownsample2(mri_filtered, NULL) ;
        MRIfree(&mri_filtered) ;
        if (Gdiag & DIAG_WRITE)
          MRIwrite(mri_tmp, "downfilt.mgz") ;
        region.x += xborder ;
        region.y += yborder ;
        region.z += zborder ;
#if 0
        region.dx -=2*xborder;
        region.dy-= 2*yborder;
        region.dz -= 2 * zborder;
#else
        region.dx -= xrborder + xborder;
        region.dy -= yrborder + yborder;
        region.dz -= zrborder + zborder;
#endif
        if (region.dx <= 0 || region.dy <= 0 || region.dz <= 0) {
          fprintf(stderr, "invalid region: (%d,%d,%d) --> (%d,%d,%d)\n",
                  region.x,region.y,region.z,region.dx,region.dy,region.dz);
        } else
          MRIextractIntoRegion(mri_tmp,mri_dst,xborder,yborder,zborder,&region);
        MRIfree(&mri_tmp);
      }
    }
  }
  MRIextractIntoRegion(mri_dst,mri_full, 0, 0, 0, &clip_region);
  MRIfree(&mri_dst) ;
  if (DIAG_VERBOSE_ON && (Gdiag & DIAG_SHOW))
    fprintf(stderr, "writing output image %s...", out_fname) ;
  MRIwrite(mri_full, out_fname) ;
  MRIfree(&mri_full) ;
  if (DIAG_VERBOSE_ON && (Gdiag & DIAG_SHOW))
    fprintf(stderr, "done.\n") ;
  exit(0) ;
  return(0) ;  /* for ansi */
}
コード例 #5
0
ファイル: mri_extract.c プロジェクト: zkaufman/freesurfer
int
main(int argc, char *argv[]) {
  char   **av ;
  int    ac, nargs, x0, y0, z0, dx, dy, dz ;
  MRI    *mri_src, *mri_dst = NULL ;
  char   *in_dir, *out_dir ;
  MRI_REGION box ;

  /* rkt: check for and handle version tag */
  nargs = handle_version_option (argc, argv, "$Id: mri_extract.c,v 1.8 2016/06/08 13:42:17 fischl Exp $", "$Name:  $");
  if (nargs && argc - nargs == 1)
    exit (0);
  argc -= nargs;

  Progname = argv[0] ;
  ErrorInit(NULL, NULL, NULL) ;
  DiagInit(NULL, NULL, NULL) ;

  ac = argc ;
  av = argv ;
  for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) {
    if (isdigit(*(argv[1]+1)))
      break ; // not an option - a negative number
    nargs = get_option(argc, argv) ;
    argc -= nargs ;
    argv += nargs ;
  }

  /*
     command line:

     mri_extract <src_dir> x0 y0 z0 dx dy dz <dst_dir>
  */

  if (argc < 8)
    ErrorExit(ERROR_BADPARM,
              "usage: %s <src volume> x0 y0 z0 dx dy dz <dst volume>", Progname) ;

  in_dir = argv[1] ;
  if (sscanf(argv[2], "%d", &x0) != 1)
    ErrorExit(ERROR_BADPARM,
              "%s: could not scan x0 from '%s'", Progname, argv[2]) ;
  if (sscanf(argv[3], "%d", &y0) != 1)
    ErrorExit(ERROR_BADPARM,
              "%s: could not scan y0 from '%s'", Progname, argv[3]) ;
  if (sscanf(argv[4], "%d", &z0) != 1)
    ErrorExit(ERROR_BADPARM,
              "%s: could not scan z0 from '%s'", Progname, argv[4]) ;
  if (sscanf(argv[5], "%d", &dx) != 1)
    ErrorExit(ERROR_BADPARM,
              "%s: could not scan dx from '%s'", Progname, argv[5]) ;
  if (sscanf(argv[6], "%d", &dy) != 1)
    ErrorExit(ERROR_BADPARM,
              "%s: could not scan dy from '%s'", Progname, argv[6]) ;
  if (sscanf(argv[7], "%d", &dz) != 1)
    ErrorExit(ERROR_BADPARM,
              "%s: could not scan dz from '%s'", Progname, argv[7]) ;

  out_dir = argv[8] ;

  if (verbose)
    fprintf(stderr, "reading from %s...", in_dir) ;
  mri_src = MRIread(in_dir) ;
  if (verbose)
    fprintf(stderr, "done\n") ;

  if (!mri_src)
    exit(1) ;

  MRIboundingBox(mri_src, thresh, &box) ;
  if (x0 < 0)
    x0 = box.x-pad ;
  if (y0 < 0)
    y0 = box.y-pad ;
  if (z0 < 0)
    z0 = box.z-pad ;
  if (dx < 0)
    dx = box.dx+2*pad ;
  if (dy < 0)
    dy = box.dy+2*pad ;
  if (dz < 0)
    dz = box.dz+2*pad ;
  printf("using bounding box (%d, %d, %d) -> (%d, %d, %d)\n", x0, y0,z0, x0+dx-1, y0+dy-1,z0+dz-1) ;

  mri_dst = MRIextract(mri_src, NULL, x0, y0, z0, dx, dy, dz) ;

  if (!mri_dst)
    exit(1) ;

  if (verbose)
    fprintf(stderr, "\nwriting to %s", out_dir) ;
  MRIwrite(mri_dst, out_dir) ;
  if (verbose)
    fprintf(stderr, "\n") ;
  exit(0) ;
  return(0) ;
}