DGtalTools  1.2.0
vol2sdp.cpp
1 
30 #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 "CLI11.hpp"
38 
39 
40 using namespace std;
41 using namespace DGtal;
42 
43 
44 
91 int main( int argc, char** argv )
92 {
93  CLI::App app;
94  std::string inputFilename;
95  std::string outputFilename {"result.sdp"};
96  bool exportImageValues {false};
97  int thresholdMin {128};
98  int thresholdMax {255};
99  DGtal::int64_t rescaleInputMin {0};
100  DGtal::int64_t rescaleInputMax {255};
101  typedef ImageContainerBySTLVector < Z3i::Domain, unsigned char > Image3D;
102 
103  // parse command line using CLI ----------------------------------------------
104  app.description("Convert volumetric file into a digital set of points from a given threshold.\n vol2sdp -i ${DGtal}/examples/samples/lobster.vol -o volumeList.sdp");
105  app.add_option("-i,--input,1", inputFilename, "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." )
106  ->required()
107  ->check(CLI::ExistingFile);
108  app.add_option("--output,-o,2", outputFilename, "sequence of discrete point file (.sdp)", true);
109  app.add_flag("--exportImageValues,-e",exportImageValues, "option to export also the image value of the voxel in a fourth field.");
110  app.add_option("--thresholdMin,-m", thresholdMin, "threshold min (excluded) to define binary shape.", true);
111  app.add_option("--thresholdMax,-M", thresholdMax, "threshold max (included) to define binary shape.", true);
112  app.add_option("--rescaleInputMin", rescaleInputMin, "min value used to rescale the input intensity (to avoid basic cast into 8 bits image).", true);
113  app.add_option("--rescaleInputMax", rescaleInputMax, "max value used to rescale the input intensity (to avoid basic cast into 8 bits image).", true);
114 
115  app.get_formatter()->column_width(40);
116  CLI11_PARSE(app, argc, argv);
117  // END parse command line using CLI ----------------------------------------------
118 
119  typedef DGtal::functors::Rescaling<DGtal::int64_t ,unsigned char > RescalFCT;
120  Image3D inputImage = GenericReader< Image3D >::importWithValueFunctor( inputFilename,
121  RescalFCT(rescaleInputMin,
122  rescaleInputMax,
123  0, 255) );
124  trace.info() << " [done] " << std::endl ;
125  std::ofstream outStream;
126  outStream.open(outputFilename.c_str());
127 
128  trace.info() << "Processing image to output file " << outputFilename ;
129  //Processing all points
130  outStream << "# sdp file generate by vol2sdp with source vol:" << inputFilename
131  << " and threshold min: " << thresholdMin << " max:" << thresholdMax << std::endl;
132  outStream << "# format: x y z ";
133  if(exportImageValues){
134  outStream << " image_value";
135  }
136  outStream << std::endl;
137 
138  for(Image3D::Domain::ConstIterator it=inputImage.domain().begin(); it != inputImage.domain().end(); ++it){
139  if(inputImage(*it) >= thresholdMin && inputImage(*it) <= thresholdMax ){
140  outStream << (*it)[0] << " " << (*it)[1] << " " << (*it)[2];
141  if(exportImageValues){
142  outStream << " " << (unsigned int) inputImage(*it);
143  }
144 
145  outStream << std::endl;
146  }
147  }
148  outStream.close();
149  trace.info() << " [done] " << std::endl ;
150  return EXIT_SUCCESS;
151 }
152 
Definition: ATu0v1.h:57