DGtalTools  1.2.0
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 
36 #include "CLI11.hpp"
37 
38 #include <vector>
39 #include <string>
40 #include <climits>
41 
42 
43 
44 using namespace DGtal;
45 
46 
85 void missingParam ( std::string param )
86 {
87  trace.error() <<" Parameter: "<<param<<" is required..";
88  trace.info() <<std::endl;
89  exit ( 1 );
90 }
91 
92 typedef ImageSelector < Z2i::Domain, unsigned char>::Type MyImage;
93 
94 
95 int main( int argc, char** argv )
96 {
97 // parse command line using CLI ----------------------------------------------
98  CLI::App app;
99  std::string inputFileName;
100  std::string outputFileName {"result.png"};
101  double noise {0.5};
102 
103  app.description("Add Kanungo noise to a binary object with 0 values as background points and values >0 for the foreground ones.\n Example: \n imgAddNoise ${DGtal}/examples/samples/klokan.pgm noise.pgm ");
104  app.add_option("-i,--input,1", inputFileName, "input image file name (any 2D image format accepted by DGtal::GenericReader)." )
105  ->required()
106  ->check(CLI::ExistingFile);
107  app.add_option("-o,--output,2", outputFileName, "output image file name (any 2D image format accepted by DGtal::GenericWriter)", true);
108  app.add_option("-n,--noise", noise, "Kanungo noise level in ]0,1[ (default 0.5)", true);
109 
110  app.get_formatter()->column_width(40);
111  CLI11_PARSE(app, argc, argv);
112  // END parse command line using CLI ----------------------------------------------
113 
114 
115  typedef functors::IntervalForegroundPredicate<MyImage> Binarizer;
116  MyImage image = GenericReader<MyImage>::import( inputFileName );
117  trace.info() <<"Input image: "<< image<<std::endl;
118  Binarizer predicate(image, 0,255);
119 
120  KanungoNoise<Binarizer, Z2i::Domain> kanungo(predicate, image.domain(), noise);
121 
122  MyImage result(image.domain());
123  for(Z2i::Domain::ConstIterator it = image.domain().begin(), itend = image.domain().end(); it!= itend; ++it)
124  {
125  if (kanungo(*it))
126  result.setValue(*it, 255);
127  else
128  result .setValue(*it, 0);
129  }
130 
131  result >> outputFileName;
132 
133  return EXIT_SUCCESS;
134 }
135 
136 
137 
Definition: ATu0v1.h:57