DGtalTools  0.9.4
imgAddNoise.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 
49 
85 void missingParam ( std::string param )
86 {
87  trace.error() <<" Parameter: "<<param<<" is required..";
88  trace.info() <<std::endl;
89  exit ( 1 );
90 }
91 
93 
94 
95 int main( int argc, char** argv )
96 {
97  // parse command line ----------------------------------------------
98  po::options_description general_opt("Allowed options are: ");
99  general_opt.add_options()
100  ("help,h", "display this message")
101  ("input,i", po::value<std::string>(), "input image file name (any 2D image format accepted by DGtal::GenericReader)")
102  ("output,o", po::value<std::string>(), "output image file name (any 2D image format accepted by DGtal::GenericWriter)")
103  ("noise,n", po::value<double>()->default_value(0.5), "Kanungo noise level in ]0,1[ (default 0.5)") ;
104 
105  bool parseOK=true;
106  po::variables_map vm;
107  try{
108  po::store(po::parse_command_line(argc, argv, general_opt), vm);
109  }catch(const std::exception& ex){
110  trace.info()<< "Error checking program options: "<< ex.what()<< std::endl;
111  parseOK=false;
112  }
113  po::notify(vm);
114  if(vm.count("help")||argc<=1|| !parseOK)
115  {
116  trace.info()<< "Add Kanungo noise to a binary object with 0 values "
117  << "as background points and values >0 for the foreground ones."
118  <<std::endl << "Basic usage: "<<std::endl
119  << "\t imgAddNoi0se [options] --input <imageName> --output <outputImage>"
120  << "-noise 0.3" <<std::endl
121  << general_opt << "\n"
122  << "Example: \n"
123  << "imgAddNoise -i ${DGtal}/examples/samples/klokan.pgm -o noise.pgm "
124  << std::endl;
125 
126  return 0;
127  }
128 
129  //Parameters
130  if ( ! ( vm.count ( "input" ) ) ) missingParam ( "--input" );
131  const std::string input = vm["input"].as<std::string>();
132  if ( ! ( vm.count ( "output" ) ) ) missingParam ( "--output" );
133  const std::string output = vm["output"].as<std::string>();
134  const double noise = vm["noise"].as<double>();
135 
137  MyImage image = GenericReader<MyImage>::import( input );
138  trace.info() <<"Input image: "<< image<<std::endl;
139  Binarizer predicate(image, 0,255);
140 
141 
142  KanungoNoise<Binarizer, Z2i::Domain> kanungo(predicate, image.domain(), noise);
143 
144  MyImage result(image.domain());
145  for(Z2i::Domain::ConstIterator it = image.domain().begin(), itend = image.domain().end(); it!= itend; ++it)
146  {
147  if (kanungo(*it))
148  result.setValue(*it, 255);
149  else
150  result .setValue(*it, 0);
151  }
152 
153  result >> output;
154 
155  return 0;
156 }
157 
158 
159 
static TContainer import(const std::string &filename, std::vector< unsigned int > dimSpace=std::vector< unsigned int >())
Trace trace(traceWriterTerm)
std::ostream & info()
const Domain & domain() const
std::ostream & error()