int main(int argc, char* argv[]) { IoParams params; try { params.parse(argc,argv); } catch (const std::exception& excp) { std::cerr << " Exception while parsing cmd-line\n" << excp.what() << std::endl; exit(1); } catch (...) { std::cerr << " unhandled exception caught while parsing cmd line\n"; exit(1); } boost::shared_ptr<gmp::VolumeMorph> pmorph(new gmp::VolumeMorph); if ( !params.strTemplate.empty() ) { MRI* mri = MRIread( const_cast<char*>(params.strTemplate.c_str()) ); if (!mri) std::cerr << " Failed to open template mri " << params.strTemplate << std::endl; else pmorph->set_volGeom_fixed(mri); MRIfree(&mri); } if ( !params.strSubject.empty() ) { MRI* mri = MRIread( const_cast<char*>(params.strSubject.c_str()) ); if (!mri) std::cerr << " Failed to open subject mri " << params.strSubject << std::endl; else pmorph->set_volGeom_moving(mri); MRIfree(&mri); } IoParams::MorphContainerType::iterator it; for ( it = params.items.begin(); it != params.items.end(); ++it ) { if ( it->type == "affine" ) { boost::shared_ptr<gmp::AffineTransform3d> paffine( new gmp::AffineTransform3d); float* pf = read_transform( it->file.c_str() ); if ( !pf ) { std::cerr << " failed to read transform\n"; exit(1); } paffine->set_pars( pf); pmorph->m_transforms.push_back( paffine ); } else if ( it->type == "volume" ) { boost::shared_ptr<gmp::DeltaTransform3d> pvol( new gmp::DeltaTransform3d); MRI* field = MRIread ( const_cast<char*>( it->file.c_str() ) ); if ( !field ) { std::cerr << " failed to read field " << std::endl; exit(1); } pvol->set_field(field); pmorph->m_transforms.push_back( pvol ); } else if ( it->type == "gcam" ) { boost::shared_ptr<gmp::DeltaTransform3d> pvol( new gmp::DeltaTransform3d); GCA_MORPH* gcam = GCAMread( const_cast<char*>( it->file.c_str() ) ); if ( !gcam ) { std::cerr << " failed to read GCAM " << std::endl; exit(1); } // create bogus MRI to hold the geometry MRI* mri_template = MRIread( const_cast<char*> ( params.strTemplate.c_str() ) ); MRI* mriBuf = MRIalloc( gcam->image.width, gcam->image.height, gcam->image.depth, MRI_UCHAR); useVolGeomToMRI( &gcam->image, mriBuf ); GCAMrasToVox(gcam, mriBuf ); MRIfree( &mriBuf ); std::cout << " atlas geometry = " << gcam->atlas.width << " , " << gcam->atlas.height << " , " << gcam->atlas.depth << std::endl << " image geometry = " << gcam->image.width << " , " << gcam->image.height << " , " << gcam->image.depth << std::endl; MRI* mri = MRIallocSequence( mri_template->width, mri_template->height, mri_template->depth, MRI_FLOAT, 3); MRIcopyHeader(mri_template, mri); g_vDbgCoords = params.vDbgCoords; try { //MRI* mask = CopyGcamToDeltaField(gcam, mri); pvol->set_field(mri); //pvol->set_mask(mask); pmorph->m_transforms.push_back( pvol ); } catch (const std::string& e) { std::cerr << " Exception caught while processing GCAM node\n" << e << std::endl; exit(1); } MRIfree(&mri_template); } else if ( it->type == "morph" ) { boost::shared_ptr<gmp::VolumeMorph> tmpMorph(new gmp::VolumeMorph); try { tmpMorph->load( it->file.c_str() ); } catch (const char* msg) { std::cerr << " Exception caught while loading morph in file " << it->file << std::endl; exit(1); } for ( gmp::VolumeMorph::TransformContainerType::iterator transformIter = tmpMorph->m_transforms.begin(); transformIter != tmpMorph->m_transforms.end(); ++transformIter ) pmorph->m_transforms.push_back( *transformIter ); } else if ( it->type == "mesh" ) { boost::shared_ptr<gmp::FemTransform3d> pfem(new gmp::FemTransform3d); boost::shared_ptr<CMesh3d> pmesh(new CMesh3d); pmesh->load( it->file.c_str() ); pfem->m_sharedMesh = pmesh; pmorph->m_transforms.push_back(pfem); } else { std::cerr << " unhandled transform type " << it->type << std::endl; } } // next it // finally write morph file try { pmorph->save( params.strOut.c_str() ); } catch (const char* msg) { std::cerr << " Exception caught while saving morph\n" << msg << std::endl; exit(1); } return 0; }
int main(int ac, char* av[]) { SimpleTimer timer; IoParams params; try { params.parse(ac,av); } catch (const std::exception& e) { std::cerr << " Exception caught while parsing cmd-line: " << e.what() << "\n Type --help for command info! " << std::endl; exit(1); } std::cout << " using bounding box " << (params.useBoundingBox?"yes":"no") << std::endl; // load fixed MRI* mriFixed = MRIread( getChar( params.strFixed ) ); if ( !mriFixed ) { std::cerr << " failed reading fixed volumes\n"; exit(1); } // load moving MRI* mriMoving = MRIread( getChar( params.strMoving ) ); if ( !mriMoving ) { std::cerr << " failed reading moving volume\n"; exit(1); } // load morph boost::shared_ptr<gmp::VolumeMorph> pmorph(new gmp::VolumeMorph); try { pmorph->load( params.strMorph.c_str(), params.zlibBuffer ); } catch (const char* msg) { std::cerr << " Failed loading morph: \n" << msg << std::endl; exit(1); } pmorph->m_template = mriFixed; initOctree2(*pmorph); // set the interpolation if ( params.strInterp.empty() ) pmorph->m_interpolationType = SAMPLE_TRILINEAR; else if (strcmp(params.strInterp.c_str(), "linear")==0) pmorph->m_interpolationType = SAMPLE_TRILINEAR; else if (strcmp(params.strInterp.c_str(), "nearest")==0) pmorph->m_interpolationType = SAMPLE_NEAREST; else pmorph->m_interpolationType = SAMPLE_TRILINEAR; if (params.doTest) { std::cout << " Writing out some tests.\n"; SimpleTimer t1; VOL_GEOM vgLike; initVolGeom(&vgLike); getVolGeom(pmorph->m_template, &vgLike); MRI* mriOut = pmorph->apply_transforms(mriMoving, true, &vgLike); std::cout << " morph completed in " << t1.elapsed_min() << " minutes\n"; MRIwrite(mriOut, "tmpout1.mgz"); MRIfree(&mriOut); } // produce the GCAM GCA_MORPH* gcam = pmorph->exportGcam(mriMoving, params.useBoundingBox, params.threshold); GCAMwrite( gcam, const_cast<char*> (params.strGcam.c_str())); // test the presence of the gc structures -- LZ: WHAT DOES THAT DO??? GCAMnormalizeIntensities(gcam, mriFixed); if ( params.doTest && (!params.useBoundingBox || 1) ) { std::cout << " applying morph\n" << " width = " << gcam->width << std::endl; MRI* mriOut = GCAMmorphToAtlas(mriMoving, gcam, mriFixed, 0, pmorph->m_interpolationType); std::cout << " AFTER MORPH\n"; std::cout << " out MRI params = " << " width = " << mriOut->width << " height = " << mriOut->height << " depth = " << mriOut->depth << " nframes = " << mriOut->nframes << std::endl; MRIwrite(mriOut, "tmpout2.mgz"); } else { std::cout << " skipping tmpout2.mgz - using bounding box\n"; } std::cout << " Export performed in " << timer.elapsed_min() << " minutes \n"; return 0; }
int main(int argc, char*argv[]) { IoParams params; try { params.parse(argc,argv); if ( params.scanner == "siemens" && ( params.gefile != "" || params.magfile == "" || params.phasefile == "" ) ) throw std::logic_error("Siemens scanner should only have siemens mag and siemens phase options. See help"); if ( params.scanner == "ge" && ( params.gefile == "" || params.magfile != "" || params.phasefile != "" ) ) throw std::logic_error("GE scanner should only have a ge-file option in the command line. See help"); } catch (std::exception& excp) { std::cerr << "ERROR: Exception caught while parsing the command-line\n" << excp.what() << std::endl; exit(1); } std::cout << "Reading the input file(s)\n"; MRI *mrimag=NULL, *mriphase=NULL; if ( params.scanner == "siemens" ) { mrimag = DICOMRead2(params.magfile.c_str(), TRUE ); if ( mrimag == NULL ) { std::cerr << "ERROR: The Siemens magnitude DICOM can't be read. Check the file\n"; exit(1); } MRI* _sphase = DICOMRead2(params.phasefile.c_str(), TRUE ); if ( _sphase == NULL ) { std::cerr << "ERROR: The Siemens phase DICOM can't be read. Check the file\n"; exit(1); } //mriphase = MRIcloneBySpace(_sphase, MRI_FLOAT, 1); /*float vmin, vmax; MRIvalRange(_sphase, &vmin, &vmax); printf("%f, %f\n", vmin, vmax);*/ mriphase = MRIchangeType(_sphase, MRI_FLOAT, -M_PI, M_PI, 0); MRIvalScale(mriphase, mriphase, -M_PI, M_PI); MRIfree(&_sphase); } else if ( params.scanner == "ge" ) { MRI *_gedcm = NULL; _gedcm = DICOMRead2(params.gefile.c_str(), TRUE); if ( _gedcm == NULL ) { std::cerr << "ERROR: The GE DICOM can't be read. Check the file\n"; exit(1); } mrimag = MRIcloneBySpace(_gedcm, MRI_FLOAT, 1); mriphase = MRIcloneBySpace(_gedcm, MRI_FLOAT, 1); float _r, _i; for ( int i=0; i < _gedcm->width; i++) for ( int j=0; j < _gedcm->height; j++) for ( int k=0; k < _gedcm->depth; k++) { _r = (float)(MRIgetVoxVal(_gedcm, i, j, k, 1)) ; _i = (float)(MRIgetVoxVal(_gedcm, i, j, k, 2)) ; MRIsetVoxVal(mrimag, i, j, k, 0, (float)MRIgetVoxVal(_gedcm, i, j, k, 0)); //MRIsetVoxVal(mrimag, i, j, k, 0, sqrt((_i/1000.0)*(_i/1000.0) + (_r/1000.0)*(_r/1000.0))); MRIsetVoxVal(mriphase, i, j, k, 0, atan2(_i, _r) ); } MRIfree(&_gedcm); } else if ( params.scanner == "philips" ) { MRI *_phildcm = NULL; _phildcm = DICOMRead2(params.philipsfile.c_str(), TRUE); if ( _phildcm == NULL ) { std::cerr << "ERROR: The Philips DICOM can't be read. Check the file\n"; exit(1); } mrimag = MRIcloneBySpace(_phildcm, MRI_FLOAT, 1); MRI *_pphase = MRIcloneBySpace(_phildcm, MRI_FLOAT, 1); for ( int i=0; i < _phildcm->width; i++) for ( int j=0; j < _phildcm->height; j++) for ( int k=0; k < _phildcm->depth; k++) { MRIsetVoxVal(mrimag, i, j, k, 0, (float)MRIgetVoxVal(_phildcm, i, j, k, 0)); MRIsetVoxVal(_pphase, i, j, k, 0, (float)MRIgetVoxVal(_phildcm, i, j, k, 1)); } mriphase = MRIchangeType(_pphase, MRI_FLOAT, -M_PI, M_PI, 0); MRIvalScale(mriphase, mriphase, -M_PI, M_PI); MRIfree(&_phildcm); MRIfree(&_pphase); } else { std::cerr << "ERROR: You didn't specify the scanner make ( one of ge, siemens and philips )\n"; exit(1); } std::cout << "Writing out the Magnitude and Phase files\n"; MRIwrite(mrimag, params.outmag.c_str()); MRIwrite(mriphase, params.outphase.c_str()); MRIfree(&mrimag); MRIfree(&mriphase); }
int main(int ac, char* av[]) { // first, handle stuff for --version and --all-info args int nargs = 0; nargs = handle_version_option ( ac, av, "$Id: mris_volmask.cpp,v 1.26 2014/11/06 03:40:22 nicks Exp $", "$Name: $" ); if (nargs && ac - nargs == 1) { exit (0); } ac -= nargs; // parse command-line IoParams params; try { params.parse(ac,av); } catch (std::exception& excp) { std::cerr << " Exception caught while parsing the command-line\n" << excp.what() << std::endl; exit(1); } // process input files // will also resolve the paths depending on the mode of the application // (namely if the subject option has been specified or not) MRI* mriTemplate; MRIS* surfLeftWhite = NULL; MRIS* surfLeftPial = NULL; MRIS* surfRightPial = NULL; MRIS* surfRightWhite = NULL; std::string outputPath; try { outputPath = LoadInputFiles(params, mriTemplate, surfLeftWhite, surfLeftPial, surfRightWhite, surfRightPial); } catch (std::exception& e) { std::cerr << " Exception caught while processing input files \n" << e.what() << std::endl; exit(1); } if (params.bEditAseg) { std::string subjDir = params.subjectsDir / params.subject; std::string pathSurf(subjDir / "surf"), pathMriOutput = outputPath / "aseg.ribbon.mgz"; printf("inserting LH into aseg...\n") ; insert_ribbon_into_aseg(mriTemplate, mriTemplate, surfLeftWhite, surfLeftPial, LEFT_HEMISPHERE) ; printf("inserting RH into aseg...\n") ; insert_ribbon_into_aseg(mriTemplate, mriTemplate, surfRightWhite, surfRightPial, RIGHT_HEMISPHERE); printf("writing output to %s\n", (const_cast<char*>( pathMriOutput.c_str() ))); MRIwrite(mriTemplate, (const_cast<char*>( pathMriOutput.c_str() ))) ; exit(0) ; } /* Process LEFT hemisphere */ //--------------------- // proces white surface - convert to voxel-space // // allocate distance MRI* dLeftWhite = MRIalloc( mriTemplate->width, mriTemplate->height, mriTemplate->depth, MRI_FLOAT ); MRIcopyHeader(mriTemplate, dLeftWhite); // Computes the signed distance to given surface. Sign indicates // whether it is on the inside or outside. params.capValue - // saturation/clip value for distance. std::cout << "computing distance to left white surface \n" ; ComputeSurfaceDistanceFunction(surfLeftWhite, dLeftWhite, params.capValue); // if the option is there, output distance if ( params.bSaveDistance ) MRIwrite ( dLeftWhite, const_cast<char*>( (outputPath / "lh.dwhite." + params.outRoot + ".mgz").c_str() ) ); //----------------------- // process pial surface MRI* dLeftPial = MRIalloc( mriTemplate->width, mriTemplate->height, mriTemplate->depth, MRI_FLOAT); MRIcopyHeader(mriTemplate,dLeftPial); std::cout << "computing distance to left pial surface \n" ; ComputeSurfaceDistanceFunction(surfLeftPial, dLeftPial, params.capValue); if ( params.bSaveDistance ) MRIwrite ( dLeftPial, const_cast<char*>( (outputPath / "lh.dpial." + params.outRoot + ".mgz").c_str() ) ); // combine them and create a mask for the left hemi. Must be // outside of white and inside pial. Creates labels for WM and Ribbon. MRI* maskLeftHemi = CreateHemiMask(dLeftPial,dLeftWhite, params.labelLeftWhite, params.labelLeftRibbon, params.labelBackground); // no need for the hemi distances anymore MRIfree(&dLeftWhite); MRIfree(&dLeftPial); /* Process RIGHT hemi */ //------------------- // process white MRI* dRightWhite = MRIalloc( mriTemplate->width, mriTemplate->height, mriTemplate->depth, MRI_FLOAT); MRIcopyHeader(mriTemplate, dRightWhite); std::cout << "computing distance to right white surface \n" ; ComputeSurfaceDistanceFunction( surfRightWhite, dRightWhite, params.capValue); if ( params.bSaveDistance ) MRIwrite ( dRightWhite, const_cast<char*>( (outputPath / "rh.dwhite." + params.outRoot + ".mgz").c_str() ) ); //-------------------- // process pial MRI* dRightPial = MRIalloc( mriTemplate->width, mriTemplate->height, mriTemplate->depth, MRI_FLOAT); MRIcopyHeader(mriTemplate, dRightPial); std::cout << "computing distance to right pial surface \n" ; ComputeSurfaceDistanceFunction(surfRightPial, dRightPial, params.capValue); if ( params.bSaveDistance ) MRIwrite ( dRightPial, const_cast<char*>( (outputPath / "rh.dpial." + params.outRoot + ".mgz").c_str() ) ); // compute hemi mask MRI* maskRightHemi = CreateHemiMask(dRightPial, dRightWhite, params.labelRightWhite, params.labelRightRibbon, params.labelBackground); // no need for the hemi distances anymore MRIfree(&dRightWhite); MRIfree(&dRightPial); /* finally combine the two created masks -- need to resolve overlap */ MRI* finalMask = CombineMasks(maskLeftHemi, maskRightHemi, params.labelBackground); MRIcopyHeader( mriTemplate, finalMask); // write final mask std::cout << "writing volume " << const_cast<char*>( (outputPath / (params.outRoot + ".mgz")).c_str() ) << endl; MRIwrite( finalMask, const_cast<char*>( (outputPath / (params.outRoot + ".mgz")).c_str() ) ); // sanity-check: make sure location 0,0,0 is background (not brain) if ( MRIgetVoxVal(finalMask,0,0,0,0) != 0 ) { cerr << "ERROR: ribbon has non-zero value at location 0,0,0" << endl; exit(1); } /* if present, also write the ribbon masks by themselves */ if ( params.bSaveRibbon ) { std::cout << " writing ribbon files\n"; // filter the mask of the left hemi MRI* ribbon = FilterLabel(maskLeftHemi, params.labelLeftRibbon); MRIcopyHeader( mriTemplate, ribbon); MRIwrite( ribbon, const_cast<char*>( (outputPath / "lh." + params.outRoot + ".mgz").c_str() ) ); // sanity-check: make sure location 0,0,0 is background (not brain) if ( MRIgetVoxVal(ribbon,0,0,0,0) != 0 ) { cerr << "ERROR: lh ribbon has non-zero value at location 0,0,0" << endl; exit(1); } MRIfree(&ribbon); ribbon = FilterLabel(maskRightHemi, params.labelRightRibbon); MRIcopyHeader( mriTemplate, ribbon); MRIwrite( ribbon, const_cast<char*>( (outputPath / "rh." + params.outRoot + ".mgz").c_str() ) ); // sanity-check: make sure location 0,0,0 is background (not brain) if ( MRIgetVoxVal(ribbon,0,0,0,0) != 0 ) { cerr << "ERROR: rh ribbon has non-zero value at location 0,0,0" << endl; exit(1); } MRIfree(&ribbon); } return 0; }