DGtalTools  0.9.4
volReSample.cpp
1 
29 #include <iostream>
31 #include "DGtal/base/Common.h"
32 #include "DGtal/helpers/StdDefs.h"
33 
34 #include "DGtal/io/readers/GenericReader.h"
35 #include "DGtal/io/writers/GenericWriter.h"
36 #include "DGtal/images/ConstImageAdapter.h"
37 #include "DGtal/kernel/BasicPointFunctors.h"
38 
39 #include <boost/program_options/options_description.hpp>
40 #include <boost/program_options/parsers.hpp>
41 #include <boost/program_options/variables_map.hpp>
42 
43 
44 
45 using namespace std;
46 using namespace DGtal;
47 
48 
49 
98 namespace po = boost::program_options;
100 
101 int main( int argc, char** argv )
102 {
105  DGtal::int32_t, double > ReSampler;
106  typedef DGtal::ConstImageAdapter<Image3D, Image3D::Domain, ReSampler,
107  Image3D::Value, DGtal::functors::Identity > SamplerImageAdapter;
108 
109 
110  // parse command line ----------------------------------------------
111  po::options_description general_opt("Allowed options are: ");
112  general_opt.add_options()
113  ("help,h", "display this message")
114  ("input,i", po::value<std::string >(), "input volumetric file (.vol, .longvol, .pgm3d) " )
115  ("output,o", po::value<std::string>(), "the new volumetric file (.vol, .longvol, .pgm3d) " )
116  ("gridSize,g", po::value<std::vector<double> >()->multitoken(), "size_x size_y size_z : the grid size of the re sampling ");
117 
118 
119  bool parseOK=true;
120  po::variables_map vm;
121  try{
122  po::store(po::parse_command_line(argc, argv, general_opt), vm);
123  }catch(const std::exception& ex){
124  parseOK=false;
125  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
126  }
127  po::notify(vm);
128 
129 
130 
131  if( !parseOK || vm.count("help") || !vm.count("gridSize") || ! vm.count("input")||! vm.count("output") )
132  {
133  std::cout << "Usage: " << argv[0] << " [input-files] [output-file]\n"
134  << "Re sample a 3D volumetric image (.vol, .longvol, .pgm3d) with a given grid size. \n"
135  << general_opt << "\n";
136  std::cout << "Example:\n to re sample an image with scale x,y,z = 0.98, 0.98, 5.0, you can do: \n"
137  << "volResSample -i image3d.vol -g 1 1 2 -o imageReSampled.vol \n" << endl;
138 
139  return 0;
140  }
141 
142 
143 
144 
145  std::vector< double > aGridSizeReSample = vm["gridSize"].as<std::vector<double > >();
146  if(aGridSizeReSample.size()!=3){
147  trace.error() << "The grid size should contains 3 elements" << std::endl;
148  return 0;
149  }
150 
151  std::string inputFileName = vm["input"].as<std::string>();
152  std::string outputFileName = vm["output"].as<std::string>();
153 
154 
155  trace.info()<< "Importing volume file : " << inputFileName<< " ... " ;
156  Image3D input3dImage = GenericReader<Image3D>::import(inputFileName);
157  trace.info()<< "[done]" << endl;
158 
159 
160  PointVector<3,int> shiftVector3D(0 ,0, 0);
161  DGtal::functors::BasicDomainSubSampler< HyperRectDomain<SpaceND<3, int> >,
162  DGtal::int32_t, double > reSampler(input3dImage.domain(),
163  aGridSizeReSample, shiftVector3D);
164 
165  const functors::Identity aFunctor{};
166  SamplerImageAdapter sampledImage ( input3dImage, reSampler.getSubSampledDomain(), reSampler, aFunctor );
167  GenericWriter<SamplerImageAdapter>::exportFile(outputFileName, sampledImage);
168 
169 
170  return 0;
171 }
STL namespace.
Trace trace(traceWriterTerm)
std::ostream & info()
const Domain & domain() const
boost::int32_t int32_t
std::ostream & error()