27 #include <DGtal/base/Common.h>
28 #include "DGtal/io/readers/GenericReader.h"
29 #include "DGtal/io/writers/GenericWriter.h"
30 #include "DGtal/images/ImageContainerBySTLVector.h"
31 #include "DGtal/images/ImageSelector.h"
32 #include <DGtal/geometry/volumes/KanungoNoise.h>
33 #include <DGtal/images/IntervalForegroundPredicate.h>
34 #include <boost/program_options/options_description.hpp>
35 #include <boost/program_options/parsers.hpp>
36 #include <boost/program_options/variables_map.hpp>
39 #include <boost/pending/disjoint_sets.hpp>
45 using namespace DGtal;
48 namespace po = boost::program_options;
92 void missingParam(
const std::string ¶m )
94 trace.
error() <<
" Parameter: " << param <<
" is required..";
101 int main(
int argc,
char ** argv )
104 po::options_description general_opt(
"Allowed options are: " );
105 general_opt.add_options()(
"help,h",
"display this message" )(
106 "input,i", po::value<std::string>(),
107 "input image file name (any 3D image format accepted by "
108 "DGtal::GenericReader)" )(
"output,o", po::value<std::string>(),
109 "output image file name (any 3D image format "
110 "accepted by DGtal::GenericWriter)" )(
111 "noise,n", po::value<double>()->default_value( 0.5 ),
112 "Kanungo noise level in ]0,1[ (default 0.5)" )(
113 "max,m",
"Extract only the largest 6-connected component." );
116 po::variables_map vm;
119 po::store( po::parse_command_line( argc, argv, general_opt ), vm );
121 catch (
const std::exception & ex )
123 trace.
info() <<
"Error checking program options: " << ex.what()
128 if ( vm.count(
"help" ) || argc <= 1 || !parseOK )
130 trace.
info() <<
"Adds Kanungo noise to a binary object with 0 values as "
131 "background points and values >0 for the foreground ones."
133 <<
"Basic usage: " << std::endl
134 <<
"\t volAddNoi0se [options] --input <imageName> --output "
135 "<outputImage> -noise 0.3"
137 << general_opt <<
"\n";
142 bool MaxFlag = vm.count(
"max" );
144 if ( !( vm.count(
"input" ) ) )
145 missingParam(
"--input" );
146 const std::string input = vm[
"input" ].as<std::string>();
147 if ( !( vm.count(
"output" ) ) )
148 missingParam(
"--output" );
149 const std::string output = vm[
"output" ].as<std::string>();
150 const double noise = vm[
"noise" ].as<
double>();
154 trace.
info() <<
"Input image: " << image << std::endl;
155 Binarizer predicate( image, 0, 255 );
160 MyImage result( image.
domain() );
162 itend = image.
domain().end();
165 if ( kanungo( *it ) )
166 result.setValue( *it, 255 );
168 result.setValue( *it, 0 );
179 typedef std::map<Z3i::Point, std::size_t> Rank;
180 typedef std::map<Z3i::Point, Z3i::Point> Parent;
183 boost::associative_property_map<Rank> rankPMap( rankMap );
184 boost::associative_property_map<Parent> parentPMap( parentMap );
185 boost::disjoint_sets<boost::associative_property_map<Rank>,
186 boost::associative_property_map<Parent>>
187 dsets( rankPMap, parentPMap );
190 for (
auto e : result.domain() )
192 if ( result( e ) != 0 )
203 for (
auto e : result.domain() )
205 if ( result( e ) != 0 )
207 if ( result.domain().isInside( e + decx ) &&
208 ( result( e ) == result( e + decx ) ) )
209 dsets.union_set( e, e + decx );
211 if ( result.domain().isInside( e + decy ) &&
212 ( result( e ) == result( e + decy ) ) )
213 dsets.union_set( e, e + decy );
215 if ( result.domain().isInside( e + decz ) &&
216 ( result( e ) == result( e + decz ) ) )
217 dsets.union_set( e, e + decz );
224 std::map<Z3i::Point, unsigned int> sizes;
225 for (
auto p : result.domain() )
227 if ( result( p ) != 0 )
233 unsigned int maxElement = 0;
235 for (
auto i : sizes )
237 if ( maxElement < i.second )
239 maxElement = i.second;
243 trace.
info() <<
"Largest component has " << maxElement <<
" voxels."
251 trace.
info() <<
"Largest ref point: " << largest << std::endl;
252 for (
auto e : result.domain() )
254 if ( result( e ) != 0 )
256 if ( dsets.find_set( e ) != largest )
257 result.setValue( e, 0 );
void beginBlock(const std::string &keyword="")
static TContainer import(const std::string &filename, std::vector< unsigned int > dimSpace=std::vector< unsigned int >())
Trace trace(traceWriterTerm)
const Domain & domain() const