DGtalTools  0.9.2
vol2sdp.cpp
1 
29 #include <iostream>
31 #include <fstream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 #include "DGtal/images/ImageContainerBySTLVector.h"
35 #include "DGtal/io/readers/VolReader.h"
36 
37 #include <boost/program_options/options_description.hpp>
38 #include <boost/program_options/parsers.hpp>
39 #include <boost/program_options/variables_map.hpp>
40 
41 using namespace std;
42 using namespace DGtal;
43 
44 
46 namespace po = boost::program_options;
47 
48 
82 int main( int argc, char** argv )
83 {
84  typedef ImageContainerBySTLVector < Z3i::Domain, unsigned char > Image3D;
85 
86  // parse command line ----------------------------------------------
87  po::options_description general_opt("Allowed options are: ");
88  general_opt.add_options()
89  ("help,h", "display this message")
90  ("input,i", po::value<std::string>(), "volumetric file (.vol) " )
91  ("output,o", po::value<std::string>(), "sequence of discrete point file (.sdp) " )
92  ("exportImageValues,e","option to export also the image value of the voxel in a fourth field.")
93  ("thresholdMin,m", po::value<int>()->default_value(128), "min threshold (default 128)" )
94  ("thresholdMax,M", po::value<int>()->default_value(255), "max threshold (default 255)" );
95 
96 
97  bool parseOK=true;
98  po::variables_map vm;
99  try{
100  po::store(po::parse_command_line(argc, argv, general_opt), vm);
101  }catch(const std::exception& ex){
102  parseOK=false;
103  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
104  }
105  po::notify(vm);
106  if( !parseOK || vm.count("help")||argc<=1)
107  {
108  std::cout << "Usage: " << argv[0] << " [input] [output]\n"
109  << "Convert volumetric file into a digital set of points from a given threshold."
110  << general_opt << "\n";
111  std::cout << "Example:\n"
112  << "vol2sdp -i ${DGtal}/examples/samples/lobster.vol -o volumeList.sdp \n";
113  return 0;
114  }
115 
116  if(! vm.count("input") ||! vm.count("output"))
117  {
118  trace.error() << " Input and output filename are needed to be defined" << endl;
119  return 0;
120  }
121 
122 
123  string inputFilename = vm["input"].as<std::string>();
124  string outputFilename = vm["output"].as<std::string>();
125 
126  trace.info() << "Reading input file " << inputFilename ;
127  Image3D inputImage = DGtal::VolReader<Image3D>::importVol(inputFilename);
128  trace.info() << " [done] " << std::endl ;
129  std::ofstream outStream;
130  outStream.open(outputFilename.c_str());
131  int minTh = vm["thresholdMin"].as<int>();
132  int maxTh = vm["thresholdMax"].as<int>();
133 
134  trace.info() << "Processing image to output file " << outputFilename ;
135  //Processing all points
136  outStream << "# sdp file generate by vol2sdp with source vol:" << inputFilename << " and threshold min: " << minTh << " max:" << maxTh << std::endl;
137  outStream << "# format: x y z ";
138  if(vm.count("exportImageValues")){
139  outStream << " image_value";
140  }
141  outStream << std::endl;
142 
143 
144  for(Image3D::Domain::ConstIterator it=inputImage.domain().begin(); it != inputImage.domain().end(); ++it){
145  if(inputImage(*it) >= minTh && inputImage(*it) <= maxTh ){
146  outStream << (*it)[0] << " " << (*it)[1] << " " << (*it)[2];
147  if(vm.count("exportImageValues")){
148  outStream << " " << (unsigned int) inputImage(*it);
149  }
150 
151  outStream << std::endl;
152  }
153  }
154  outStream.close();
155 
156  trace.info() << " [done] " << std::endl ;
157 
158 
159  return 0;
160 
161 }
162 
163 
164 
165 
STL namespace.