DGtalTools  0.9.4
heightfield2vol.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/writers/VolWriter.h"
36 #include "DGtal/io/readers/GenericReader.h"
37 #include "DGtal/images/ConstImageAdapter.h"
38 #include "DGtal/kernel/BasicPointFunctors.h"
39 
40 
41 #include <boost/program_options/options_description.hpp>
42 #include <boost/program_options/parsers.hpp>
43 #include <boost/program_options/variables_map.hpp>
44 
45 using namespace std;
46 using namespace DGtal;
47 
48 
50 namespace po = boost::program_options;
51 
86 // Defining a Helper to get the 3D point functor from an 2DImage
87 template<typename TImage2D, typename TPoint3D >
88 struct Image3DPredicatFrom2DImage{
89  typedef TPoint3D Point3D;
91  typedef typename TImage2D::Value Value;
95  Image3DPredicatFrom2DImage(DGtal::ConstAlias<TImage2D> anImage, double aScale,
96  unsigned int maxHeight,
97  unsigned int fg, unsigned int bg
98  ):myImageRef(anImage),
99  myScale(aScale),
100  myMaxHeight(maxHeight),
101  myFG(fg), myBG(bg) {
102  }
103  inline
104  unsigned int operator()(const Point3D &aPoint) const {
106  return (*myImageRef)(projXY(aPoint))*myScale >= aPoint[2] ? myFG: myBG ;
107  }
108 
109  inline
110  Domain domain() const {
111  return Domain(Z3i::Point(0,0,0), Z3i::Point(myImageRef->domain().upperBound()[0],
112  myImageRef->domain().upperBound()[1],
113  myMaxHeight) );
114  }
116  double myScale;
117  unsigned int myMaxHeight;
118  unsigned int myFG;
119  unsigned int myBG;
120 };
121 
122 
123 
124 int main( int argc, char** argv )
125 {
127 
128 
129  // parse command line ----------------------------------------------
130  po::options_description general_opt("Allowed options are: ");
131  general_opt.add_options()
132  ("help,h", "display this message")
133  ("input,i", po::value<std::string>(), "heightfield file." )
134  ("output,o", po::value<std::string>(), "volumetric file ")
135  ("scale,s", po::value<double>()->default_value(1.0), "set the scale factor on height values. (default 1.0)")
136  ("volZ,z", po::value<unsigned int>()->default_value(255), "set the Z max value of domain.")
137  ("foregroundValue,f", po::value<unsigned int>()->default_value(128), "specify the foreground value of the resulting voxel." )
138  ("backgroundValue,b", po::value<unsigned int>()->default_value(0), "specify the background value of the resulting volumetric file.");
139 
140  bool parseOK=true;
141  po::variables_map vm;
142  try{
143  po::store(po::parse_command_line(argc, argv, general_opt), vm);
144  }catch(const std::exception& ex){
145  parseOK=false;
146  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
147  }
148  po::notify(vm);
149  if( !parseOK || vm.count("help")||argc<=1)
150  {
151  std::cout << "Usage: " << argv[0] << " [input] [output]\n"
152  << "Convert a 2D heightfield image into a volumetric file. "
153  << general_opt << "\n";
154  std::cout << "Example:\n"
155  << "heightfield2vol -i ${DGtal}/examples/samples/church.pgm -o volResu.vol -s 0.3 -z 50 \n";
156  return 0;
157  }
158 
159  if(! vm.count("input") ||! vm.count("output"))
160  {
161  trace.error() << " Input and output filename are needed to be defined" << endl;
162  return 0;
163  }
164 
165  string inputFilename = vm["input"].as<std::string>();
166  string outputFilename = vm["output"].as<std::string>();
167 
168  trace.info() << "Reading input file " << inputFilename ;
169  Image2D inputImage = DGtal::GenericReader<Image2D>::import(inputFilename);
170  double scale = vm["scale"].as<double>();
171  unsigned int maxZ = vm["volZ"].as<unsigned int>();
172  trace.info() << " [done] " << std::endl ;
173 
174  unsigned int foregroundValue = vm["foregroundValue"].as<unsigned int>();
175  unsigned int backgroundValue = vm["backgroundValue"].as<unsigned int>();
176 
177  typedef Image3DPredicatFrom2DImage<Image2D, Z3i::Point> HeightMapVol;
178  Image3DPredicatFrom2DImage<Image2D, Z3i::Point> image3Dpredicate(inputImage, scale, maxZ, foregroundValue, backgroundValue);
179  trace.info() << "Processing image to output file " << outputFilename ;
180 
181  VolWriter<HeightMapVol>::exportVol(outputFilename, image3Dpredicate);
182 
183  trace.info() << " [done] " << std::endl ;
184  return 0;
185 }
STL namespace.
static TContainer import(const std::string &filename, std::vector< unsigned int > dimSpace=std::vector< unsigned int >())
Trace trace(traceWriterTerm)
std::ostream & info()
std::ostream & error()
typename Self::Domain Domain