ANIMA  4.0
animaBuildSamplesFromVolumes.cxx
Go to the documentation of this file.
1 #include <iostream>
2 #include <tclap/CmdLine.h>
3 
4 #include <itkTimeProbe.h>
5 #include <itkImageConstIterator.h>
6 #include <itkImageRegionConstIterator.h>
7 #include <itkImageRegionConstIteratorWithIndex.h>
9 
10 int main(int argc, char **argv)
11 {
12  TCLAP::CmdLine cmd("Given a mask creates and a list of volume data, populates a csv file where each colmuns represents one of the volume data and each line a given voxel. The voxel for which the mask is 0 are not set into the csv file. "
13  "\\ Note: the voxel location are lost into the csv file. INRIA / IRISA - VisAGeS/Empenn Team", ' ',ANIMA_VERSION);
14  TCLAP::ValueArg<std::string> dataListArg("i","database","Image List",true,"","image list",cmd);
15  TCLAP::ValueArg<std::string> maskArg("m","maskname","Mask",true,"","mask",cmd);
16  TCLAP::ValueArg<std::string> resNameArg("o","outputname","CSV file name",true,"","file name for the output csv file",cmd);
17  try
18  {
19  cmd.parse(argc,argv);
20  }
21  catch (TCLAP::ArgException& e)
22  {
23  std::cerr << "Error: " << e.error() << "for argument " << e.argId() << std::endl;
24  return(1);
25  }
26 
27  // load mask file
28  typedef itk::Image <unsigned char, 3> MaskType;
29  MaskType::Pointer maskImage = anima::readImage <MaskType> (maskArg.getValue());
30  typedef itk::ImageRegionConstIteratorWithIndex<MaskType> MaskIterator;
31 
32  MaskIterator iterMaskImage(maskImage,maskImage->GetRequestedRegion());
33 
34  typedef itk::Image <double, 3> ImageType;
35  typedef itk::ImageRegionConstIterator<ImageType> ImageIterator;
36 
37  // load filenames file
38  std::ifstream fileIn(dataListArg.getValue());
39 
40  std::vector< std::vector<double> > allSamples;
41  std::string oneLine;
42 
43  unsigned int nbImg=0;
44  std::vector<std::string> labels;
45  std::vector<unsigned int> xSample;
46  std::vector<unsigned int> ySample;
47  std::vector<unsigned int> zSample;
48 
49  while (std::getline(fileIn, oneLine))
50  {
51  std::size_t pos = oneLine.find(":"); // position of ":" in str
52  std::string imgeName = oneLine.substr (pos+1); // get from ":" to the end
53  labels.push_back(oneLine.substr(0,pos));
54 
55  itk::SmartPointer<ImageType> ltReader;
56  try
57  {
58  ltReader=anima::readImage<ImageType>(imgeName);
59  }
60  catch(std::exception& e)
61  {
62  std::cout<<"file for field "<<labels.back()<<" is missing"<<std::endl;
63  labels.pop_back();
64  continue;
65  }
66  ImageIterator iterCurrentImage( ltReader, ltReader->GetRequestedRegion() );
67 
68  iterMaskImage.GoToBegin();
69  iterCurrentImage.GoToBegin();
70  std::vector<double> oneSample;
71  while(!iterCurrentImage.IsAtEnd())
72  {
73  if(iterMaskImage.Value()!=0)
74  { oneSample.push_back(iterCurrentImage.Value());
75 
76  if(nbImg==0)
77  {
78  const unsigned int x=iterCurrentImage.GetIndex()[0];
79  const unsigned int y=iterCurrentImage.GetIndex()[1];
80  const unsigned int z=iterCurrentImage.GetIndex()[2];
81  xSample.push_back(x);
82  ySample.push_back(y);
83  zSample.push_back(z);
84  }
85  }
86 
87  ++iterMaskImage;
88  ++iterCurrentImage;
89  }
90  allSamples.push_back(oneSample);
91  ++nbImg;
92  }
93 
94  if (resNameArg.getValue() != "")
95  {
96  // save csv
97  std::ofstream file(resNameArg.getValue());
98 
99  for(unsigned int indexI=0;indexI<allSamples.size();++indexI)
100  {
101  file<<labels[indexI];
102  if(indexI<(allSamples.size()-1))
103  file<<",";
104  }
105  file<<",indexX,indexY,indexZ";
106  file<<std::endl;
107 
108  for(unsigned int indexJ=0;indexJ<allSamples[0].size();++indexJ)
109  {
110  for(unsigned int indexI=0;indexI<allSamples.size();++indexI)
111  {
112 
113  file<<allSamples[indexI][indexJ];
114  if(indexI<(allSamples.size()-1))
115  file<<",";
116  }
117  file<<","<<xSample[indexJ]<<","<<ySample[indexJ]<<","<<zSample[indexJ];
118  file<<std::endl;
119  }
120 
121  file.close();
122  }
123 
124  return EXIT_SUCCESS;
125 }
126 
int main(int argc, char **argv)