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" 41 #include <boost/program_options/options_description.hpp> 42 #include <boost/program_options/parsers.hpp> 43 #include <boost/program_options/variables_map.hpp> 46 using namespace DGtal;
50 namespace po = boost::program_options;
87 template<
typename TImage2D,
typename TPo
int3D >
88 struct Image3DPredicatFrom2DImage{
89 typedef TPoint3D Point3D;
90 typedef HyperRectDomain<Z3i::Space> Domain;
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),
100 myMaxHeight(maxHeight),
104 unsigned int operator()(
const Point3D &aPoint)
const {
105 functors::Projector<SpaceND<2, typename TImage2D::Integer> > projXY;
106 return (*myImageRef)(projXY(aPoint))*myScale >= aPoint[2] ? myFG: myBG ;
110 Domain domain()
const {
111 return Domain(Z3i::Point(0,0,0), Z3i::Point(myImageRef->domain().upperBound()[0],
112 myImageRef->domain().upperBound()[1],
115 CountedConstPtrOrConstPtr<TImage2D> myImageRef;
117 unsigned int myMaxHeight;
124 int main(
int argc,
char** argv )
126 typedef ImageContainerBySTLVector < Z3i::Domain, unsigned char > Image3D;
127 typedef ImageContainerBySTLVector < Z2i::Domain, unsigned char> Image2D;
129 typedef DGtal::ConstImageAdapter<Image3D, Z2i::Domain, DGtal::functors::Point2DEmbedderIn3D<DGtal::Z3i::Domain>,
130 Image3D::Value, DGtal::functors::Identity > ImageAdapterExtractor;
133 po::options_description general_opt(
"Allowed options are: ");
134 general_opt.add_options()
135 (
"help,h",
"display this message")
136 (
"input,i", po::value<std::string>(),
"heightfield file." )
137 (
"output,o", po::value<std::string>(),
"volumetric file ")
138 (
"scale,s", po::value<double>()->default_value(1.0),
"set the scale factor on height values. (default 1.0)")
139 (
"volZ,z", po::value<unsigned int>()->default_value(255),
"set the Z max value of domain.")
140 (
"foregroundValue,f", po::value<unsigned int>()->default_value(128),
"specify the foreground value of the resulting voxel." )
141 (
"backgroundValue,b", po::value<unsigned int>()->default_value(0),
"specify the background value of the resulting volumetric file.");
144 po::variables_map vm;
146 po::store(po::parse_command_line(argc, argv, general_opt), vm);
147 }
catch(
const std::exception& ex){
149 trace.info()<<
"Error checking program options: "<< ex.what()<< endl;
152 if( !parseOK || vm.count(
"help")||argc<=1)
154 std::cout <<
"Usage: " << argv[0] <<
" [input] [output]\n" 155 <<
"Convert a 2D heightfield image into a volumetric file. " 156 << general_opt <<
"\n";
157 std::cout <<
"Example:\n" 158 <<
"heightfield2vol -i ${DGtal}/examples/samples/church.pgm -o volResu.vol -s 0.3 -z 50 \n";
162 if(! vm.count(
"input") ||! vm.count(
"output"))
164 trace.error() <<
" Input and output filename are needed to be defined" << endl;
168 string inputFilename = vm[
"input"].as<std::string>();
169 string outputFilename = vm[
"output"].as<std::string>();
171 trace.info() <<
"Reading input file " << inputFilename ;
172 Image2D inputImage = DGtal::GenericReader<Image2D>::import(inputFilename);
173 double scale = vm[
"scale"].as<
double>();
174 unsigned int maxZ = vm[
"volZ"].as<
unsigned int>();
175 trace.info() <<
" [done] " << std::endl ;
177 unsigned int foregroundValue = vm[
"foregroundValue"].as<
unsigned int>();
178 unsigned int backgroundValue = vm[
"backgroundValue"].as<
unsigned int>();
180 typedef Image3DPredicatFrom2DImage<Image2D, Z3i::Point> HeightMapVol;
181 Image3DPredicatFrom2DImage<Image2D, Z3i::Point> image3Dpredicate(inputImage, scale, maxZ, foregroundValue, backgroundValue);
182 trace.info() <<
"Processing image to output file " << outputFilename ;
184 VolWriter<HeightMapVol>::exportVol(outputFilename, image3Dpredicate);
186 trace.info() <<
" [done] " << std::endl ;