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>
44 using namespace DGtal;
47 namespace po = boost::program_options;
85 void missingParam ( std::string param )
87 trace.error() <<
" Parameter: "<<param<<
" is required..";
88 trace.info() <<std::endl;
92 typedef ImageSelector < Z2i::Domain, unsigned char>::Type MyImage;
95 int main(
int argc,
char** argv )
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)") ;
106 po::variables_map vm;
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;
114 if(vm.count(
"help")||argc<=1|| !parseOK)
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"
123 <<
"imgAddNoise -i ${DGtal}/examples/samples/klokan.pgm -o noise.pgm "
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>();
136 typedef functors::IntervalForegroundPredicate<MyImage> Binarizer;
137 MyImage image = GenericReader<MyImage>::import( input );
138 trace.info() <<
"Input image: "<< image<<std::endl;
139 Binarizer predicate(image, 0,255);
142 KanungoNoise<Binarizer, Z2i::Domain> kanungo(predicate, image.domain(), noise);
144 MyImage result(image.domain());
145 for(Z2i::Domain::ConstIterator it = image.domain().begin(), itend = image.domain().end(); it!= itend; ++it)
148 result.setValue(*it, 255);
150 result .setValue(*it, 0);