Ejemplo n.º 1
0
MRI *
MRIflattenOverlay(MRI_SURFACE *mris, MRI *mri_overlay, MRI *mri_flat, double res, LABEL *label_overlay,
                  MRI **pmri_vertices)
{
  double   xmin, ymin, xmax, ymax, fdist, lambda[3], xf, yf, val0, val1, val2, val ;
  int      width, height, x, y, fno, ret, z ;
  MHT      *mht ;
  FACE     *face;
  MRI      *mri_vertices ;

  find_biggest_inscribed_rectangle(mris, &xmin, &ymin, &xmax, &ymax) ;
  width = (int)ceil((xmax-xmin)/res) ; width = (int)(floor(width/2.0)*2.0+1) ;   xmax=xmin+width;
  height = (int)ceil((ymax-ymin)/res) ;height = (int)(floor(height/2.0)*2.0+1) ; ymax=ymin+height;

  // 1st frame is correlations and 2nd is vertex #
  mri_vertices = MRIalloc(width, height, 1, MRI_FLOAT) ;
  MRIsetValues(mri_vertices, -1) ;
  mri_flat = MRIalloc(width, height, mri_overlay->nframes, MRI_FLOAT) ;
  mri_vertices->xstart = mri_flat->xstart = xmin ; mri_vertices->xend = mri_flat->xend = xmax ;
  mri_vertices->ystart = mri_flat->ystart = ymin ; mri_vertices->yend = mri_flat->yend = ymax ;
  mri_vertices->zstart = mri_flat->zstart = 0 ; 
  mri_vertices->zend = mri_flat->zend = mri_overlay->nframes-1 ;
  mri_vertices->c_r = mri_flat->c_r = xmin ;  mri_vertices->c_a = mri_flat->c_a = ymin ; 
  mri_vertices->c_s = mri_flat->c_s = 0 ;
  MRIsetResolution(mri_flat, res, res, 1) ;
  MRIsetResolution(mri_vertices, res, res, 1) ;
  if (label_overlay)  // constrain processing to only this label
    LabelRipRestOfSurface(label_overlay, mris) ;
  mht = MHTfillTableAtResolution(mris, NULL, CURRENT_VERTICES, 1.0) ;
  for (x = 0 ; x < width; x++)
    for (y = 0 ; y < height ; y++)
    {
      xf = x*res + xmin ;  yf = y*res + ymin ;   // back to flattened coords
      MHTfindClosestFaceGeneric(mht, mris, xf, yf, 0.0, 10*res, 2, 1, &face, &fno, &fdist) ;
      if (fno >= 0)  // otherwise this point is not in a face
      {
        ret = face_barycentric_coords(mris, fno, CURRENT_VERTICES, xf, yf, 0, &lambda[0], &lambda[1],&lambda[2]); 
        if (ret >= 0)
        {
          if (lambda[0] > lambda[1])
          {
            if (lambda[0] > lambda[2])
            {
              if (face->v[0] == Gdiag_no)
                DiagBreak() ;
              MRIsetVoxVal(mri_vertices, x, y, 0, 0, face->v[0]) ;
            }
            else
            {
              if (face->v[2] == Gdiag_no)
                DiagBreak() ;
              MRIsetVoxVal(mri_vertices, x, y, 0, 0, face->v[2]) ;
            }
          }
          else
          {
            if (lambda[1] > lambda[2])
            {
              if (face->v[1] == Gdiag_no)
                DiagBreak() ;
              MRIsetVoxVal(mri_vertices, x, y, 0, 0, face->v[1]) ;
            }
            else
            {
              if (face->v[2] == Gdiag_no)
                DiagBreak() ;
              MRIsetVoxVal(mri_vertices, x, y, 0, 0, face->v[2]) ;
            }
          }

          for (z = 0 ;z < mri_flat->depth ; z++)
          {
            val0 = MRIgetVoxVal(mri_overlay, face->v[0], 0, 0, z) ;
            val1 = MRIgetVoxVal(mri_overlay, face->v[1], 0, 0, z) ;
            val2 = MRIgetVoxVal(mri_overlay, face->v[2], 0, 0, z) ;
            val = lambda[0]*val0 + lambda[1]*val1 + lambda[2]*val2 ;
            MRIsetVoxVal(mri_flat, x, y, z, 0, val) ;
          }
        }
        else if (fabs(xf) < 10 && fabs(yf) < 10)
        {
          MHTfindClosestFaceGeneric(mht, mris, xf, yf, 0.0, 1000, -1, 1, &face, &fno, &fdist) ;
          printf("(%d, %d) --> %f %f unmapped (goes to face %d, v (%d, %d, %d) if projected\n",
                 x, y, xf, yf, fno, face->v[0], face->v[1], face->v[2]) ;
          DiagBreak() ;
        }
      }
    }

  if (pmri_vertices)
    *pmri_vertices = mri_vertices ;
  MHTfree(&mht) ;
  return(mri_flat) ;
}
Ejemplo n.º 2
0
int main ( int argc, char** argv ) {

  MRI* mri = NULL;
  int zX = 256;
  int zY = 256;
  int zZ = 256;
  int err = NO_ERROR;
  int nX = 0;
  int nY = 0;
  int nZ = 0;
  float sizeX = 1.0;
  float sizeY = 1.0;
  float sizeZ = 1.0;
  int setMethod = SET_METHOD_XYZ;
  int setValue = 0;
  char fnVol[256] = "new_volume.mgz";
  int i;
  char* arg = NULL;

  for ( i = 1; i < argc; i++ ) {

    arg = argv[i];

    if ( argv[i] && *argv[i] != '-' ) {
      printf( "ERROR: Unrecognized argument %s\n", argv[i] );
      PrintUsage( NULL );
      exit( 1 );
    }

    while ( arg[0] == '-' )
      arg = arg+1;

    if ( strlen(arg) <= 0 )
      continue;

    if ( strcmp(arg,"h") == 0 ||
         strcmp(arg,"help") == 0 ) {
      PrintUsage( NULL );
      exit( 0 );
    }

    if ( strcmp(arg,"f") == 0 ||
         strcmp(arg,"filename") == 0 ) {
      if ( i+1 >= argc ) {
        PrintUsage( "No argument to filename option." );
        exit( 1 );
      }
      strcpy( fnVol, argv[i+1] );
      i++;
    }

    if ( strcmp(arg,"x") == 0 ||
         strcmp(arg,"width") == 0 ) {
      if ( i+1 >= argc ) {
        PrintUsage( "No argument to width option." );
        exit( 1 );
      }
      zX = atoi(argv[i+1]);
      i++;
    }
    if ( strcmp(arg,"y") == 0 ||
         strcmp(arg,"height") == 0 ) {
      if ( i+1 >= argc ) {
        PrintUsage( "No argument to height option." );
        exit( 1 );
      }
      zY = atoi(argv[i+1]);
      i++;
    }
    if ( strcmp(arg,"z") == 0 ||
         strcmp(arg,"depth") == 0 ) {
      if ( i+1 >= argc ) {
        PrintUsage( "No argument to depth option." );
        exit( 1 );
      }
      zZ = atoi(argv[i+1]);
      i ++;
    }

    if ( strcmp(arg,"sizex") == 0 ) {
      if ( i+1 >= argc ) {
        PrintUsage( "No argument to sizex option." );
        exit( 1 );
      }
      sizeX = atof(argv[i+1]);
      i++;
    }
    if ( strcmp(arg,"sizey") == 0 ) {
      if ( i+1 >= argc ) {
        PrintUsage( "No argument to sizey option." );
        exit( 1 );
      }
      sizeY = atof(argv[i+1]);
      i++;
    }
    if ( strcmp(arg,"sizez") == 0 ) {
      if ( i+1 >= argc ) {
        PrintUsage( "No argument to sizez optoin." );
        exit( 1 );
      }
      sizeZ = atof(argv[i+1]);
      i++;
    }

    if ( strcmp(arg,"set-method") == 0 ) {
      if ( i+1 >= argc ) {
        PrintUsage( "No argument to set-method option." );
        exit( 1 );
      }
      if ( strcmp( argv[i+1], "xyz" ) == 0 ) {
        setMethod = SET_METHOD_XYZ;
        i++;
        printf( "set_method is xyz\n" );
      } else if ( strcmp( argv[i+1], "random" ) == 0 ) {
        setMethod = SET_METHOD_RANDOM;
        i++;
        printf( "set_method is random\n" );
      } else if ( strncmp( argv[i+1], "constant", 9 ) == 0 ) {
        if ( i+2 >= argc ) {
          PrintUsage( "No value argument to constant method option." );
          exit( 1 );
        }
        setMethod = SET_METHOD_CONSTANT;
        setValue = atoi( argv[i+2] );
        i+=2;
        printf( "set_method is constant, %d\n", setValue );
      } else {
        PrintUsage( "Unrecognized argument to set-method option" );
        exit( 1 );
      }
    }
  }

  printf( "Creating volume %s\n"
          "  width = %d height = %d depth = %d\n"
          "  xsize = %f ysize = %f zsize = %f\n"
          "  set method = %s, constant = %d\n",
          fnVol, zX, zY, zZ,
          sizeX, sizeY, sizeZ,
          sSetMethods[setMethod], setValue );

  mri = MRIalloc( zX, zY, zZ, MRI_UCHAR );
  if ( NULL == mri ) {
    fprintf( stderr, "Couldn't create volume.\n" );
    return 1;
  }
  MRIsetResolution( mri, sizeX, sizeY, sizeZ );

  switch ( setMethod ) {
  case SET_METHOD_CONSTANT:
    MRIvalueFill( mri, 0 );
    break;
  case SET_METHOD_RANDOM:
    for ( nZ = 0; nZ < zZ; nZ++ ) {
      for ( nY = 0; nY < zY; nY++ ) {
        for ( nX = 0; nX < zX; nX++ ) {
          MRIvox( mri, nX, nY, nZ ) = (int)((random()/(float)RAND_MAX)*255.0);
        }
      }
    }
    break;
  case SET_METHOD_XYZ:
    for ( nZ = 0; nZ < zZ; nZ++ ) {
      for ( nY = 0; nY < zY; nY++ ) {
        for ( nX = 0; nX < zX; nX++ ) {
          MRIvox( mri, nX, nY, nZ ) =
            (((float)nZ/(float)zZ)*255.0/3.0) +
            (((float)nY/(float)zY)*255.0/3.0) +
            (((float)nX/(float)zX)*255.0/3.0) ;
        }
      }
    }
    break;
  default:
    for ( nZ = (zZ/2) - (zZ/4); nZ < (zZ/2) + (zZ/4); nZ++ ) {
      for ( nY = (zY/2) - (zY/4); nY < (zY/2) + (zY/4); nY++ ) {
        for ( nX = (zX/2) - (zX/4); nX < (zX/2) + (zX/4); nX++ ) {
          MRIvox( mri, nX, nY, nZ ) = 255;
        }
      }
    }
  }

  err = MRIwrite( mri, fnVol );
  if ( NO_ERROR != err ) {
    fprintf( stderr, "Couldn't write volume.\n" );
    return 1;
  }

  MRIfree( &mri );

  return 0;
}
int
main(int argc, char *argv[]) {
  char   **av, fname[STRLEN] ;
  int    ac, nargs ;
  char   *reg_fname, *in_fname, *out_stem, *cp ;
  int    msec, minutes, seconds, nvox, float2int ;
  struct timeb start ;
  MRI_SURFACE *mris_lh_white, *mris_rh_white, *mris_lh_pial, *mris_rh_pial ;
  MRI         *mri_aseg, *mri_seg, *mri_pial, *mri_tmp, *mri_ribbon, *mri_in, *mri_cortex, 
    *mri_subcort_gm, *mri_wm, *mri_csf ;
  MATRIX      *m_regdat ;
  float       intensity, betplaneres, inplaneres ;

  /* rkt: check for and handle version tag */
  nargs = handle_version_option (argc, argv, "$Id: mri_compute_volume_fractions.c,v 1.9 2012/11/07 18:58:02 greve Exp $", "$Name:  $");
  if (nargs && argc - nargs == 1)
    exit (0);
  argc -= nargs;

  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) ;
  if (!strlen(sdir)) {
    cp = getenv("SUBJECTS_DIR") ;
    if (!cp)
      ErrorExit(ERROR_BADPARM,
                "%s: SUBJECTS_DIR not defined in environment.\n", Progname) ;
    strcpy(sdir, cp) ;
  }
  reg_fname = argv[1] ; in_fname = argv[2] ;
  out_stem = argv[3] ; Progname = argv[0] ;
  ErrorInit(NULL, NULL, NULL) ;
  DiagInit(NULL, NULL, NULL) ;

  TimerStart(&start) ;

  if (stricmp(reg_fname, "identity.nofile") == 0)
  {
    printf("using identity transform\n") ;
    m_regdat = NULL ;
    inplaneres = betplaneres = intensity = 1 ;
    float2int = 0 ;
    if (subject == NULL)
      subject = "unknown" ;
  }
  else
  {
    char *saved_subject = subject ;
    printf("reading registration file %s\n", reg_fname) ;
    regio_read_register(reg_fname, &subject, &inplaneres,
                        &betplaneres, &intensity,  &m_regdat,
                        &float2int);
    
    if (saved_subject)  // specified on cmdline
      subject = saved_subject ;
    m_regdat = regio_read_registermat(reg_fname) ;
    if (m_regdat == NULL)
      ErrorExit(ERROR_NOFILE, "%s: could not load registration file from %s", Progname,reg_fname) ;
  }
  printf("Format is %s\n",fmt);
    
  sprintf(fname, "%s/%s/surf/lh.white", sdir, subject) ;
  printf("reading surface %s\n", fname) ;
  mris_lh_white = MRISread(fname) ;
  if (mris_lh_white == NULL)
    ErrorExit(ERROR_NOFILE, "%s: could not load lh white surface from %s", Progname,fname) ;

  sprintf(fname, "%s/%s/surf/rh.white", sdir, subject) ;
  printf("reading surface %s\n", fname) ;
  mris_rh_white = MRISread(fname) ;
  if (mris_rh_white == NULL)
    ErrorExit(ERROR_NOFILE, "%s: could not load rh white surface from %s", Progname,fname) ;

  sprintf(fname, "%s/%s/surf/lh.pial", sdir, subject) ;
  printf("reading surface %s\n", fname) ;
  mris_lh_pial = MRISread(fname) ;
  if (mris_lh_pial == NULL)
    ErrorExit(ERROR_NOFILE, "%s: could not load lh pial surface from %s", Progname,fname) ;

  sprintf(fname, "%s/%s/surf/rh.pial", sdir, subject) ;
  printf("reading surface %s\n", fname) ;
  mris_rh_pial = MRISread(fname) ;
  if (mris_rh_pial == NULL)
    ErrorExit(ERROR_NOFILE, "%s: could not load rh pial surface from %s", Progname,fname) ;

  sprintf(fname, "%s/%s/mri/aseg.mgz", sdir, subject) ;
  printf("reading volume %s\n", fname) ;
  mri_aseg = MRIread(fname) ;
  if (mri_aseg == NULL)
    ErrorExit(ERROR_NOFILE, "%s: could not load aseg volume from %s", Progname,fname) ;

  printf("reading movable volume %s\n", in_fname) ;
  mri_in = MRIread(in_fname) ;
  if (mri_in == NULL)
    ErrorExit(ERROR_NOFILE, "%s: could not load input volume from %s", Progname,in_fname) ;
 
  nvox = (int)ceil(256/resolution); 
  mri_pial = MRIalloc(nvox, nvox, nvox, MRI_UCHAR) ;
  MRIsetResolution(mri_pial, resolution, resolution, resolution) ;

  mri_pial->xstart = -resolution*mri_pial->width/2.0 ;
  mri_pial->xend = resolution*mri_pial->width/2.0 ;
  mri_pial->ystart = -resolution*mri_pial->height/2.0 ;
  mri_pial->yend = resolution*mri_pial->height/2.0 ;
  mri_pial->zstart = -resolution*mri_pial->depth/2.0 ;
  mri_pial->zend = resolution*mri_pial->depth/2 ;
  mri_pial->c_r = mri_aseg->c_r ; mri_pial->c_a = mri_aseg->c_a ; mri_pial->c_s = mri_aseg->c_s ;
  MRIreInitCache(mri_pial) ; 

  printf("filling interior of lh pial surface...\n") ;
  MRISfillInterior(mris_lh_pial, resolution, mri_pial) ;
  mri_seg = MRIclone(mri_pial, NULL) ;
  mri_tmp = MRIclone(mri_pial, NULL) ;
  printf("filling interior of rh pial surface...\n") ;
  MRISfillInterior(mris_rh_pial, resolution, mri_tmp) ;
  MRIcopyLabel(mri_tmp, mri_pial, 1) ;
  MRIclear(mri_tmp) ;
  printf("filling interior of lh white matter surface...\n") ;
  MRISfillWhiteMatterInterior(mris_lh_white, mri_aseg, mri_seg, resolution,
                              WM_VAL, SUBCORT_GM_VAL, CSF_VAL);
  printf("filling interior of rh white matter surface...\n") ;
  MRISfillWhiteMatterInterior(mris_rh_white, mri_aseg, mri_tmp, resolution,
                              WM_VAL, SUBCORT_GM_VAL, CSF_VAL);
  MRIcopyLabel(mri_tmp, mri_seg, WM_VAL) ;
  MRIcopyLabel(mri_tmp, mri_seg, SUBCORT_GM_VAL) ;
  MRIcopyLabel(mri_tmp, mri_seg, CSF_VAL) ;
  MRIfree(&mri_tmp) ;
  
  mri_ribbon = MRInot(mri_seg, NULL) ;
  MRIcopyLabel(mri_seg, mri_pial, CSF_VAL) ;
  MRIreplaceValuesOnly(mri_pial, mri_pial, CSF_VAL, 0) ;
  MRIand(mri_ribbon, mri_pial, mri_ribbon, 1) ;
  MRIbinarize(mri_ribbon, mri_ribbon, 1, 0, GM_VAL) ;
  MRIcopyLabel(mri_ribbon, mri_seg, GM_VAL) ;
  MRIreplaceValuesOnly(mri_seg, mri_seg, CSF_VAL, 0) ;
  add_aseg_structures_outside_ribbon(mri_seg, mri_aseg, mri_seg, WM_VAL, SUBCORT_GM_VAL, CSF_VAL) ;


  {
    MATRIX *m_conformed_to_epi_vox2vox, *m_seg_to_conformed_vox2vox,
           *m_seg_to_epi_vox2vox ;

    if (m_regdat == NULL)    // assume identity transform
      m_seg_to_epi_vox2vox = MRIgetVoxelToVoxelXform(mri_seg, mri_in) ;
    else
    {
      m_conformed_to_epi_vox2vox = MRIvoxToVoxFromTkRegMtx(mri_in, mri_aseg, m_regdat);
      m_seg_to_conformed_vox2vox = MRIgetVoxelToVoxelXform(mri_seg, mri_aseg) ;
      
      m_seg_to_epi_vox2vox = MatrixMultiply(m_conformed_to_epi_vox2vox, m_seg_to_conformed_vox2vox, NULL) ;
      MatrixFree(&m_regdat) ; MatrixFree(&m_conformed_to_epi_vox2vox) ; 
      MatrixFree(&m_seg_to_conformed_vox2vox);

    }
    printf("seg to EPI vox2vox matrix:\n") ;
    MatrixPrint(Gstdout, m_seg_to_epi_vox2vox) ;
    mri_cortex = MRIalloc(mri_in->width, mri_in->height, mri_in->depth, MRI_FLOAT) ;
    MRIcopyHeader(mri_in, mri_cortex) ;
    mri_subcort_gm = MRIclone(mri_cortex, NULL) ;
    mri_wm = MRIclone(mri_cortex, NULL) ;
    mri_csf = MRIclone(mri_cortex, NULL) ;
    printf("computing partial volume fractions...\n") ;
    MRIcomputePartialVolumeFractions(mri_in, m_seg_to_epi_vox2vox, mri_seg, mri_wm, mri_subcort_gm, mri_cortex, mri_csf,
                                     WM_VAL, SUBCORT_GM_VAL, GM_VAL, 0) ;
  }
  
  sprintf(fname, "%s.wm.%s", out_stem,fmt) ;
  printf("writing wm %% to %s\n", fname) ;
  MRIwrite(mri_wm, fname) ;

  sprintf(fname, "%s.subcort_gm.%s", out_stem,fmt) ;
  printf("writing subcortical gm %% to %s\n", fname) ;
  MRIwrite(mri_subcort_gm, fname) ;

  sprintf(fname, "%s.cortex.%s", out_stem, fmt) ;
  printf("writing cortical gm %% to %s\n", fname) ;
  MRIwrite(mri_cortex, fname) ;
  
  sprintf(fname, "%s.csf.%s", out_stem,fmt) ;
  printf("writing csf %% to %s\n", fname) ;
  MRIwrite(mri_csf, fname) ;

  msec = TimerStop(&start) ;
  seconds = nint((float)msec/1000.0f) ;
  minutes = seconds / 60 ;
  seconds = seconds % 60 ;
  printf("volume fraction calculation took %d minutes"
          " and %d seconds.\n", minutes, seconds) ;
  exit(0) ;
  return(0) ;
}