MRI *  
correct_largestCC_and_fill_holes(MRI *segmri, MRI *outmri)
{
  int i, x, y, z, width, height, depth = 0, val;
  width  = segmri->width ;
  height = segmri->height ;
  depth  = segmri->depth ;
  // char fname[STR_LEN];

  if (!outmri)
    printf("no outmri volume exists!\n") ;
    //outmri = MRIclone(segmri, NULL) ;
    //outmri = MRIalloc(width, height, depth, MRI_INT);
  
  int *segidlist;
  int nsegids = 0;
  segidlist = MRIsegIdListNot0(segmri, &nsegids, 0);

  MRI *currlabelvol = MRIalloc(width, height, depth, MRI_INT); 
  int currlabel = 0;

  for (i = 0; i < nsegids; i++)
    {
      currlabel = segidlist[i];
      
      if (currlabel > 0) 
	{
	  // label volume 
	  for (x = 0 ; x < width ; x++)
	    for (y = 0 ; y < height ; y++)
	      for (z = 0 ; z < depth ; z++)
		if (MRIgetVoxVal(segmri,x,y,z,0) == currlabel)
		  MRIsetVoxVal(currlabelvol,x,y,z,0,1);
		else
		  MRIsetVoxVal(currlabelvol,x,y,z,0,0);
	  
	  // choose largest connected components
	  // sprintf(fname, "/tmp/%d.%s", currlabel, "before-largest-conn-component.mgz") ;
	  // MRIwrite(currlabelvol, fname) ;
	  GetLargestCC6(currlabelvol); // Note: how to fill the background???!! -- WM
	  // sprintf(fname, "/tmp/%d.%s", currlabel, "largest-conn-component.mgz") ;
	  // MRIwrite(currlabelvol, fname) ;
	  // remove holes on that component
	  RemoveHoles(currlabelvol);
	  // sprintf(fname, "/tmp/%d.%s", currlabel, "holes-removed.mgz") ;
	  // MRIwrite(currlabelvol, fname) ;
	  
	  //fill ouput volume with new labels
	  for (x = 0 ; x < width ; x++)
	    for (y = 0 ; y < height ; y++)
	      for (z = 0 ; z < depth ; z++)
		{
		  val = MRIgetVoxVal(currlabelvol,x,y,z,0);
		  if (val == 1)
		    MRIsetVoxVal(outmri,x,y,z,0,currlabel);
		}
	}
    }
  
  return(outmri) ;
}
int main(int argc, char *argv[]) {
  char   **av;
  MRI *mri_seg;
  int ac, nargs;

  int x, y, z;
  int target_value = 255;


  Progname = argv[0];

  nargs = handle_version_option (argc, argv, "$Id: mri_extract_largest_CC.c,v 1.6 2011/03/02 00:04:15 nicks Exp $", "$Name: stable5 $");
  argc -= nargs ;
  if (1 == argc)
    exit (0);

  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(1);

  if (!stricmp(hemi, "lh")) {
    target_value = lh_label;
  } else {
    target_value = rh_label;
  }

  mri_seg = MRIread(argv[1]) ;
  if (!mri_seg)
    ErrorExit(ERROR_BADPARM, "%s: could not read label volume %s",
              Progname, argv[1]) ;

  for (z = 0; z < mri_seg->depth; z++)
    for (y = 0; y < mri_seg->height; y++)
      for (x = 0; x < mri_seg->width; x++) {
        if (MRIgetVoxVal(mri_seg, x, y, z, 0) < threshold)
          MRIsetVoxVal(mri_seg, x, y, z, 0, 0);
        else
          MRIsetVoxVal(mri_seg, x, y, z, 0, 255);
      }

  GetLargestCC6(mri_seg);

  for (z = 0; z < mri_seg->depth; z++)
    for (y = 0; y < mri_seg->height; y++)
      for (x = 0; x < mri_seg->width; x++) {
        if (MRIgetVoxVal(mri_seg, x, y, z, 0) > 0)
          MRIsetVoxVal(mri_seg, x, y, z, 0, target_value);
      }

  MRIwrite(mri_seg, argv[2]);

  exit(0);

}  /*  end main()  */