Example #1
0
void
IoParams::parse(int ac,
                char* av[])
{
  namespace po = boost::program_options;
  typedef std::vector<std::string> StringVectorType;
  StringVectorType vItems;
  po::options_description desc("Allowed Options");

  desc.add_options()
  ("help", " produce help message")
  ("out", po::value(&strOut), " output transform file (.tm3d)")
  ("template", po::value(&strTemplate), " template volume (only for geometry) - if a gcam is present, it needs to be specified")
  ("subject", po::value(&strSubject), " subject volume (only for geometry)" )
  ("in", po::value(&vItems)->multitoken(), " input transforms - affine|volume|morph|mesh|gcam + fname")
  ("dbg", po::value(&vDbgCoords)->multitoken(), " enter debug coordinates ")
  ;

  po::variables_map vm;
  po::store( po::parse_command_line(ac,av,desc), vm);
  po::notify(vm);

  if ( vm.count("help") )
  {
    std::cout << desc << std::endl;
    exit(0);
  }

  if ( strOut.empty() )
  {
    std::cerr << " Empty output file name\n";
    exit(1);
  }

  // parse input terms
  if ( vItems.empty() )
  {
    std::cout << " nothing to do - no transforms specified\n";
    exit(0);
  }

  if (  vItems.size() % 2 )
  {
    std::cerr << " odd number of transform tokens\n";
    exit(1);
  }

  for ( StringVectorType::iterator it = vItems.begin();
        it != vItems.end(); )
  {
    MorphItem item;
    item.type = *it++;
    item.file = *it++;
    this->items.push_back(item);
  } // next it
}
void buildImageIntensityModelOnROI(const char* referenceFilename, const char* maskFilename, const char* dir, const char* outputImageFilename) {


	typedef itk::PCAModelBuilder<RepresenterType> ModelBuilderType;
	typedef itk::StatisticalModel<RepresenterType> StatisticalModelType;
    typedef std::vector<std::string> StringVectorType;
    typedef itk::DataManager<RepresenterType> DataManagerType;

    RepresenterType::Pointer representer = RepresenterType::New();

	typedef itk::ImageFileReader< ImageType > MaskReaderType;
	MaskReaderType::Pointer maskReader = MaskReaderType::New();
	maskReader->SetFileName( maskFilename );
    maskReader->Update();

	representer->SetReference( ReadImageFromFile(referenceFilename), maskReader->GetOutput() );

    StringVectorType filenames;
    getdir(dir, filenames, ".vtk");

    DataManagerType::Pointer dataManager = DataManagerType::New();
	dataManager->SetRepresenter(representer);

    for (StringVectorType::const_iterator it = filenames.begin(); it != filenames.end(); it++) {
        std::string fullpath = (std::string(dir) + "/") + *it;

        dataManager->AddDataset( ReadImageFromFile(fullpath), fullpath.c_str());
    }

	ModelBuilderType::Pointer pcaModelBuilder = ModelBuilderType::New();
    StatisticalModelType::Pointer model = pcaModelBuilder->BuildNewModel(dataManager->GetSampleDataStructure(), 0);

    std::cout<<"dimensionality of the data: "<<model->GetDomain().GetNumberOfPoints()<<", dimension of the images: "<<(*dataManager->GetSampleDataStructure().begin())->GetSample()->GetLargestPossibleRegion().GetNumberOfPixels()<<std::endl;

    std::cout<<"writing the mean sample to a png file..."<<std::endl;
    
	typedef itk::ImageFileWriter< ImageType > ImageWriterType;
	ImageWriterType::Pointer writer = ImageWriterType::New();
	writer->SetFileName( outputImageFilename );
	writer->SetInput(model->DrawSample());
    writer->Update();

}
//
// Build a new shape model from vtkPolyData, given in datadir.
//
int main(int argc, char** argv) {

    if (argc < 3) {
        std::cout << "Usage " << argv[0] << " datadir modelname" << std::endl;
        exit(-1);
    }
    std::string datadir(argv[1]);
    std::string modelname(argv[2]);


    // All the statismo classes have to be parameterized with the RepresenterType.
    // For building a shape model with vtk, we use the vtkPolyDataRepresenter.
    typedef vtkStandardMeshRepresenter RepresenterType;
    typedef DataManager<vtkPolyData> DataManagerType;
    typedef PCAModelBuilder<vtkPolyData> ModelBuilderType;
    typedef StatisticalModel<vtkPolyData> StatisticalModelType;

    typedef std::vector<std::string> StringVectorType;
    StringVectorType filenames;
    getdir(datadir, filenames, ".vtk");
    if (filenames.size() == 0) {
        std::cerr << "did not find any vtk files in directory " << datadir << " exiting.";
        exit(-1);
    }
    try {

        // We create a new representer object. For the vtkPolyDataRepresenter, we have to set a reference
        // and the alignmentType. The alignmenttype (which is here RIGID) determines how the dataset that we
        // will use will later be aligned to the reference.

        vtkPolyData* reference = loadVTKPolyData(datadir + "/" + filenames[0]);
        boost::scoped_ptr<RepresenterType> representer(RepresenterType::Create(reference));

        // We create a datamanager and provide it with a pointer  to the representer
        boost::scoped_ptr<DataManagerType> dataManager(DataManagerType::Create(representer.get()));


        // Now we add our data to the data manager
        // load the data and add it to the data manager. We take the first 17 hand shapes that we find in the data folder
        for (unsigned i = 0; i < filenames.size() ; i++) {
            vtkPolyData* dataset = loadVTKPolyData(datadir + "/" + filenames[i]);

            // We provde the filename as a second argument.
            // It will be written as metadata, and allows us to more easily figure out what we did later.
            dataManager->AddDataset(dataset, filenames[i]);

            // it is save to delete the dataset after it was added, as the datamanager direclty copies it.
            dataset->Delete();
        }

        // To actually build a model, we need to create a model builder object.
        // Calling the build model with a list of samples from the data manager, returns a new model.
        // The second parameter to BuildNewModel is the variance of the noise on our data
        boost::scoped_ptr<ModelBuilderType> modelBuilder(ModelBuilderType::Create());

        boost::scoped_ptr<StatisticalModelType> model(modelBuilder->BuildNewModel(dataManager->GetData(), 0.01));

        // Once we have built the model, we can save it to disk.
        model->Save(modelname);
        std::cout << "Successfully saved shape model as " << modelname << std::endl;

        reference->Delete();
    } catch (StatisticalModelException& e) {
        std::cout << "Exception occured while building the shape model" << std::endl;
        std::cout << e.what() << std::endl;
    }
}