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"
43 using namespace DGtal;
84 template<
typename TImage2D,
typename TPo
int3D >
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),
97 myMaxHeight(maxHeight),
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 ;
107 Domain domain()
const {
108 return Domain(Z3i::Point(0,0,0), Z3i::Point(myImageRef->domain().upperBound()[0],
109 myImageRef->domain().upperBound()[1],
112 CountedConstPtrOrConstPtr<TImage2D> myImageRef;
114 unsigned int myMaxHeight;
121 int main(
int argc,
char** argv )
123 typedef ImageContainerBySTLVector < Z2i::Domain, unsigned char> Image2D;
127 std::string inputFileName;
128 std::string outputFileName {
"result.vol"};
130 unsigned int foregroundValue = {128};
131 unsigned int backgroundValue = {0};
133 unsigned int maxZ {255};
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)
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.");
146 app.get_formatter()->column_width(40);
147 CLI11_PARSE(app, argc, argv);
151 trace.info() <<
"Reading input file " << inputFileName ;
152 Image2D inputImage = DGtal::GenericReader<Image2D>::import(inputFileName);
154 trace.info() <<
" [done] " << std::endl ;
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 ;
161 VolWriter<HeightMapVol>::exportVol(outputFileName, image3Dpredicate);
162 trace.info() <<
" [done] " << std::endl ;