DGtalTools  0.9.4
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 <boost/program_options/options_description.hpp>
37 #include <boost/program_options/parsers.hpp>
38 #include <boost/program_options/variables_map.hpp>
39 
40 using namespace std;
41 using namespace DGtal;
42 using namespace Z3i;
43 
44 namespace po = boost::program_options;
45 
46 
47 
98 void missingParam ( std::string param )
99 {
100  trace.error() <<" Parameter: "<<param<<" is required..";
101  trace.info() <<std::endl;
102  exit ( 1 );
103 }
104 
105 template<typename Val, typename Image, typename Point, typename Domain>
106 Val maxVal(Image &image, Point &p, Domain& domain)
107 {
108  typename Image::Domain dom( p*2, p*2 + Point::diagonal(1));
109  Val v=image(p*2);
110  for(typename Image::Domain::ConstIterator it=dom.begin(), itend=dom.end();
111  it != itend;
112  ++it)
113  if ( domain.isInside(*it) && image( *it) > v) v=image(*it);
114 
115  return v;
116 }
117 template<typename Val, typename Image, typename Point, typename Domain>
118 Val minVal(Image &image, Point &p, Domain& domain)
119 {
120  typename Image::Domain dom( p*2, p*2 + Point::diagonal(1));
121  Val v=image(p*2);
122  for(typename Image::Domain::ConstIterator it=dom.begin(), itend=dom.end();
123  it != itend;
124  ++it)
125  if ( domain.isInside(*it)&& image( *it) < v) v=image(*it);
126 
127  return v;
128 }
129 template<typename Val, typename Image, typename Point, typename Domain>
130 Val meanVal(Image &image, Point &p, Domain& domain)
131 {
132  typename Image::Domain dom( p*2, p*2 + Point::diagonal(1));
133  int v=0;
134  int nb=0;
135  for(typename Image::Domain::ConstIterator it=dom.begin(), itend=dom.end();
136  it != itend;
137  ++it)
138  if ( domain.isInside(*it) )
139  {
140  nb++;
141  v+=image(*it);
142  }
143  return static_cast<unsigned char>( v/nb );
144 }
145 
146 
147 int main(int argc, char**argv)
148 {
149 
150  // parse command line ----------------------------------------------
151  po::options_description general_opt ( "Allowed options are: " );
152  general_opt.add_options()
153  ( "help,h", "display this message." )
154  ( "input,i", po::value<std::string>(), "Input vol file." )
155  ( "output,o", po::value<string>(),"Output filename." )
156  ("function,f", po::value<string>()->default_value("mean"), "Function used to the down-sampling: {none,max, min, mean}" );
157 
158 
159  po::variables_map vm;
160  po::store ( po::parse_command_line ( argc, argv, general_opt ), vm );
161  po::notify ( vm );
162  if ( vm.count ( "help" ) ||argc<=1 )
163  {
164  trace.info() << "Brutally sub sample a vol file (division by 2 in each direction)."<<std::endl
165  << std::endl << "Basic usage: "<<std::endl
166  << "\tvolSubSample --input <volFileName> --o <volOutputFileName> "<<std::endl
167  << general_opt << "\n";
168  return 0;
169  }
170 
171  //Parse options
172  if ( ! ( vm.count ( "input" ) ) ) missingParam ( "--input" );
173  std::string filename = vm["input"].as<std::string>();
174  std::string function = vm["function"].as<std::string>();
175  if ( ! ( vm.count ( "output" ) ) ) missingParam ( "--output" );
176  std::string outputFileName = vm["output"].as<std::string>();
177 
178 
179  trace.beginBlock("Loading file");
181 
182  MyImageC imageC = VolReader< MyImageC >::importVol ( filename );
183  MyImageC outputImage( Z3i::Domain( imageC.domain().lowerBound(),
184  (imageC.domain().upperBound()-imageC.domain().lowerBound())/Vector().diagonal(2)));
185 
186  trace.endBlock();
187  Point subvector = Vector().diagonal(2);
188  Point p;
189  unsigned char val;
190  trace.beginBlock("Down-scaling the volume...");
191  trace.info()<<"Function= "<<function<<std::endl;
192  trace.info() << outputImage.domain() << std::endl;
193  //Fast Copy
194  if (function == "none")
195  for(MyImageC::Domain::ConstIterator it = outputImage.domain().begin(),
196  itend = outputImage.domain().end(); it != itend; ++it)
197  {
198  p = (*it) * 2;
199  outputImage.setValue( *it , imageC( p ));
200  }
201  else
202  if (function == "max")
203  for(MyImageC::Domain::ConstIterator it = outputImage.domain().begin(),
204  itend = outputImage.domain().end(); it != itend; ++it)
205  {
206  val = maxVal<unsigned char, MyImageC, Point>(imageC, *it, imageC.domain());
207  outputImage.setValue( *it , val );
208  }
209  else
210  if (function == "min")
211  for(MyImageC::Domain::ConstIterator it = outputImage.domain().begin(),
212  itend = outputImage.domain().end(); it != itend; ++it)
213  {
214  val = minVal<unsigned char, MyImageC, Point>(imageC, *it, imageC.domain());
215  outputImage.setValue( *it , val);
216  }
217  else
218 
219  if (function == "mean")
220  for(MyImageC::Domain::ConstIterator it = outputImage.domain().begin(),
221  itend = outputImage.domain().end(); it != itend; ++it)
222  {
223  val = meanVal<unsigned char, MyImageC, Point>(imageC, *it, imageC.domain());
224  outputImage.setValue( *it , val );
225  }
226  else
227  trace.error() << "Bad function !"<<std::endl;
228 
229 
230  trace.endBlock();
231 
232  trace.beginBlock("Exporting...");
233  bool res = VolWriter< MyImageC>::exportVol(outputFileName, outputImage);
234  trace.endBlock();
235  if (res) return 0; else return 1;
236 }
void beginBlock(const std::string &keyword="")
static Self diagonal(Component val=1)
STL namespace.
double endBlock()
Trace trace(traceWriterTerm)
std::ostream & info()
typename Self::Point Point
std::vector< Value >::const_iterator ConstIterator
std::ostream & error()
typename Self::Domain Domain