DGtalTools  0.9.4
slice2vol.cpp
1 
29 #include <iostream>
31 #include "DGtal/base/Common.h"
32 #include "DGtal/helpers/StdDefs.h"
33 
34 #include "DGtal/io/readers/GenericReader.h"
35 #include "DGtal/io/writers/GenericWriter.h"
36 #include "DGtal/kernel/BasicPointFunctors.h"
37 
38 #include <boost/program_options/options_description.hpp>
39 #include <boost/program_options/parsers.hpp>
40 #include <boost/program_options/variables_map.hpp>
41 
42 
43 
44 using namespace std;
45 using namespace DGtal;
46 
47 
74 namespace po = boost::program_options;
76 
77 int main( int argc, char** argv )
78 {
81 
82 
83  // parse command line ----------------------------------------------
84  po::options_description general_opt("Allowed options are: ");
85  general_opt.add_options()
86  ("help,h", "display this message")
87  ("sliceOrientation,s", po::value<unsigned int>()->default_value(2), "specify the slice orientation for which the slice are considered (by default =2 (Z direction))" )
88  ("input,i", po::value<std::vector <std::string> >()->multitoken(), "input 2D files (.pgm) " )
89  ("output,o", po::value<std::string>(), "volumetric file (.vol, .longvol .pgm3d) " );
90 
91 
92  bool parseOK=true;
93  po::variables_map vm;
94  try{
95  po::store(po::parse_command_line(argc, argv, general_opt), vm);
96  }catch(const std::exception& ex){
97  parseOK=false;
98  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
99  }
100  po::notify(vm);
101 
102 
103  if( ! vm.count("input") || ! vm.count("output") || !parseOK || vm.count("help"))
104  {
105  std::cout << "Usage: " << argv[0] << " [input-files] [output]\n"
106  << "Converts set of 2D images into volumetric file (pgm3d, vol, longvol). "
107  << general_opt << "\n";
108  std::cout << "Example:\n"
109  << "slice2vol -i slice1.pgm slice2.pgm slice3.pgm -o out.vol \n"
110  << "see vol2slice"<<endl;
111 
112  return 0;
113  }
114 
115  if(! (vm.count("input") && vm.count("output")) )
116  {
117  trace.error() << " Input and output filename are needed to be defined" << endl;
118  return 0;
119  }
120 
121 
122 
123  std::string outputFileName = vm["output"].as<std::string>();
124  std::vector<string> vectImage2DNames = vm["input"].as<std::vector<std::string> >();
125  unsigned int sliceOrientation = vm["sliceOrientation"].as<unsigned int>();
126  std::vector<Image2D> vectImages2D;
127  // Reading all images
128  for(unsigned int i=0; i< vectImage2DNames.size(); i++){
129  trace.info() << "Reading image " << i ;
130  Image2D image = GenericReader<Image2D>::import(vectImage2DNames.at(i));
131  vectImages2D.push_back(image);
132  trace.info() << " [done]" << std::endl;
133  }
134 
135  Image2D::Domain domImage2D = vectImages2D.at(0).domain();
137  DGtal::functors::Projector<DGtal::Z3i::Space> projIn3Dupper(vectImages2D.size()-1);
138  projIn3Dlower.initAddOneDim(sliceOrientation);
139  projIn3Dupper.initAddOneDim(sliceOrientation);
140  Image3D::Domain domImage3D (projIn3Dlower(vectImages2D.at(0).domain().lowerBound()),
141  projIn3Dupper(vectImages2D.at(0).domain().upperBound()));
142 
143  Image3D imageResult (domImage3D);
144  for( unsigned int i=0; i<vectImages2D.size(); i++){
145  Image2D sliceImage = vectImages2D.at(i);
147  projIn3D.initAddOneDim(sliceOrientation);
148  for(Image2D::Domain::ConstIterator it = sliceImage.domain().begin();
149  it!= sliceImage.domain().end(); it++){
150  Z3i::Point pt =projIn3D(*it);
151  imageResult.setValue(pt, sliceImage(*it));
152  }
153  }
154  trace.info() << "Exporting 3d image ... " << std::endl ;
155  GenericWriter<Image3D>::exportFile(outputFileName, imageResult);
156  trace.info() << "[done]";
157  return 0;
158 }
159 
160 
161 
162 
STL namespace.
Trace trace(traceWriterTerm)
std::ostream & info()
void initAddOneDim(const Dimension &newDim)
const Domain & domain() const
std::vector< Value >::const_iterator ConstIterator
std::ostream & error()