DGtalTools  1.5.beta
volSubSample.cpp
1 
28 #include <iostream>
29 #include <DGtal/base/Common.h>
30 #include <DGtal/io/readers/VolReader.h>
31 #include <DGtal/io/writers/VolWriter.h>
32 #include <DGtal/helpers/StdDefs.h>
33 #include <DGtal/images/Image.h>
34 #include <DGtal/images/ImageContainerBySTLVector.h>
35 
36 #include "CLI11.hpp"
37 
38 
39 using namespace std;
40 using namespace DGtal;
41 using namespace Z3i;
42 
43 
112 void missingParam ( std::string param )
113 {
114  trace.error() <<" Parameter: "<<param<<" is required..";
115  trace.info() <<std::endl;
116  exit ( 1 );
117 }
118 
119 template<typename Val, typename Image, typename Point, typename Domain>
120 Val maxVal(Image const& image, Point const& p, Domain const& domain)
121 {
122  typename Image::Domain dom( p*2, p*2 + Point::diagonal(1));
123  Val v=image(p*2);
124  for(typename Image::Domain::ConstIterator it=dom.begin(), itend=dom.end();
125  it != itend;
126  ++it)
127  if ( domain.isInside(*it) && image( *it) > v) v=image(*it);
128 
129  return v;
130 }
131 template<typename Val, typename Image, typename Point, typename Domain>
132 Val minVal(Image const& image, Point const& p, Domain const& domain)
133 {
134  typename Image::Domain dom( p*2, p*2 + Point::diagonal(1));
135  Val v=image(p*2);
136  for(typename Image::Domain::ConstIterator it=dom.begin(), itend=dom.end();
137  it != itend;
138  ++it)
139  if ( domain.isInside(*it)&& image( *it) < v) v=image(*it);
140 
141  return v;
142 }
143 template<typename Val, typename Image, typename Point, typename Domain>
144 Val meanVal(Image const& image, Point const& p, Domain const& domain)
145 {
146  typename Image::Domain dom( p*2, p*2 + Point::diagonal(1));
147  int v=0;
148  int nb=0;
149  for(typename Image::Domain::ConstIterator it=dom.begin(), itend=dom.end();
150  it != itend;
151  ++it)
152  if ( domain.isInside(*it) )
153  {
154  nb++;
155  v+=image(*it);
156  }
157  return static_cast<unsigned char>( v/nb );
158 }
159 
160 
161 int main(int argc, char**argv)
162 {
163 
164  // parse command line ----------------------------------------------
165  // parse command line using CLI ----------------------------------------------
166  CLI::App app;
167  std::string inputFileName;
168  std::string outputFileName {"result.vol"};
169  std::string function {"mean"};
170 
171  app.description("Brutally sub sample a vol file (division by 2 in each direction).\n Basic usage: \n \tvolSubSample <volFileName> <volOutputFileName> ");
172 
173 
174  app.add_option("-i,--input,1", inputFileName, "Input vol file." )
175  ->required()
176  ->check(CLI::ExistingFile);
177  app.add_option("-o,--output,2", outputFileName, "Output filename.",true);
178  app.add_option("-f,--function", function, "Function used to the down-sampling: {none,max, min, mean}", true)
179  -> check(CLI::IsMember({"mean", "none", "max", "min", "mean"}));
180 
181  app.get_formatter()->column_width(40);
182  CLI11_PARSE(app, argc, argv);
183  // END parse command line using CLI ----------------------------------------------
184 
185 
186  trace.beginBlock("Loading file");
188 
189  MyImageC imageC = VolReader< MyImageC >::importVol ( inputFileName );
190  MyImageC outputImage( Z3i::Domain( imageC.domain().lowerBound(),
191  (imageC.domain().upperBound()-imageC.domain().lowerBound())/Vector().diagonal(2)));
192 
193  trace.endBlock();
194  Point subvector = Vector().diagonal(2);
195  Point p;
196  unsigned char val;
197  trace.beginBlock("Down-scaling the volume...");
198  trace.info()<<"Function= "<<function<<std::endl;
199  trace.info() << outputImage.domain() << std::endl;
200  //Fast Copy
201  if (function == "none")
202  for(MyImageC::Domain::ConstIterator it = outputImage.domain().begin(),
203  itend = outputImage.domain().end(); it != itend; ++it)
204  {
205  p = (*it) * 2;
206  outputImage.setValue( *it , imageC( p ));
207  }
208  else
209  if (function == "max")
210  for(MyImageC::Domain::ConstIterator it = outputImage.domain().begin(),
211  itend = outputImage.domain().end(); it != itend; ++it)
212  {
213  val = maxVal<unsigned char, MyImageC, Point>(imageC, *it, imageC.domain());
214  outputImage.setValue( *it , val );
215  }
216  else
217  if (function == "min")
218  for(MyImageC::Domain::ConstIterator it = outputImage.domain().begin(),
219  itend = outputImage.domain().end(); it != itend; ++it)
220  {
221  val = minVal<unsigned char, MyImageC, Point>(imageC, *it, imageC.domain());
222  outputImage.setValue( *it , val);
223  }
224  else
225 
226  if (function == "mean")
227  for(MyImageC::Domain::ConstIterator it = outputImage.domain().begin(),
228  itend = outputImage.domain().end(); it != itend; ++it)
229  {
230  val = meanVal<unsigned char, MyImageC, Point>(imageC, *it, imageC.domain());
231  outputImage.setValue( *it , val );
232  }
233  else
234  trace.error() << "Bad function !"<<std::endl;
235 
236 
237  trace.endBlock();
238 
239  trace.beginBlock("Exporting...");
240  bool res = VolWriter< MyImageC>::exportVol(outputFileName, outputImage);
241  trace.endBlock();
242  if (res) return 0; else return 1;
243 }
int main(int argc, char **argv)
std::vector< Value >::const_iterator ConstIterator
typename Self::Domain Domain
typename Self::Point Point
static Self diagonal(Component val=1)
std::ostream & error()
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
DGtal::LinearOperator< Calculus, dim, duality, dim, duality > diagonal(const DGtal::KForm< Calculus, dim, duality > &kform)
Trace trace(traceWriterTerm)