DGtal  0.9.2
imageBasicSubsampling.cpp
1 
30 #include <iostream>
32 #include "ConfigExamples.h"
33 #include "DGtal/helpers/StdDefs.h"
34 #include "DGtal/base/Common.h"
35 
37 #include "DGtal/kernel/BasicPointFunctors.h"
38 #include "DGtal/images/ConstImageAdapter.h"
40 
41 #include "DGtal/images/ImageContainerBySTLVector.h"
42 
43 #include "DGtal/io/readers/GenericReader.h"
44 #include "DGtal/io/writers/GenericWriter.h"
45 
47 
48 using namespace std;
49 using namespace DGtal;
50 
52 
53 int main( int /*argc*/, char** /*argv*/ )
54 {
55 
58  typedef ConstImageAdapter<Image2D, Image2D::Domain,
60  Image2D::Value,
61  functors::Identity > ConstImageAdapterForSubSampling;
62 
64 
67  typedef ConstImageAdapter<Image3D, Image3D::Domain,
69  Image3D::Value,
70  functors::Identity > ConstImageAdapterForSubSampling3D;
72 
73 
74  trace.beginBlock ( "Example imageBasicSubsampling" );
75 
76  std::string imageFilename3D = examplesPath + "samples/Al.100.vol";
77  std::string imageFilename2D = examplesPath + "samples/church.pgm";
78  Image3D image3D = GenericReader<Image3D>::import( imageFilename3D );
79  Image2D image2D = GenericReader<Image2D>::import( imageFilename2D );
80 
81  for (unsigned int i=1; i<=20; i*=2){
82  std::vector<Z2i::Domain::Size> aGridSize2D;
83  aGridSize2D.push_back(i);
84  aGridSize2D.push_back(i);
85  // Construction of the functor for the 2D domain subsampling from a given grid size and shift.
87  DGtal::functors::BasicDomainSubSampler<Image2D::Domain> subSampler2D(image2D.domain(), aGridSize2D, Z2i::Point(0 ,0));
89 
91  // Get the new domain produces from the subsampler and define the ConstImageAdapter:
93  Image2D::Domain subSampledDomain2D = subSampler2D.getSubSampledDomain();
94  ConstImageAdapterForSubSampling subsampledImage2D (image2D, subSampledDomain2D, subSampler2D, df);
96 
97  stringstream outputname;
98  outputname << "subSampledImage"<< i<< "x"<< i << ".pgm" ;
100  GenericWriter<ConstImageAdapterForSubSampling>::exportFile(outputname.str(), subsampledImage2D );
102  trace.info() << "Exporting 2D subsampled image by grid size :" << i << "x"<< i<< " in "<< outputname.str() << std::endl;
103  }
104 
105  for (unsigned int i=1; i<=20; i*=2){
106  std::vector<Z3i::Domain::Size> aGridSize3D;
107  aGridSize3D.push_back(i);
108  aGridSize3D.push_back(i);
109  aGridSize3D.push_back(i);
110 
111  // Construction of the functor for the 3D domain subsampling from a given grid size and shift.
113  DGtal::functors::BasicDomainSubSampler<Image3D::Domain> subSampler3D(image3D.domain(), aGridSize3D, Z3i::Point(0 ,0, 0));
115 
117  // Get the new domain produces from the subsampler and define the ConstImageAdapter:
119  Image3D::Domain subSampledDomain3D = subSampler3D.getSubSampledDomain();
120  ConstImageAdapterForSubSampling3D subsampledImage3D (image3D, subSampledDomain3D, subSampler3D, df);
122 
123  stringstream outputname3D;
124  outputname3D << "subSampledImage3D"<< i<< "x"<< i << ".vol" ;
126  GenericWriter<ConstImageAdapterForSubSampling3D>::exportFile(outputname3D.str(), subsampledImage3D );
128  trace.info() << "Exporting 3D subsampled image by grid size :" << i << "x"<< i<< "x"<< i<< " in "<< outputname3D.str() << std::endl;
129  }
130 
131 
132  trace.endBlock();
133  return 0;
134 }
135 // //
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:130
STL namespace.
double endBlock()
Aim: Functor that subsamples an initial domain by given a grid size and a shift vector. By this way, for a given point considered in a new domain, it allows to recover the point coordinates in the source domain. Such functor can be usefull to apply basic image subsampling in any dimensions by using ImageAdapter class.
Aim: Provide a mechanism to save image (2D or 3D) into file with the best saver loader according to a...
Definition: GenericWriter.h:93
Aim: Define a simple default functor that just returns its argument.
Aim: Provide a mechanism to load with the bestloader according to an image (2D or 3D) filename (by pa...
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & info()
Aim: implements a const image adapter with a given domain (i.e. a subdomain) and 2 functors : g for d...