DGtalTools  0.9.4
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/GenericReader.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 
95 int main( int argc, char** argv )
96 {
98 
99  // parse command line ----------------------------------------------
100  po::options_description general_opt("Allowed options are: ");
101  general_opt.add_options()
102  ("help,h", "display this message")
103  ("input,i", po::value<std::string>(), "vol file (.vol, .longvol .p3d, .pgm3d and if WITH_ITK is selected: dicom, dcm, mha, mhd) or sdp (sequence of discrete points). For longvol, dicom, dcm, mha or mhd formats, the input values are linearly scaled between 0 and 255." )
104  ("output,o", po::value<std::string>(), "sequence of discrete point file (.sdp) " )
105  ("exportImageValues,e","option to export also the image value of the voxel in a fourth field.")
106  ("thresholdMin,m", po::value<int>()->default_value(128), "min threshold (default 128)" )
107  ("thresholdMax,M", po::value<int>()->default_value(255), "max threshold (default 255)" )
108  ("rescaleInputMin", po::value<DGtal::int64_t>()->default_value(0), "min value used to rescale the input intensity (to avoid basic cast into 8 bits image).")
109  ("rescaleInputMax", po::value<DGtal::int64_t>()->default_value(255), "max value used to rescale the input intensity (to avoid basic cast into 8 bits image).");
110 
111 
112  bool parseOK=true;
113  po::variables_map vm;
114  try{
115  po::store(po::parse_command_line(argc, argv, general_opt), vm);
116  }catch(const std::exception& ex){
117  parseOK=false;
118  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
119  }
120  po::notify(vm);
121  if( !parseOK || vm.count("help")||argc<=1)
122  {
123  std::cout << "Usage: " << argv[0] << " [input] [output]\n"
124  << "Convert volumetric file into a digital set of points from a given threshold."
125  << general_opt << "\n";
126  std::cout << "Example:\n"
127  << "vol2sdp -i ${DGtal}/examples/samples/lobster.vol -o volumeList.sdp \n";
128  return 0;
129  }
130 
131  if(! vm.count("input") ||! vm.count("output"))
132  {
133  trace.error() << " Input and output filename are needed to be defined" << endl;
134  return 0;
135  }
136 
137 
138  string inputFilename = vm["input"].as<std::string>();
139  string outputFilename = vm["output"].as<std::string>();
140 
141  trace.info() << "Reading input file " << inputFilename ;
142  DGtal::int64_t rescaleInputMin = vm["rescaleInputMin"].as<DGtal::int64_t>();
143  DGtal::int64_t rescaleInputMax = vm["rescaleInputMax"].as<DGtal::int64_t>();
144 
146  Image3D inputImage = GenericReader< Image3D >::importWithValueFunctor( inputFilename,RescalFCT(rescaleInputMin,
147  rescaleInputMax,
148  0, 255) );
149  trace.info() << " [done] " << std::endl ;
150  std::ofstream outStream;
151  outStream.open(outputFilename.c_str());
152  int minTh = vm["thresholdMin"].as<int>();
153  int maxTh = vm["thresholdMax"].as<int>();
154 
155  trace.info() << "Processing image to output file " << outputFilename ;
156  //Processing all points
157  outStream << "# sdp file generate by vol2sdp with source vol:" << inputFilename << " and threshold min: " << minTh << " max:" << maxTh << std::endl;
158  outStream << "# format: x y z ";
159  if(vm.count("exportImageValues")){
160  outStream << " image_value";
161  }
162  outStream << std::endl;
163 
164 
165  for(Image3D::Domain::ConstIterator it=inputImage.domain().begin(); it != inputImage.domain().end(); ++it){
166  if(inputImage(*it) >= minTh && inputImage(*it) <= maxTh ){
167  outStream << (*it)[0] << " " << (*it)[1] << " " << (*it)[2];
168  if(vm.count("exportImageValues")){
169  outStream << " " << (unsigned int) inputImage(*it);
170  }
171 
172  outStream << std::endl;
173  }
174  }
175  outStream.close();
176 
177  trace.info() << " [done] " << std::endl ;
178 
179 
180  return 0;
181 
182 }
183 
184 
185 
186 
STL namespace.
Trace trace(traceWriterTerm)
std::ostream & info()
const Domain & domain() const
std::vector< Value >::const_iterator ConstIterator
std::ostream & error()
boost::int64_t int64_t