37 #include <boost/format.hpp>
41 #include "DGtal/base/Common.h"
42 #include "DGtal/helpers/StdDefs.h"
43 #include "DGtal/io/readers/GenericReader.h"
44 #include "DGtal/io/writers/GenericWriter.h"
188 using namespace DGtal;
190 int main(
int argc,
char* argv[] )
199 string inpainting_mask;
213 string scv {
"0xff0000"};
217 stringstream ssDescr;
218 ssDescr <<
"Computes a piecewise smooth approximation of a grey-level or color image, by optimizing the Ambrosio-Tortorelli functional (with u a 0-form and v a 1-form).";
219 ssDescr <<
"Usage: " << argv[0] <<
" -i toto.pgm\n"
220 <<
"Computes the Ambrosio-Tortorelli reconstruction/segmentation of an input image."
221 <<
"It outputs 2 or 3 images (of basename given by option --output) giving the"
222 <<
" reconstructed image u, and other images superposing u and the discontinuities v."
226 <<
" | a.(u-g)^2 + v^2 |grad u|^2 + le.|grad v|^2 + (l/4e).(1-v)^2 "
230 <<
"Discretized as (u 0-form, v 1-form, A vertex-edge bdry, B edge-face bdy)" << endl
231 <<
"E(u,v) = a(u-g)^t (u-g) + u^t A^t diag(v)^2 A^t u + l e v^t (A A^t + B^t B) v + l/(4e) (1-v)^t (1-v)" << endl
233 <<
"Example: ./at-u0-v1 -i ../Images/cerclesTriangle64b02.pgm -o tmp -a 0.05 -e 1 --lambda-1 0.1 --lambda-2 0.00001";
234 app.description(ssDescr.str());
238 app.add_option(
"-i,--input,1", f1,
"the input image PPM filename." )
240 ->check(CLI::ExistingFile);
241 app.add_option(
"--inpainting-mask,-m", inpainting_mask,
"the input inpainting mask filename." );
242 app.add_option(
"--output,-o", f2,
"the output image basename.",
true);
243 auto lambdaOpt = app.add_option(
"--lambda,-l",l,
"the parameter lambda.");
244 app.add_option(
"--lambda-1,-1",l1,
"the initial parameter lambda (l1).",
true);
245 app.add_option(
"--lambda-2,-2",l2,
"the final parameter lambda (l2).",
true );
246 app.add_option(
"--lambda-ratio,-q",lr,
"the division ratio for lambda from l1 to l2.",
true);
247 app.add_option(
"--alpha,-a",a,
"the parameter alpha.",
true);
248 auto epsOpt = app.add_option(
"--epsilon,-e",
"the initial and final parameter epsilon of AT functional at the same time.");
250 app.add_option(
"--epsilon-1",e1,
"the initial parameter epsilon.",
true);
251 app.add_option(
"--epsilon-2",e2,
"the final parameter epsilon.",
true);
252 app.add_option(
"--epsilon-r",er,
"sets the ratio between two consecutive epsilon values of AT functional.",
true);
254 app.add_option(
"--nbiter,-n",nbiter,
"the maximum number of iterations.",
true );
255 auto snrOpt = app.add_option(
"--image-snr", isnr,
"the input image without deterioration if you wish to compute the SNR.");
256 app.add_option(
"--pixel-size,-p", pix_sz,
"the pixel size for outputing images (useful when one wants to see the discontinuities v on top of u).",
true);
257 app.add_option(
"--color-v,-c",scv,
"the color chosen for displaying the singularities v (e.g. red is 0xff0000).",
true );
258 app.add_option(
"--verbose,-v", verb,
"the verbose level (0: silent, 1: less silent, etc).",
true );
260 app.get_formatter()->column_width(40);
261 CLI11_PARSE(app, argc, argv);
266 Color color_v( (
unsigned int) std::stoul( scv,
nullptr, 16 ), 255 );
267 if ( lambdaOpt->count()) l1 = l2 = l;
268 if ( l2 > l1 ) l2 = l1;
269 if ( lr <= 1.0 ) lr = sqrt(2);
270 if ( epsOpt->count() > 0 ){
274 bool snr = snrOpt->count() > 0;
277 bool color_image = f1.size() > 4 && f1.compare( f1.size() - 4, 4,
".ppm" ) == 0;
278 bool grey_image = f1.size() > 4 && f1.compare( f1.size() - 4, 4,
".pgm" ) == 0;
279 if ( ! color_image && ! grey_image )
281 trace.error() <<
"Input image file must be either a PGM (grey-level) or a PPM (color) image with these extensions."
291 typedef ImageContainerBySTLVector<Domain, Color> ColorImage;
292 typedef ImageContainerBySTLVector<Domain, unsigned char> GreyLevelImage;
296 trace.beginBlock(
"Reading PPM image");
297 ColorImage image = PPMReader<ColorImage>::importPPM( f1 );
299 trace.beginBlock(
"Building AT");
300 domain = image.domain();
301 K.init( domain.lowerBound(), domain.upperBound() - Point::diagonal( 1 ),
true );
303 AT.addInput( image, [] ( Color c ) ->
double {
return ((
double) c.red()) / 255.0; } );
304 AT.addInput( image, [] ( Color c ) ->
double {
return ((
double) c.green()) / 255.0; } );
305 AT.addInput( image, [] ( Color c ) ->
double {
return ((
double) c.blue()) / 255.0; } );
308 else if ( grey_image )
310 trace.beginBlock(
"Reading PGM image");
311 GreyLevelImage image = PGMReader<GreyLevelImage>::importPGM( f1 );
313 trace.beginBlock(
"Building AT");
314 domain = image.domain();
315 K.init( domain.lowerBound(), domain.upperBound() - Point::diagonal( 1 ),
true );
317 AT.addInput( image, [] (
unsigned char c ) {
return ((
double) c) / 255.0; } );
322 if ( snr && color_image )
324 trace.beginBlock(
"Reading ideal PPM image");
325 ColorImage image = PPMReader<ColorImage>::importPPM( isnr );
327 AT.addInput( image, [] ( Color c ) ->
double {
return ((
double) c.red()) / 255.0; }, true );
328 AT.addInput( image, [] ( Color c ) ->
double {
return ((
double) c.green()) / 255.0; }, true );
329 AT.addInput( image, [] ( Color c ) ->
double {
return ((
double) c.blue()) / 255.0; }, true );
331 else if ( snr && grey_image )
333 trace.beginBlock(
"Reading ideal PGM image");
334 GreyLevelImage image = PGMReader<GreyLevelImage>::importPGM( isnr );
336 AT.addInput( image, [] (
unsigned char c ) {
return ((
double) c) / 255.0; }, true );
341 Domain out_domain( pix_sz * domain.lowerBound(),
342 pix_sz * domain.upperBound() + Point::diagonal( pix_sz - 1) );
345 double g_snr = snr ? AT.computeSNR() : 0.0;
347 if ( inpainting_mask.size() > 0 )
349 string fm = inpainting_mask;
350 trace.beginBlock(
"Reading inpainting mask");
351 GreyLevelImage mask = GenericReader<GreyLevelImage>::import( fm );
353 Calculus::PrimalForm0 m( AT.calculus );
354 for ( Calculus::Index index = 0; index < m.myContainer.rows(); index++)
356 auto cell = m.getSCell( index );
357 double col = ((double) mask( K.sCoords( cell ) )) / 255.0;
358 m.myContainer( index ) = col > 0.0 ? 1.0 : 0.0;
364 ossGM << boost::format(
"%s-g-mask.pgm") %f2;
365 GreyLevelImage image_mg( domain );
366 Calculus::DualForm2 mg = AT.primal_h0 * functions::dec::diagonal( m ) * AT.getG( 0 );
367 functions::dec::form2ToGreyLevelImage
368 ( AT.calculus, mg, image_mg, 0.0, 1.0, 1 );
369 PGMWriter<GreyLevelImage>::exportPGM( ossGM.str(), image_mg );
371 else if ( color_image )
374 ossGM << boost::format(
"%s-g-mask.ppm") %f2;
375 ColorImage image_mg( domain );
376 Calculus::DualForm2 mg0 = AT.primal_h0 * functions::dec::diagonal( m ) * AT.getG( 0 );
377 Calculus::DualForm2 mg1 = AT.primal_h0 * functions::dec::diagonal( m ) * AT.getG( 1 );
378 Calculus::DualForm2 mg2 = AT.primal_h0 * functions::dec::diagonal( m ) * AT.getG( 2 );
379 functions::dec::threeForms2ToRGBColorImage
380 ( AT.calculus, mg0, mg1, mg2, image_mg, 0.0, 1.0, 1 );
381 PPMWriter<ColorImage, functors::Identity >::exportPPM( ossGM.str(), image_mg );
387 trace.info() << AT << std::endl;
392 trace.info() <<
"************ lambda = " << l1 <<
" **************" << endl;
394 for ( eps = e1; eps >= e2; eps /= er )
396 trace.info() <<
" ======= epsilon = " << eps <<
" ========" << endl;
397 AT.setEpsilon( eps );
400 trace.progressBar( n, nbiter );
404 n_v = AT.computeVariation();
405 }
while ( ( n_v > 0.0001 ) && ( ++n < nbiter ) );
406 trace.progressBar( n, nbiter );
407 trace.info() <<
"[#### last variation = " << n_v <<
" " << endl;
411 if ( verb > 0 ) trace.beginBlock(
"Writing u[0] as PGM image");
412 ostringstream ossU, ossV, ossW;
413 ossU << boost::format(
"%s-a%.5f-l%.7f-u.pgm") % f2 % a % l1;
414 ossV << boost::format(
"%s-a%.5f-l%.7f-u-v.pgm") % f2 % a % l1;
415 ossW << boost::format(
"%s-a%.5f-l%.7f-u-v.ppm") % f2 % a % l1;
416 Calculus::DualForm2 u = AT.primal_h0 * AT.getU( 0 );
417 Calculus::DualForm1 v = AT.primal_h1 * AT.getV();
419 GreyLevelImage image_u( domain );
420 functions::dec::form2ToGreyLevelImage
421 ( AT.calculus, u, image_u, 0.0, 1.0, 1 );
422 PGMWriter<GreyLevelImage>::exportPGM( ossU.str(), image_u );
424 GreyLevelImage image_uv( out_domain );
425 functions::dec::form2ToGreyLevelImage
426 ( AT.calculus, u, image_uv, 0.0, 1.0, pix_sz );
427 functions::dec::dualForm1ToGreyLevelImage
428 ( AT.calculus, v, image_uv, 0.0, 1.0, pix_sz );
429 PGMWriter<GreyLevelImage>::exportPGM( ossV.str(), image_uv );
431 ColorImage cimage( out_domain );
432 functions::dec::threeForms2ToRGBColorImage
433 ( AT.calculus, u, u, u, cimage, 0.0, 1.0, pix_sz );
434 functions::dec::dualForm1ToRGBColorImage
435 ( AT.calculus, v, cimage, color_v, 0.0, 1.0, pix_sz );
436 PPMWriter<ColorImage, functors::Identity >::exportPPM( ossW.str(), cimage );
437 if ( verb > 0 ) trace.endBlock();
439 else if ( color_image )
441 if ( verb > 0 ) trace.beginBlock(
"Writing u[0,1,2] as PGM image");
442 ostringstream ossU, ossV;
443 ossU << boost::format(
"%s-a%.5f-l%.7f-u.ppm") % f2 % a % l1;
444 ossV << boost::format(
"%s-a%.5f-l%.7f-u-v.ppm") % f2 % a % l1;
445 Calculus::DualForm2 u0 = AT.primal_h0 * AT.getU( 0 );
446 Calculus::DualForm2 u1 = AT.primal_h0 * AT.getU( 1 );
447 Calculus::DualForm2 u2 = AT.primal_h0 * AT.getU( 2 );
448 Calculus::DualForm1 v = AT.primal_h1 * AT.getV();
450 ColorImage image_u( domain );
451 functions::dec::threeForms2ToRGBColorImage
452 ( AT.calculus, u0, u1, u2, image_u, 0.0, 1.0, 1 );
453 PPMWriter<ColorImage, functors::Identity >::exportPPM( ossU.str(), image_u );
454 ColorImage image_uv( out_domain );
455 functions::dec::threeForms2ToRGBColorImage
456 ( AT.calculus, u0, u1, u2, image_uv, 0.0, 1.0, pix_sz );
457 functions::dec::dualForm1ToRGBColorImage
458 ( AT.calculus, v, image_uv, color_v, 0.0, 1.0, pix_sz );
459 PPMWriter<ColorImage, functors::Identity >::exportPPM( ossV.str(), image_uv );
460 if ( verb > 0 ) trace.endBlock();
465 double u_snr = AT.computeSNR();
466 trace.info() <<
"- SNR of u = " << u_snr <<
" SNR of g = " << g_snr << endl;
Aim: This class solves Ambrosio-Tortorelli functional in a plane for u a (vector of) 0-form(s) and v ...
DiscreteExteriorCalculus< 2, 2, LinearAlgebra > Calculus