Esempio n. 1
0
int main(int argc, char *argv[]) {
  char *inner_mris_fname,*outer_mris_fname,*input_mri_pref,*output_mri_pref;
  MRI *mri=0,*mri_src=0;
  MRI_SURFACE *inner_mris=0,*outer_mris=0;
  int nargs;
  LABEL *area = NULL ;

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

  Progname=argv[0];
  if (argc > 1)
    while (*argv[1] == '-')
    {
      int nargs = 0 ;
      switch (toupper(argv[1][1]))
      {
      case 'L':
        printf("cropping ribbon to label file %s\n", argv[2]) ;
        nargs = 1 ;
        area = LabelRead(NULL, argv[2]) ;
        if (area == NULL)
          ErrorExit(ERROR_NOFILE, 
                    "%s: could not read label file %s\n", 
                    argv[2]) ;
        break ;
      default:
        break ;
      }
      argc -= (nargs+1) ;
      if (argc == 1) break;
      argv += (nargs+1) ;
    }

  /* Set command-line parameters */
  if (argc!=5) {
    printf("Usage: mri_ribbon [-l fname.label] \\ \n"
           "       inner_surface_fname \\ \n"
           "       outer_surface_fname \\ \n"
           "       input_volume_pref \\ \n"
           "       output_volume_pref\n");
    exit(1);
  }

  inner_mris_fname=argv[1];
  outer_mris_fname=argv[2];
  input_mri_pref=argv[3];
  output_mri_pref=argv[4];

  /* Read surface information from inner surface file */
  printf("Reading surface file %s.\n",inner_mris_fname);
  inner_mris=MRISread(inner_mris_fname);
  if (!inner_mris) {
    printf("Could not read surface file %s.\n",inner_mris_fname);
    exit(1);
  }

  /* Read surface information from outer surface file */
  printf("Reading surface file %s.\n",outer_mris_fname);
  outer_mris=MRISread(outer_mris_fname);
  if (!outer_mris) {
    printf("Could not read surface file %s.\n",outer_mris_fname);
    exit(1);
  }

  /* Read example volume from file */
  printf("Reading MRI volume directory %s.\n",input_mri_pref);
  mri_src=MRIread(input_mri_pref);
  if (!mri_src) {
    printf("Could not read MRI volume directory %s.\n",input_mri_pref);
    exit(1);
  }

  /* Extract ribbon */
  printf("Extracting ribbon.\n");
  mri=MRISribbon(inner_mris,outer_mris,mri_src,NULL);

  if (area)
    MRIcropVolumeToLabel(mri, mri, area, inner_mris, outer_mris) ;
  /* Save MRI volume to directory */
  printf("Writing volume file %s.\n",output_mri_pref);
  MRIwrite(mri,output_mri_pref);

  MRIfree(&mri);
  MRIfree(&mri_src);
  MRISfree(&inner_mris);
  MRISfree(&outer_mris);

  return 0;
}
Esempio n. 2
0
int
insert_ribbon_into_aseg(MRI *mri_src_aseg, MRI *mri_aseg, 
                        MRI_SURFACE *mris_white, MRI_SURFACE *mris_pial, 
                        int hemi)
{
  MRI  *mri_ribbon, *mri_white ;
  int  x, y, z, gm_label, wm_label, label, nbr_label, dont_change ;

  if (mri_src_aseg != mri_aseg)
    mri_aseg = MRIcopy(mri_src_aseg, mri_aseg) ;

  gm_label = hemi == LEFT_HEMISPHERE ? 
    Left_Cerebral_Cortex : Right_Cerebral_Cortex ;
  wm_label = hemi == LEFT_HEMISPHERE ? 
    Left_Cerebral_White_Matter : Right_Cerebral_White_Matter ;

  mri_white = MRIclone(mri_aseg, NULL) ;
  mri_ribbon = MRISribbon(mris_white, mris_pial, mri_aseg, NULL) ;
  MRISfillInterior(mris_white, mri_aseg->xsize, mri_white) ;

  for (x = 0 ; x < mri_aseg->width ; x++)
    for (y = 0 ; y < mri_aseg->height ; y++)
      for (z = 0 ; z < mri_aseg->depth ; z++)
      {
        if (x == Gx && y == Gy && z == Gz)
          DiagBreak() ;
        label = nint(MRIgetVoxVal(mri_aseg, x, y, z, 0)) ;
        if (nint(MRIgetVoxVal(mri_ribbon, x, y, z, 0)) == 255)  // in ribbon, set to GM
        {
          if (IS_CORTEX(label) || IS_WHITE_CLASS(label))
          {
            int  xi, yi, zi, xk, yk, zk ;
            // check to make sure we are really in cortex
            for (dont_change = 0, xk = -1 ; xk <= 1 ; xk++)
            {
              xi = mri_aseg->xi[xk+x] ;
              for (yk = -1 ; yk <= 1 ; yk++)
              {
                yi = mri_aseg->yi[yk+y] ;
                for (zk = -1 ; zk <= 1 ; zk++)
                {
                  zi = mri_aseg->zi[zk+z] ;
                  nbr_label = (int)MRIgetVoxVal(mri_aseg, xi, yi, zi, 0) ;
                  switch (nbr_label)
                  {
                  default:
                    break ;
                  case Left_Hippocampus:
                  case Right_Hippocampus:
                  case Left_Amygdala:
                  case Right_Amygdala:
                  case Left_VentralDC:
                  case Right_VentralDC:
                  case Brain_Stem:
                  case Left_Lateral_Ventricle:
                  case Right_Lateral_Ventricle:
                  case Left_Inf_Lat_Vent:
                  case Right_Inf_Lat_Vent:
                  case Left_Thalamus_Proper:
                  case Right_Thalamus_Proper:
                  case Left_choroid_plexus:
                  case Right_choroid_plexus:
                  case CC_Posterior:
                  case CC_Mid_Posterior:
                  case CC_Central:
                  case CC_Mid_Anterior:
                  case CC_Anterior:
                    dont_change = 1 ;
                    break ;
                  }
                }
              }
            }
            if (dont_change == 0)
              MRIsetVoxVal(mri_aseg, x, y, z, 0, gm_label) ;
          }
        }
        else  // not in ribbon
        {
          if (MRIgetVoxVal(mri_white, x, y, z, 0) > 0)  // inside white surface - disambiguate
          {
            if (label == gm_label)  // gm inside white surface should be wm
              MRIsetVoxVal(mri_aseg, x, y, z, 0, wm_label) ;
          }
          else if (label == gm_label)  // gm outside ribbon should be unknown
            MRIsetVoxVal(mri_aseg, x, y, z, 0, Unknown) ;
        }
      }


  if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON)
  {
    MRIwrite(mri_ribbon, "r.mgz") ;
    MRIwrite(mri_white, "w.mgz");
  }
  MRIfree(&mri_ribbon) ; MRIfree(&mri_white) ;
  return(NO_ERROR) ;
}