Example #1
0
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) ;
}
Example #2
0
int
main(int argc, char *argv[]) {
  char        **av, *out_fname, *T1_fname, *PD_fname ;
  int         ac, nargs ;
  MRI         *mri_T1, *mri_PD, *mri_out, *mri_T2star = NULL ;
  float       TR, TE, alpha ;

  char cmdline[CMD_LINE_LEN] ;

  make_cmd_version_string (argc, argv, "$Id: mri_synthesize.c,v 1.18 2011/03/02 00:04:25 nicks Exp $", "$Name:  $", cmdline);

  /* rkt: check for and handle version tag */
  nargs = handle_version_option (argc, argv, "$Id: mri_synthesize.c,v 1.18 2011/03/02 00:04:25 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 < 7)
    usage_exit() ;

  TR = atof(argv[1]) ;
  alpha = atof(argv[2]) ;
  TE = atof(argv[3]) ;
  T1_fname = argv[4] ;
  PD_fname = argv[5] ;
  out_fname = argv[6] ;

  printf("reading T1 volume from %s...\n", T1_fname) ;
  mri_T1 = MRIread(T1_fname) ;
  if (!mri_T1)
    ErrorExit(ERROR_NOFILE, "%s: could not read T1 volume %s", Progname,
              T1_fname) ;
  if (extract) {
    MRI *mri_tmp ;
    int dx, dy, dz ;

    dx = mri_T1->width/2 ;
    dy = mri_T1->height/2 ;
    dz = mri_T1->depth/2 ;
    printf("extracting interior %dx%dx%d\n", dx, dy, dz) ;
    mri_tmp = MRIextract(mri_T1, NULL, dx/2, dy/2, dz/2, dx, dy, dz) ;
    MRIfree(&mri_T1) ;
    mri_T1 = mri_tmp ;
  }
  if (jpdf_name) {
    transform_T1_values_using_joint_pdf(mri_T1, jpdf_name, invert) ;
  }

  printf("reading PD volume from %s...\n", PD_fname) ;
  mri_PD = MRIread(PD_fname) ;
  if (!mri_PD)
    ErrorExit(ERROR_NOFILE, "%s: could not read PD volume %s", Progname,
              PD_fname) ;

  if (T2star_fname != NULL) {
    printf("reading T2* volume from %s...\n", T2star_fname) ;
    mri_T2star = MRIread(T2star_fname) ;
    if (!mri_T2star)
      ErrorExit(ERROR_NOFILE, "%s: could not read T2* volume %s", Progname,
                T2star_fname) ;
  }

  if (PDsat > 0)
    saturate_PD(mri_PD, PDsat) ;
  if (extract) {
    MRI *mri_tmp ;
    int dx, dy, dz ;

    dx = mri_PD->width/2 ;
    dy = mri_PD->height/2 ;
    dz = mri_PD->depth/2 ;
    mri_tmp = MRIextract(mri_PD, NULL, dx/2, dy/2, dz/2, dx, dy, dz) ;
    MRIfree(&mri_PD) ;
    mri_PD = mri_tmp ;
  }
  if (use_weighting) {
    mri_out = MRIsynthesizeWeightedVolume(mri_T1, mri_PD, w5, TR, w30, TR, 110,TE);
  } else {
    printf("synthesizing volume with TR=%2.1f msec, TE=%2.1f msec, and alpha=%2.2f degrees...\n",
           TR, TE, alpha) ;
    if (normalize)
      normalize_PD(mri_PD, 1000) ;
    if (discard)
      discard_PD(mri_PD, 250, 1500) ;
    if (nl_remap_T1)
      remap_T1(mri_T1, nl_mean, nl_scale) ;
    if (nfaf > 0)
      mri_out = MRIsynthesizeWithFAF(mri_T1, mri_PD, NULL, TR, RADIANS(alpha), TE, nfaf, faf_coefs) ;
    else
      mri_out = MRIsynthesize(mri_T1, mri_PD, mri_T2star, NULL, TR, RADIANS(alpha), TE) ;
  }

  if (nbias > 0)
    apply_bias_field(mri_out, nbias, bias_coefs) ;
  printf("writing output to %s.\n", out_fname) ;
  MRIaddCommandLine(mri_out, cmdline) ;
  MRIwrite(mri_out, out_fname) ;

  exit(0) ;
  return(0) ;  /* for ansi */
}
Example #3
0
int
main(int argc, char *argv[])
{
  char         **av, in_surf_fname[STRLEN], *in_patch_fname, *out_patch_fname,
  fname[STRLEN], path[STRLEN], *cp, hemi[10] ;
  int          ac, nargs ;
  MRI_SURFACE  *mris ;
  MRI          *mri_vertices ;

  /* rkt: check for and handle version tag */
  nargs = handle_version_option
          (argc, argv,
           "$Id: mris_flatten.c,v 1.42 2016/12/10 22:57:46 fischl Exp $",
           "$Name:  $");
  if (nargs && argc - nargs == 1)
    exit (0);
  argc -= nargs;

  Gdiag |= DIAG_SHOW ;
  Progname = argv[0] ;
  ErrorInit(NULL, NULL, NULL) ;
  DiagInit(NULL, NULL, NULL) ;
  Gdiag |= (DIAG_SHOW | DIAG_WRITE) ;
  memset(&parms, 0, sizeof(parms)) ;
  parms.dt = .1 ;
  parms.projection = PROJECT_PLANE ;
  parms.tol = 0.2 ;
  parms.n_averages = 1024 ;
  parms.l_dist = 1.0 ;
  parms.l_nlarea = 1.0 ;
  parms.niterations = 40 ;
  parms.area_coef_scale = 1.0 ;
  parms.dt_increase = 1.01 /* DT_INCREASE */;
  parms.dt_decrease = 0.98 /* DT_DECREASE*/ ;
  parms.error_ratio = 1.03 /*ERROR_RATIO */;
  parms.integration_type = INTEGRATE_LINE_MINIMIZE ;
  parms.momentum = 0.9 ;
  parms.desired_rms_height = -1.0 ;
  parms.base_name[0] = 0 ;
  parms.nbhd_size = 7 ;    /* out to 7-connected neighbors */
  parms.max_nbrs = 12 ;    /* 12 at each distance */
  ac = argc ;
  av = argv ;
  for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++)
  {
    nargs = get_option(argc, argv) ;
    argc -= nargs ;
    argv += nargs ;
  }

  if (argc < 3)
    print_help() ;

  parms.base_dt = base_dt_scale * parms.dt ;
  in_patch_fname = argv[1] ;
  out_patch_fname = argv[2] ;
  FileNamePath(in_patch_fname, path) ;
  cp = strrchr(in_patch_fname, '/') ;
  if (!cp)
    cp = in_patch_fname ;
  cp = strchr(cp, '.') ;
  if (cp)
  {
    strncpy(hemi, cp-2, 2) ;
    hemi[2] = 0 ;
  }
  else
    strcpy(hemi, "lh") ;
  if (one_surf_flag)
    sprintf(in_surf_fname, "%s", in_patch_fname) ;
  else
    sprintf(in_surf_fname, "%s/%s.%s", path, hemi, original_surf_name) ;

  if (parms.base_name[0] == 0)
  {
    FileNameOnly(out_patch_fname, fname) ;
    cp = strchr(fname, '.') ;
    if (cp)
      strcpy(parms.base_name, cp+1) ;
    else
      strcpy(parms.base_name, "flattened") ;
  }

  mris = MRISread(in_surf_fname) ;
  if (!mris)
    ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s",
              Progname, in_surf_fname) ;

  if (sphere_flag)
  {
    MRIScenter(mris, mris) ;
    mris->radius = MRISaverageRadius(mris) ;
    MRISstoreMetricProperties(mris) ;
    MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ;
  }

  if (Gdiag_no >= 0)
  {
    int n ;
    printf("vertex %d has %d nbrs before patch:\n",
           Gdiag_no, mris->vertices[Gdiag_no].vnum) ;
    for (n = 0 ; n < mris->vertices[Gdiag_no].vnum ; n++)
      printf("\t%d\n", mris->vertices[Gdiag_no].v[n]) ;
  }
  if (one_surf_flag)  /* only have the 1 surface - no patch file */
  {
    mris->patch = 1 ;
    mris->status = MRIS_PATCH ;
    if (!FEQUAL(rescale,1))
    {
      MRISscaleBrain(mris, mris, rescale) ;
      MRIScomputeMetricProperties(mris) ;
    }
    MRISstoreMetricProperties(mris) ;
    MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ;

  } 
  else
  {
    MRISresetNeighborhoodSize(mris, mris->vertices[0].nsize) ; // set back to max
    if (label_fname) // read in a label instead of a patch
    {
      LABEL *area ;
      area = LabelRead(NULL, label_fname) ;
      if (area == NULL)
        ErrorExit(ERROR_BADPARM, "%s: could not read label file %s",
                  Progname, label_fname) ;

      LabelDilate(area, mris, dilate_label, CURRENT_VERTICES) ;
      MRISclearMarks(mris) ;
      LabelMark(area, mris) ;
      MRISripUnmarked(mris) ;
      MRISripFaces(mris);
      mris->patch = 1 ;
      mris->status = MRIS_CUT ;
      LabelFree(&area) ;
      printf("%d valid vertices (%2.1f %% of total)\n",
             MRISvalidVertices(mris), 
             100.0*MRISvalidVertices(mris)/mris->nvertices) ;
    }
    else
    {
      if (MRISreadPatch(mris, in_patch_fname) != NO_ERROR)
        ErrorExit(ERROR_BADPARM, "%s: could not read patch file %s",
                  Progname, in_patch_fname) ;
      if (dilate)
      {
        printf("dilating patch %d times\n", dilate) ;
        MRISdilateRipped(mris, dilate) ;
        printf("%d valid vertices (%2.1f %% of total)\n",
               MRISvalidVertices(mris), 100.0*MRISvalidVertices(mris)/mris->nvertices) ;
      }
    }
    MRISremoveRipped(mris) ;
    MRISupdateSurface(mris) ;
#if 0
    mris->nsize = 1 ; // before recalculation of 2 and 3-nbrs
    {
      int vno ;
      VERTEX *v ;
      for (vno= 0 ; vno < mris->nvertices ; vno++)
      {
        v = &mris->vertices[vno] ;
        v->vtotal = v->vnum ;
        v->nsize = 1 ;
      }
    }
    MRISsetNeighborhoodSize(mris, nbrs) ;
#endif
  }

  if (Gdiag_no >= 0)
    printf("vno %d is %sin patch\n", Gdiag_no,
           mris->vertices[Gdiag_no].ripflag ? "NOT " : "") ;

  if (Gdiag_no >= 0 && mris->vertices[Gdiag_no].ripflag == 0)
  {
    int n ;
    printf("vertex %d has %d nbrs after patch:\n",
           Gdiag_no, mris->vertices[Gdiag_no].vnum) ;
    for (n = 0 ; n < mris->vertices[Gdiag_no].vnum ; n++)
      printf("\t%d\n", mris->vertices[Gdiag_no].v[n]) ;
  }
  fprintf(stderr, "reading original vertex positions...\n") ;
  if (!FZERO(disturb))
    mrisDisturbVertices(mris, disturb) ;
  if (parms.niterations > 0)
  {
    MRISresetNeighborhoodSize(mris, nbrs) ;

    if (!FZERO(parms.l_unfold) || !FZERO(parms.l_expand))
    {
      static INTEGRATION_PARMS p2 ;
      sprintf(in_surf_fname, "%s/%s.%s", path, hemi, original_surf_name) ;
      if (stricmp(original_unfold_surf_name,"none") == 0)
      {
        printf("using current position of patch as initial position\n") ;
        MRISstoreMetricProperties(mris) ;  /* use current positions */
      }
      else if (!sphere_flag && !one_surf_flag)
        MRISreadOriginalProperties(mris, original_unfold_surf_name) ;
      *(&p2) = *(&parms) ;
      p2.l_dist = 0 ;
      p2.niterations = 100 ;
      p2.nbhd_size = p2.max_nbrs = 1 ;
      p2.n_averages = 0 ;
      p2.write_iterations = parms.write_iterations > 0 ? 25 : 0 ;
      p2.tol = -1 ;
      p2.dt = 0.5 ;
      p2.l_area = 0.0 ;
      p2.l_spring = 0.9 ;
      p2.l_convex = 0.9 ;
      p2.momentum = 0 ;
      p2.integration_type = INTEGRATE_MOMENTUM ;
      MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ;
#if 0
      p2.flags |= IPFLAG_NO_SELF_INT_TEST ;
      printf("expanding surface....\n") ;
      MRISexpandSurface(mris, 4.0, &p2) ;  // push it away from fissure
#endif
      p2.niterations = 100 ;
      MRISunfold(mris, &p2, 1) ;
      p2.niterations = 300 ;
      p2.l_unfold *= 0.25 ;
      MRISunfold(mris, &p2, 1) ;
      p2.l_unfold *= 0.25 ;
      MRISunfold(mris, &p2, 1) ;
#if 0
      printf("smoothing unfolded surface..\n");
      p2.niterations = 200 ;
      p2.l_unfold = 0 ;  // just smooth it
      MRISunfold(mris, &p2, max_passes) ;
#endif
      parms.start_t = p2.start_t ;
      parms.l_unfold = parms.l_convex = parms.l_boundary = parms.l_expand=0 ;
      MRIfree(&parms.mri_dist) ;
    }

    sprintf(in_surf_fname, "%s/%s.%s", path, hemi, original_surf_name) ;
    if (!sphere_flag && !one_surf_flag)
      MRISreadOriginalProperties(mris, original_surf_name) ;
    if (randomly_flatten)
      MRISflattenPatchRandomly(mris) ;
    else
      MRISflattenPatch(mris) ;

    /* optimize metric properties of flat map */
    fprintf(stderr,"minimizing metric distortion induced by projection...\n");
    MRISscaleBrain(mris, mris, scale) ;
    MRIScomputeMetricProperties(mris) ;
    MRISunfold(mris, &parms, max_passes) ;
    MRIScenter(mris, mris) ;
    fprintf(stderr, "writing flattened patch to %s\n", out_patch_fname) ;
    MRISwritePatch(mris, out_patch_fname) ;
  }

  if (plane_flag || sphere_flag)
  {
    char fname[STRLEN] ;
    FILE *fp ;

#if 0
    sprintf(fname, "%s.%s.out",
            mris->hemisphere == RIGHT_HEMISPHERE ? "rh" : "lh",
            parms.base_name);
#else
    sprintf(fname, "flatten.log") ;
#endif
    fp = fopen(fname, "a") ;

    if (plane_flag)
      MRIScomputeAnalyticDistanceError(mris, MRIS_PLANE, fp) ;
    else if (sphere_flag)
      MRIScomputeAnalyticDistanceError(mris, MRIS_SPHERE, fp) ;
    fclose(fp) ;
  }

  if (mri_overlay)
  {
    MRI  *mri_flattened ;
    char fname[STRLEN] ;

    // if it is NxNx1x1 reshape it to be Nx1x1xN
    if ( mri_overlay->width == mri_overlay->height &&
       mri_overlay->depth == 1 &&
       mri_overlay->nframes == 1)
    {
      MRI *mri_tmp ;
      printf("reshaping to move 2nd dimension to time\n") ;
      mri_tmp = mri_reshape( mri_overlay, mri_overlay->width, 1, 1, mri_overlay->height);
      MRIfree( &mri_overlay );
      mri_overlay = mri_tmp;
    }

    // put in some special code that knows about icosahedra
    if (mris->nvertices == 163842 ||  // ic7
        mris->nvertices == 40962 ||  // ic6
        mris->nvertices == 10242 ||  // ic5
        mris->nvertices == 2562)  // ic4
    {
      int nvals, start_index, end_index ;
      MRI *mri_tmp ;
      
      printf("cross-hemispheric correlation matrix detected, reshaping...\n") ;
      nvals = mri_overlay->width * mri_overlay->height * mri_overlay->depth ;
      if (nvals == 2*mris->nvertices)   // it's a corr matrix for both hemis
      {
        if (mris->hemisphere == LEFT_HEMISPHERE || mris->hemisphere == RIGHT_HEMISPHERE)
        {
          if (mris->hemisphere == LEFT_HEMISPHERE)
          {
            start_index = 0 ; 
            end_index = mris->nvertices-1 ;
          }
          else
          {
            start_index = mris->nvertices ; 
            end_index = 2*mris->nvertices-1 ;
          }
          mri_tmp = MRIextract(mri_overlay, NULL, start_index, 0, 0, mris->nvertices, 1, 1) ;
          MRIfree(&mri_overlay) ;
          mri_overlay = mri_tmp;
        }
        else // both hemis
        {
        }
      }
    }
    
    printf("resampling overlay (%d x %d x %d x %d) into flattened coordinates..\n",
           mri_overlay->width, mri_overlay->height, mri_overlay->depth, mri_overlay->nframes) ;
    if (synth_name)
    {
      LABEL *area_lh, *area_rh ;
      char  fname[STRLEN], path[STRLEN], fname_no_path[STRLEN] ;
      int   vno, n, vno2, n2 ;

      MRIsetValues(mri_overlay, 0) ;
      FileNameOnly(synth_name, fname_no_path) ;
      FileNamePath(synth_name, path) ;
      sprintf(fname, "%s/lh.%s", path, fname_no_path) ;
      area_lh = LabelRead(NULL, fname) ;
      if (area_lh == NULL)
        ErrorExit(ERROR_NOFILE, "%s: could not read label from %s",
                  Progname,fname) ;
      sprintf(fname, "%s/rh.%s", path, fname_no_path) ;
      area_rh = LabelRead(NULL, fname) ;
      if (area_rh == NULL)
        ErrorExit(ERROR_NOFILE, "%s: could not read label from %s",
                  Progname,fname) ;
#if 0
      for (n = 0 ; n < area_lh->n_points ; n++)
      {
        vno = area_lh->lv[n].vno ;
        MRIsetVoxVal(mri_overlay, vno, 0, 0, vno, 1) ;
	printf("synthesizing map with vno %d: (%2.1f, %2.1f)\n", vno, mris->vertices[vno].x, mris->vertices[vno].y) ;
        break ;
      }
#else
      for (n = 0 ; n < area_lh->n_points ; n++)
      {
        vno = area_lh->lv[n].vno ;
        if (vno >= 0)
        {
          for (n2 = 0 ; n2 < area_lh->n_points ; n2++)
          {
            vno2 = area_lh->lv[n2].vno ;
            if (vno2 >= 0)
              MRIsetVoxVal(mri_overlay, vno, 0, 0, vno2, 1) ;
          }
          for (n2 = 0 ; n2 < area_rh->n_points ; n2++)
          {
            vno2 = area_rh->lv[n2].vno ;
            if (vno2 >= 0)
              MRIsetVoxVal(mri_overlay, vno, 0, 0, mris->nvertices+vno2, 1) ;
          }
        }
      }
#endif
    }

    mri_flattened = MRIflattenOverlay(mris, mri_overlay, NULL, 1.0, label_overlay, &mri_vertices) ;
    printf("writing flattened overlay to %s\n", out_patch_fname) ;
    MRIwrite(mri_flattened, out_patch_fname) ;
    MRIfree(&mri_flattened) ;

    FileNameRemoveExtension(out_patch_fname, fname) ;
    strcat(fname, ".vnos.mgz") ;
    printf("writing flattened vertex #s to %s\n", fname) ;
    MRIwrite(mri_vertices, fname) ;
    MRIfree(&mri_vertices) ;
  }
#if 0
  sprintf(fname, "%s.area_error", out_fname) ;
  printf("writing area errors to %s\n", fname) ;
  MRISwriteAreaError(mris, fname) ;
  sprintf(fname, "%s.angle_error", out_fname) ;
  printf("writing angle errors to %s\n", fname) ;
  MRISwriteAngleError(mris, fname) ;
  MRISfree(&mris) ;
#endif

  exit(0) ;
  return(0) ;  /* for ansi */
}
Example #4
0
static int
MRIcheckRemovals(MRI *mri_T1, MRI *mri_dst, MRI *mri_labels, int wsize)
{
    int    x, y, z, width, depth, height, whalf, ntested, nchanged, on, vertex;
    MRI    *mri_tmp, *mri_region, *mri_plane, *mri_binary_plane ;
    float  min_on ;

    whalf = (wsize-1)/2 ;

    mri_tmp = MRIcopy(mri_dst, NULL) ;
    mri_region = MRIalloc(wsize, wsize, wsize, MRI_UCHAR) ;
    min_on = .1*wsize*wsize ;
    MRIcopyLabel(mri_labels, mri_tmp, 255) ;

    MRIbinarize(mri_tmp, mri_tmp, WM_MIN_VAL, 0, 100) ;
    width = mri_T1->width ;
    height = mri_T1->height ;
    depth = mri_T1->depth ;

    ntested = nchanged = 0 ;
    if (Gdiag == 99)
    {
        MRIwrite(mri_tmp, "tmp.mgh") ;
    }
    for (z = 0 ; z < depth ; z++)
    {
        for (y = 0 ; y < height ; y++)
        {
            for (x = 0 ; x < width ; x++)
            {
                if (z == 87 && y == 88 && x == 163)  /* test1 cs filled */
                {
                    DiagBreak() ;
                }
                if (z == 88 && y == 89 && x == 163)  /* test1 cs filled */
                {
                    DiagBreak() ;
                }

                if (z == 101 && y == 133 && x == 152)
                {
                    DiagBreak() ;
                }
                if (x == 157 && y == 143 && z == 98)
                {
                    DiagBreak() ;
                }
                if (x == 156 && y == 143 && z == 98)
                {
                    DiagBreak() ;
                }

                if (x == 154 && y == 167 && z == 128)
                {
                    DiagBreak() ;
                }
                if (x == 136 && y == 147 && z == 28)
                {
                    DiagBreak() ;
                }

                if (x == 163 && y == 88 && z == 86)
                {
                    DiagBreak() ;
                }

                if ((x == 140 && y == 141 && z == 54) ||
                        (x == 140 && y == 141 && z == 53) ||
                        (x == 140 && y == 142 && z == 53) ||
                        (x == 140 && y == 142 && z == 54) ||
                        (x == 140 && y == 140 && z == 53))
                {
                    DiagBreak() ;  /* test4 cerebellum */
                }
                if (x == 142 && y == 139 && z == 54)   /* test4 */
                {
                    DiagBreak() ;
                }

                if (!MRIgetVoxVal(mri_labels, x, y, z, 0))
                {
                    continue ;
                }
                ntested++ ;

                MRIextract(mri_tmp, mri_region, x-whalf,y-whalf,z-whalf,
                           wsize, wsize, wsize) ;

                vertex =
                    MRIcountCpolvOnAtVoxel(mri_region, whalf, whalf, whalf, wsize, &on) ;

                mri_plane = MRIextractVertexPlane(mri_tmp, NULL, vertex,x,y,z,wsize);
                MRIthreshold(mri_plane, mri_plane, 50) ;
                MRIremove1dStructures(mri_plane,mri_plane, 10000,2,NULL);
                mri_binary_plane = MRIfillFG(mri_plane, NULL, whalf, whalf, 0,
                                             50, 128, &on) ;
                if (on > min_on)
                {
                    int  xk, yk, i, ntransitions, i_prev  ;

                    /*
                       now look at the winding # (number of white-black transitions
                       in a circle around the central point
                    */

                    ntransitions = 0 ;
                    for (i = 0 ; i < NPTS ; i++)
                    {
                        xk = xpts[i] ;
                        yk = ypts[i] ;
                        i_prev = i-1 ;
                        if (i_prev < 0)
                        {
                            i_prev = NPTS-1 ;
                        }
                        if (MRIgetVoxVal(mri_binary_plane, whalf+xpts[i], whalf+ypts[i], 0, 0) !=
                                MRIgetVoxVal(mri_binary_plane,whalf+xpts[i_prev],
                                             whalf+ypts[i_prev],0, 0))
                        {
                            ntransitions++ ;
                        }
                    }
                    if (ntransitions > 2)   /* not planar */
                    {
                        nchanged++ ;
                        MRIsetVoxVal(mri_dst, x, y, z, 0, MRIgetVoxVal(mri_T1, x, y, z, 0)) ;
                    }
                }
                if (Gdiag & DIAG_WRITE)
                {
                    MRIwrite(mri_region, "region.mgh") ;
                    MRIwrite(mri_plane, "plane.mgh") ;
                    MRIwrite(mri_binary_plane, "binary_plane.mgh") ;
                }
                MRIfree(&mri_plane) ;
                MRIfree(&mri_binary_plane) ;
            }
        }
    }
    MRIfree(&mri_tmp) ;
    MRIfree(&mri_region) ;
    if (Gdiag & DIAG_SHOW)
    {
        fprintf(stderr, "               %8d voxels tested (%2.2f%%)\n",
                ntested, 100.0f*(float)ntested/ (float)(width*height*depth));
        fprintf(stderr, "               %8d voxels restored (%2.2f%%)\n",
                nchanged, 100.0f*(float)nchanged/ (float)(width*height*depth));
    }
    return(NO_ERROR) ;
}