DGtalTools  1.2.0
heightfield2vol.cpp
1 
29 #include <iostream>
30 #include <fstream>
31 #include "DGtal/base/Common.h"
32 #include "DGtal/helpers/StdDefs.h"
33 #include "DGtal/images/ImageContainerBySTLVector.h"
34 #include "DGtal/io/writers/VolWriter.h"
35 #include "DGtal/io/readers/GenericReader.h"
36 #include "DGtal/images/ConstImageAdapter.h"
37 #include "DGtal/kernel/BasicPointFunctors.h"
38 
39 #include "CLI11.hpp"
40 
41 
42 using namespace std;
43 using namespace DGtal;
44 
45 
46 
83 // Defining a Helper to get the 3D point functor from an 2DImage
84 template<typename TImage2D, typename TPoint3D >
85 struct Image3DPredicatFrom2DImage{
86  typedef TPoint3D Point3D;
87  typedef HyperRectDomain<Z3i::Space> Domain;
88  typedef typename TImage2D::Value Value;
92  Image3DPredicatFrom2DImage(DGtal::ConstAlias<TImage2D> anImage, double aScale,
93  unsigned int maxHeight,
94  unsigned int fg, unsigned int bg
95  ):myImageRef(anImage),
96  myScale(aScale),
97  myMaxHeight(maxHeight),
98  myFG(fg), myBG(bg) {
99  }
100  inline
101  unsigned int operator()(const Point3D &aPoint) const {
102  functors::Projector<SpaceND<2, typename TImage2D::Integer> > projXY;
103  return (*myImageRef)(projXY(aPoint))*myScale >= aPoint[2] ? myFG: myBG ;
104  }
105 
106  inline
107  Domain domain() const {
108  return Domain(Z3i::Point(0,0,0), Z3i::Point(myImageRef->domain().upperBound()[0],
109  myImageRef->domain().upperBound()[1],
110  myMaxHeight) );
111  }
112  CountedConstPtrOrConstPtr<TImage2D> myImageRef;
113  double myScale;
114  unsigned int myMaxHeight;
115  unsigned int myFG;
116  unsigned int myBG;
117 };
118 
119 
120 
121 int main( int argc, char** argv )
122 {
123  typedef ImageContainerBySTLVector < Z2i::Domain, unsigned char> Image2D;
124 
125 // parse command line using CLI ----------------------------------------------
126  CLI::App app;
127  std::string inputFileName;
128  std::string outputFileName {"result.vol"};
129 
130  unsigned int foregroundValue = {128};
131  unsigned int backgroundValue = {0};
132  double scale {1.0};
133  unsigned int maxZ {255};
134 
135 
136  app.description("Convert a 2D heightfield image into a volumetric file.\n Example: \n heightfield2vol ${DGtal}/examples/samples/church.pgm volResu.vol -s 0.3 -z 50 \n");
137  app.add_option("-i,--input,1", inputFileName, "input heightfield file (2D image).")
138  ->check(CLI::ExistingFile)
139  ->required();
140  app.add_option("-o,--output,2", outputFileName,"output volumetric file.", true);
141  app.add_option("-s,--scale", scale, "set the scale factor on height values (default 1.0)");
142  app.add_option("-z,--volZ", maxZ, "set the Z max value of domain.");
143  app.add_option("-f,--foregroundValue", foregroundValue, "specify the foreground value of the resulting voxel.");
144  app.add_option("-b,--backgroundValue", backgroundValue, "specify the background value of the resulting volumetric file.");
145 
146  app.get_formatter()->column_width(40);
147  CLI11_PARSE(app, argc, argv);
148  // END parse command line using CLI ----------------------------------------------
149 
150 
151  trace.info() << "Reading input file " << inputFileName ;
152  Image2D inputImage = DGtal::GenericReader<Image2D>::import(inputFileName);
153 
154  trace.info() << " [done] " << std::endl ;
155 
156 
157  typedef Image3DPredicatFrom2DImage<Image2D, Z3i::Point> HeightMapVol;
158  Image3DPredicatFrom2DImage<Image2D, Z3i::Point> image3Dpredicate(inputImage, scale, maxZ, foregroundValue, backgroundValue);
159  trace.info() << "Processing image to output file " << outputFileName ;
160 
161  VolWriter<HeightMapVol>::exportVol(outputFileName, image3Dpredicate);
162  trace.info() << " [done] " << std::endl ;
163  return EXIT_SUCCESS;
164 }
Definition: ATu0v1.h:57