DGtalTools  0.9.2
volAddNoise.cpp
1 
28 #include <DGtal/base/Common.h>
29 #include "DGtal/io/readers/GenericReader.h"
30 #include "DGtal/io/writers/GenericWriter.h"
31 #include "DGtal/images/ImageContainerBySTLVector.h"
32 #include "DGtal/images/ImageSelector.h"
33 #include <DGtal/geometry/volumes/KanungoNoise.h>
34 #include <DGtal/images/IntervalForegroundPredicate.h>
35 #include <boost/program_options/options_description.hpp>
36 #include <boost/program_options/parsers.hpp>
37 #include <boost/program_options/variables_map.hpp>
38 
39 #include <vector>
40 #include <string>
41 #include <climits>
42 
43 
44 using namespace DGtal;
45 
47 namespace po = boost::program_options;
48 
90 void missingParam ( std::string param )
91 {
92  trace.error() <<" Parameter: "<<param<<" is required..";
93  trace.info() <<std::endl;
94  exit ( 1 );
95 }
96 
97 typedef ImageSelector < Z3i::Domain, unsigned char>::Type MyImage;
98 
99 
100 int main( int argc, char** argv )
101 {
102  // parse command line ----------------------------------------------
103  po::options_description general_opt("Allowed options are: ");
104  general_opt.add_options()
105  ("help,h", "display this message")
106  ("input,i", po::value<std::string>(), "input image file name (any 3D image format accepted by DGtal::GenericReader)")
107  ("output,o", po::value<std::string>(), "output image file name (any 3D image format accepted by DGtal::GenericWriter)")
108  ("noise,n", po::value<double>()->default_value(0.5), "Kanungo noise level in ]0,1[ (default 0.5)") ;
109 
110  bool parseOK=true;
111  po::variables_map vm;
112  try{
113  po::store(po::parse_command_line(argc, argv, general_opt), vm);
114  }catch(const std::exception& ex){
115  trace.info()<< "Error checking program options: "<< ex.what()<< std::endl;
116  parseOK=false;
117  }
118  po::notify(vm);
119  if(vm.count("help")||argc<=1|| !parseOK)
120  {
121  trace.info()<< "Adds Kanungo noise to a binary object with 0 values as background points and values >0 for the foreground ones." <<std::endl << "Basic usage: "<<std::endl
122  << "\t volAddNoi0se [options] --input <imageName> --output <outputImage> -noise 0.3"<<std::endl
123  << general_opt << "\n";
124  return 0;
125  }
126 
127  //Parameters
128  if ( ! ( vm.count ( "input" ) ) ) missingParam ( "--input" );
129  const std::string input = vm["input"].as<std::string>();
130  if ( ! ( vm.count ( "output" ) ) ) missingParam ( "--output" );
131  const std::string output = vm["output"].as<std::string>();
132  const double noise = vm["noise"].as<double>();
133 
134  typedef functors::IntervalForegroundPredicate<MyImage> Binarizer;
135  MyImage image = GenericReader<MyImage>::import( input );
136  trace.info() <<"Input image: "<< image<<std::endl;
137  Binarizer predicate(image, 0,255);
138 
139 
140  KanungoNoise<Binarizer, Z3i::Domain> kanungo(predicate, image.domain(), noise);
141 
142  MyImage result(image.domain());
143  for(Z3i::Domain::ConstIterator it = image.domain().begin(), itend = image.domain().end(); it!= itend; ++it)
144  {
145  if (kanungo(*it))
146  result.setValue(*it, 255);
147  else
148  result.setValue(*it, 0);
149  }
150 
151  result >> output;
152 
153  return 0;
154 }
155 
156 
157