ANIMA  4.0
animaMCMAverageImages.cxx
Go to the documentation of this file.
1 #include <tclap/CmdLine.h>
2 
3 #include <itkVectorImage.h>
5 
6 #include <animaMCMFileReader.h>
7 #include <animaMCMFileWriter.h>
9 
10 int main(int argc, char **argv)
11 {
12  TCLAP::CmdLine cmd("INRIA / IRISA - VisAGeS/Empenn Team", ' ',ANIMA_VERSION);
13 
14  TCLAP::ValueArg<std::string> inFileArg("i","input","list of MCM images (one per line)",true,"","MCM images",cmd);
15  TCLAP::ValueArg<std::string> maskArg("m","maskfiles","Input masks list in text file (mask images should contain only zeros or ones)",false,"","input masks list",cmd);
16  TCLAP::ValueArg<std::string> resArg("o","output", "Average MCM volume",true,"","result MCM volume",cmd);
17  TCLAP::ValueArg<int> outputFascicleArg("n", "nb-of-output-fascicle", "number of output fascicles", true, 0, "number of output fascicles",cmd);
18  TCLAP::ValueArg<unsigned int> nbpArg("p","numberofthreads","Number of threads to run on (default: all cores)",false,itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads(),"number of threads",cmd);
19 
20  try
21  {
22  cmd.parse(argc,argv);
23  }
24  catch (TCLAP::ArgException& e)
25  {
26  std::cerr << "Error: " << e.error() << "for argument " << e.argId() << std::endl;
27  return EXIT_FAILURE;
28  }
29 
31 
32  typedef anima::MCMFileReader <double,3> MCMReaderType;
33  typedef anima::MCMFileWriter <double, 3> MCMWriterType;
34  typedef itk::VectorImage<double, 3> ImageType;
36 
37  // Load MCM images
38  std::ifstream inputFile(inFileArg.getValue().c_str());
39 
40  if (!inputFile.is_open())
41  {
42  std::cerr << "Please provide usable file with input MCMs" << std::endl;
43  return EXIT_FAILURE;
44  }
45 
46  unsigned int nbOfImages = 0;
48 
49  while (!inputFile.eof())
50  {
51  char tmpStr[2048];
52  inputFile.getline(tmpStr,2048);
53 
54  if (strcmp(tmpStr,"") == 0)
55  continue;
56 
57  std::cout << "Loading image " << nbOfImages << " : " << tmpStr << std::endl;
58 
59  MCMReaderType mcmReader;
60  mcmReader.SetFileName(tmpStr);
61  mcmReader.Update();
62 
63  mainFilter->SetInput(nbOfImages,mcmReader.GetModelVectorImage());
64 
65  if ((firstInputModel.IsNull()&&
66  (mcmReader.GetModelVectorImage()->GetDescriptionModel()->GetNumberOfCompartments() > mcmReader.GetModelVectorImage()->GetDescriptionModel()->GetNumberOfIsotropicCompartments())))
67  firstInputModel = mcmReader.GetModelVectorImage()->GetDescriptionModel();
68 
69  nbOfImages++;
70  }
71 
72  std::ifstream masksIn;
73  if (maskArg.getValue() != "")
74  masksIn.open(maskArg.getValue());
75 
76 
77  if (masksIn.is_open())
78  {
79  char tmpStr[2048];
80  while (!masksIn.eof())
81  {
82  masksIn.getline(tmpStr,2048);
83 
84  if (strcmp(tmpStr,"") == 0)
85  continue;
86 
87  mainFilter->AddMaskImage(anima::readImage <MaskImageType> (tmpStr));
88  }
89  }
90 
92  mcmCreator.SetModelWithFreeWaterComponent(false);
93  mcmCreator.SetModelWithStaniszComponent(false);
94  mcmCreator.SetModelWithRestrictedWaterComponent(false);
95  mcmCreator.SetModelWithStationaryWaterComponent(false);
96 
97  for (unsigned int i = 0;i < firstInputModel->GetNumberOfIsotropicCompartments();++i)
98  {
99  switch (firstInputModel->GetCompartment(i)->GetCompartmentType())
100  {
101  case anima::FreeWater:
102  mcmCreator.SetModelWithFreeWaterComponent(true);
103  break;
104 
106  mcmCreator.SetModelWithRestrictedWaterComponent(true);
107  break;
108 
109  case anima::Stanisz:
110  mcmCreator.SetModelWithStaniszComponent(true);
111  break;
112 
114  mcmCreator.SetModelWithStationaryWaterComponent(true);
115  break;
116 
117  default:
118  std::cerr << "Unhandled isotropic compartment" << std::endl;
119  return EXIT_FAILURE;
120  }
121  }
122 
123  mcmCreator.SetCompartmentType(firstInputModel->GetCompartment(firstInputModel->GetNumberOfIsotropicCompartments())->GetCompartmentType());
124  mcmCreator.SetNumberOfCompartments(outputFascicleArg.getValue());
125 
126  anima::MultiCompartmentModel::Pointer outputReferenceModel = mcmCreator.GetNewMultiCompartmentModel();
127 
128  mainFilter->SetReferenceOutputModel(outputReferenceModel);
129  mainFilter->SetNumberOfWorkUnits(nbpArg.getValue());
130  mainFilter->Update();
131 
132  MCMWriterType mcmWriter;
133  mcmWriter.SetInputImage(mainFilter->GetOutput());
134 
135  mcmWriter.SetFileName(resArg.getValue());
136  mcmWriter.Update();
137 
138  return EXIT_SUCCESS;
139 }
int main(int argc, char **argv)
Really this class is some simplified factory that creates the MCM that it knows.