DGtalTools 2.0.0
Loading...
Searching...
No Matches
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
42using namespace std;
43using namespace DGtal;
44
45
46
85// Defining a Helper to get the 3D point functor from an 2DImage
86template<typename TImage2D, typename TPoint3D >
87struct Image3DPredicatFrom2DImage{
88 typedef TPoint3D Point3D;
89 typedef HyperRectDomain<Z3i::Space> Domain;
90 typedef typename TImage2D::Value Value;
94 Image3DPredicatFrom2DImage(DGtal::ConstAlias<TImage2D> anImage, double aScale,
95 unsigned int maxHeight,
96 unsigned int fg, unsigned int bg
97 ):myImageRef(anImage),
98 myScale(aScale),
99 myMaxHeight(maxHeight),
100 myFG(fg), myBG(bg) {
101 }
102 inline
103 unsigned int operator()(const Point3D &aPoint) const {
104 functors::Projector<SpaceND<2, typename TImage2D::Integer> > projXY;
105 return (*myImageRef)(projXY(aPoint))*myScale >= aPoint[2] ? myFG: myBG ;
106 }
107
108 inline
109 Domain domain() const {
110 return Domain(Z3i::Point(0,0,0), Z3i::Point(myImageRef->domain().upperBound()[0],
111 myImageRef->domain().upperBound()[1],
112 myMaxHeight) );
113 }
114 CountedConstPtrOrConstPtr<TImage2D> myImageRef;
115 double myScale;
116 unsigned int myMaxHeight;
117 unsigned int myFG;
118 unsigned int myBG;
119};
120
121
122
123int main( int argc, char** argv )
124{
125 typedef ImageContainerBySTLVector < Z2i::Domain, unsigned char> Image2D;
126
127// parse command line using CLI ----------------------------------------------
128 CLI::App app;
129 std::string inputFileName;
130 std::string outputFileName {"result.vol"};
131
132 unsigned int foregroundValue = {128};
133 unsigned int backgroundValue = {0};
134 double scale {1.0};
135 unsigned int maxZ {255};
136
137
138 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");
139 app.add_option("-i,--input,1", inputFileName, "input heightfield file (2D image).")
140 ->check(CLI::ExistingFile)
141 ->required();
142 app.add_option("-o,--output,2", outputFileName,"output volumetric file.");
143 app.add_option("-s,--scale", scale, "set the scale factor on height values (default 1.0)");
144 app.add_option("-z,--volZ", maxZ, "set the Z max value of domain.");
145 app.add_option("-f,--foregroundValue", foregroundValue, "specify the foreground value of the resulting voxel.");
146 app.add_option("-b,--backgroundValue", backgroundValue, "specify the background value of the resulting volumetric file.");
147
148 app.get_formatter()->column_width(40);
149 CLI11_PARSE(app, argc, argv);
150 // END parse command line using CLI ----------------------------------------------
151
152
153 trace.info() << "Reading input file " << inputFileName ;
154 Image2D inputImage = DGtal::GenericReader<Image2D>::import(inputFileName);
155
156 trace.info() << " [done] " << std::endl ;
157
158
159 typedef Image3DPredicatFrom2DImage<Image2D, Z3i::Point> HeightMapVol;
160 Image3DPredicatFrom2DImage<Image2D, Z3i::Point> image3Dpredicate(inputImage, scale, maxZ, foregroundValue, backgroundValue);
161 trace.info() << "Processing image to output file " << outputFileName ;
162
163 VolWriter<HeightMapVol>::exportVol(outputFileName, image3Dpredicate);
164 trace.info() << " [done] " << std::endl ;
165 return EXIT_SUCCESS;
166}
Definition ATu0v1.h:57