예제 #1
0
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 */
}
예제 #2
0
int
main(int argc, char *argv[]) {
  char          **av, fname[STRLEN], *T1_fname, *PD_fname, *output_dir, *mdir ;
  int           ac, nargs, msec, s ;
  MRI_SURFACE   *mris ;
  MRI           *mri_flash1, *mri_flash2, *mri_masked, *mri_masked_smooth, *mri_kernel,
  *mri_mean, *mri_dif, *mri_binary, *mri_distance ;
  MRI *mri_smooth, *mri_grad, *mri_inner ;
  struct timeb  then ;
  double        l_spring ;
  MRI_SEGMENTATION *mriseg ;


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

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

  memset(&parms, 0, sizeof(parms)) ;

  parms.projection = NO_PROJECTION ;
  parms.tol = 0.05 ;
  parms.check_tol = 1 ;
  parms.ignore_energy = 1 ;
  parms.dt = 0.5f ;
  parms.base_dt = BASE_DT_SCALE*parms.dt ;

  parms.l_spring_norm = 1 ;
  parms.l_shrinkwrap = 0 ;
  parms.l_intensity = 1 ;

  parms.niterations = 0 ;
  parms.write_iterations = 0 /*WRITE_ITERATIONS */;
  parms.integration_type = INTEGRATE_MOMENTUM ;
  parms.momentum = 0.0 /*0.8*/ ;
  parms.l_intensity = 1 ;
  parms.dt_increase = 1.0 /* DT_INCREASE */;
  parms.dt_decrease = 0.50 /* DT_DECREASE*/ ;
  parms.error_ratio = 50.0 /*ERROR_RATIO */;
  /*  parms.integration_type = INTEGRATE_LINE_MINIMIZE ;*/
  parms.l_surf_repulse = 0.0 ;
  parms.l_repulse = 0 /*1*/ ;

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

  mdir = getenv("FREESURFER_HOME") ;
  if (!mdir)
    ErrorExit(ERROR_BADPARM, "FREESURFER_HOME not defined in environment") ;

  if (argc < 4)
    usage_exit() ;

  /* set default parameters for white and gray matter surfaces */
  parms.niterations = 1000 ;
  if (parms.momentum < 0.0)
    parms.momentum = 0.0 /*0.75*/ ;

  TimerStart(&then) ;
  T1_fname = argv[1] ;
  PD_fname = argv[2] ;
  output_dir = argv[3] ;
  fprintf(stderr, "reading volume %s...\n", T1_fname) ;
  mri_flash1 = MRIread(T1_fname) ;
  if (!mri_flash1)
    ErrorExit(ERROR_NOFILE, "%s: could not read input volume %s", Progname, T1_fname) ;

  mri_flash2 = MRIread(PD_fname) ;
  if (!mri_flash2)
    ErrorExit(ERROR_NOFILE, "%s: could not read input volume %s", Progname, T1_fname) ;

  //  setMRIforSurface(mri_flash1);
  sprintf(fname, "%s/lib/bem/ic%d.tri", mdir, ic_init) ;
  mris = MRISread(fname) ;
  if (!mris)
    ErrorExit(ERROR_NOFILE, "%s: could not read icosahedron %s", Progname, fname) ;

  mri_mean = MRImean(mri_flash1, NULL, 5) ;
  MRIwrite(mri_mean, "mean.mgz") ;

  mri_dif = MRIabsdiff(mri_flash1, mri_flash2, NULL) ;
  MRIwrite(mri_dif, "dif.mgz") ;

  mriseg = MRIsegment(mri_mean, 30, 100000) ;
  s = MRIsegmentMax(mriseg) ;
  mri_masked = MRIsegmentToImage(mri_flash1, NULL, mriseg, s) ;
  MRIwrite(mri_masked, "mask.mgz") ;
  MRIsegmentFree(&mriseg) ;

  // MRIthresholdMask(mri_dif, mri_masked, mri_dif, 1, 0) ;
  // MRIwrite(mri_dif, "dif_masked.mgz") ;


  mri_kernel = MRIgaussian1d(2, 0) ;
  mri_smooth = MRIconvolveGaussian(mri_dif, NULL, mri_kernel) ;
  MRIwrite(mri_smooth, "smooth.mgz") ;
  MRIScopyVolGeomFromMRI(mris, mri_smooth) ;
  mris->useRealRAS = 1 ;

  initialize_surface_position(mris, mri_dif, 1, &parms) ;
  MRISwrite(mris, "init") ;
  MRISrepositionToInnerSkull(mris, mri_smooth, &parms) ;

  exit(0) ;
  mri_grad = MRIsobel(mri_smooth, NULL, NULL) ;
  MRIwrite(mri_grad, "grad.mgz") ;
  mri_inner = MRIfindInnerBoundary(mri_dif, mri_grad, NULL, 5.0) ;
  MRIwrite(mri_inner, "inner.mgz") ;
  MRIbinarize(mri_inner, mri_inner, 10, 0, 128) ;

  MRISpositionOptimalSphere(mris, mri_inner, 6) ;
  MRISwrite(mris, "optimal") ;
  exit(0) ;
  parms.sigma = 4 / mri_flash1->xsize ;
  // mri_dist = create_distance_map(mri_masked, NULL, BORDER_VAL, OUTSIDE_BORDER_STEP) ;
  MRISsetVals(mris,parms.sigma) ;
  MRIScopyValToVal2(mris) ;
  MRISsetVals(mris, 0) ;
  sprintf(parms.base_name, "%s_inner_skull%s%s", "test", output_suffix, suffix) ;
  parms.mri_brain = mri_masked ;
  l_spring = parms.l_spring_norm ;
  mri_kernel = MRIgaussian1d(parms.sigma, 0) ;


  mri_binary = MRIbinarize(mri_dif, mri_binary, 40, 0, 128) ;
  MRIwrite(mri_binary, "bin.mgz") ;
  mri_distance = MRIdistanceTransform(mri_binary, NULL, 128, 100, DTRANS_MODE_SIGNED, NULL) ;
  MRIwrite(mri_distance, "dist.mgz") ;
  mri_masked_smooth = MRIconvolveGaussian(mri_distance, NULL, mri_kernel) ;
  MRIfree(&mri_kernel) ;
  MRIwrite(mri_masked_smooth, "dif_smooth.mgz") ;

  MRISwrite(mris, "inner_skull.tri") ;

  msec = TimerStop(&then) ;
  fprintf(stderr,"positioning took %2.1f minutes\n", (float)msec/(60*1000.0f));
  exit(0) ;
  return(0) ;  /* for ansi */
}