DGtalTools 2.0.0
Loading...
Searching...
No Matches
at-u2-v0.cpp
1
32#include <iostream>
33
34#include <sstream>
35#include <string>
36#include <functional>
37#include <boost/format.hpp>
38
39
40#include "CLI11.hpp"
41
42
43#include "DGtal/base/Common.h"
44#include "DGtal/helpers/StdDefs.h"
45#include "DGtal/io/readers/GenericReader.h"
46#include "DGtal/io/writers/GenericWriter.h"
47
48#include "ATu2v0.h"
49
198using namespace std;
199using namespace DGtal;
200
201int main( int argc, char* argv[] )
202{
203 using namespace Z2i;
204
205 // parse command line ----------------------------------------------
206 CLI::App app;
207 string f1;
208 string f2 {"AT"};
209 string inpainting_mask;
210 double l;
211 double l1 {0.3125};
212 double l2 {0.0005};
213 double lr {sqrt(2)};
214 double a {1.0};
215 double epsilon;
216
217 double e1 {2.0};
218 double e2 {0.25};
219 double er {2.0};
220 int verb {0};
221 int nbiter = {10};
222 int pix_sz = {1};
223 string scv {"0xff0000"};
224 string isnr;
225 bool metric;
226
227
228 stringstream ssDescr;
229 ssDescr << "Computes a piecewise smooth approximation of a grey-level or color image, by optimizing the Ambrosio-Tortorelli functional (with u a 2-form and v a 0-form).";
230 ssDescr << "Usage: " << argv[0] << " -i toto.pgm\n"
231 << "Computes the Ambrosio-Tortorelli reconstruction/segmentation of an input image."
232 << "It outputs 2 or 3 images (of basename given by option --output) giving the"
233 << " reconstructed image u, and other images superposing u and the discontinuities v."
234 << endl << endl
235 << " / "
236 << endl
237 << " | a.(u-g)^2 + v^2 |grad u|^2 + le.|grad v|^2 + (l/4e).(1-v)^2 "
238 << endl
239 << " / "
240 << endl
241 << "Discretized as (u 2-form, v 0-form, A vertex-edge bdry, B edge-face bdy, M vertex-edge average)" << endl
242 << "E(u,v) = a(u-g)^t (u-g) + u^t B diag(M v)^2 B^t u + l e v^t A^t A v + l/(4e) (1-v)^t (1-v)" << endl
243 << endl
244 << "Example: ./at-u2-v0 -i ../Images/cerclesTriangle64b02.pgm -o tmp -a 0.05 -e 1 --lambda-1 0.1 --lambda-2 0.00001 -g"
245 << endl;
246
247 app.description(ssDescr.str());
248
249
250 app.add_option("-i,--input,1", f1, "the input image PPM filename." )
251 ->required()
252 ->check(CLI::ExistingFile);
253 app.add_option("--inpainting-mask,-m", inpainting_mask, "the input inpainting mask filename." );
254 app.add_option("--output,-o", f2, "the output image basename.");
255 auto lambdaOpt = app.add_option("--lambda,-l",l, "the parameter lambda.");
256 app.add_flag("--metric-average,-M",metric, "use metric average to smooth L1-metric." );
257
258 app.add_option("--lambda-1,-1",l1, "the initial parameter lambda (l1).");
259 app.add_option("--lambda-2,-2",l2, "the final parameter lambda (l2)." );
260 app.add_option("--lambda-ratio,-q",lr, "the division ratio for lambda from l1 to l2.");
261 app.add_option("--alpha,-a",a, "the parameter alpha.");
262 auto epsOpt = app.add_option("--epsilon,-e", "the initial and final parameter epsilon of AT functional at the same time.");
263
264 app.add_option("--epsilon-1",e1, "the initial parameter epsilon.");
265 app.add_option("--epsilon-2",e2, "the final parameter epsilon.");
266 app.add_option("--epsilon-r",er, "sets the ratio between two consecutive epsilon values of AT functional.");
267
268 app.add_option("--nbiter,-n",nbiter, "the maximum number of iterations." );
269 auto snrOpt = app.add_option("--image-snr", isnr, "the input image without deterioration if you wish to compute the SNR.");
270 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).");
271 app.add_option("--color-v,-c",scv, "the color chosen for displaying the singularities v (e.g. red is 0xff0000)." );
272 app.add_option("--verbose,-v", verb, "the verbose level (0: silent, 1: less silent, etc)." );
273
274 app.get_formatter()->column_width(40);
275 CLI11_PARSE(app, argc, argv);
276 // END parse comm"metric-average,Mand line using CLI ----------------------------------------------
277
278
279
280 Color color_v( (unsigned int) std::stoul( scv, nullptr, 16 ), 255 );
281 if ( lambdaOpt->count()) l1 = l2 = l;
282 if ( l2 > l1 ) l2 = l1;
283 if ( lr <= 1.0 ) lr = sqrt(2);
284 if ( epsOpt->count() > 0 ){
285 e1 = e2 = epsilon;
286
287 }
288 bool snr = snrOpt->count() > 0;
289
290
291 bool color_image = f1.size() > 4 && f1.compare( f1.size() - 4, 4, ".ppm" ) == 0;
292 bool grey_image = f1.size() > 4 && f1.compare( f1.size() - 4, 4, ".pgm" ) == 0;
293 if ( ! color_image && ! grey_image )
294 {
295 trace.error() << "Input image file must be either a PGM (grey-level) or a PPM (color) image with these extensions."
296 << endl;
297 return 2;
298 }
299
300 KSpace K;
301 ATu2v0< KSpace > AT( verb );
302 Domain domain;
303 AT.setMetricAverage( metric );
304
305 typedef ATu2v0<KSpace>::Calculus Calculus;
306 typedef ImageContainerBySTLVector<Domain, Color> ColorImage;
307 typedef ImageContainerBySTLVector<Domain, unsigned char> GreyLevelImage;
308 //---------------------------------------------------------------------------
309 if ( color_image )
310 {
311 trace.beginBlock("Reading PPM image");
312 ColorImage image = PPMReader<ColorImage>::importPPM( f1 );
313 trace.endBlock();
314 trace.beginBlock("Building AT");
315 domain = image.domain();
316 K.init( domain.lowerBound(), domain.upperBound(), true );
317 AT.init( K );
318 AT.addInput( image, [] ( Color c ) -> double { return ((double) c.red()) / 255.0; } );
319 AT.addInput( image, [] ( Color c ) -> double { return ((double) c.green()) / 255.0; } );
320 AT.addInput( image, [] ( Color c ) -> double { return ((double) c.blue()) / 255.0; } );
321 trace.endBlock();
322 }
323 else if ( grey_image )
324 {
325 trace.beginBlock("Reading PGM image");
326 GreyLevelImage image = PGMReader<GreyLevelImage>::importPGM( f1 );
327 trace.endBlock();
328 trace.beginBlock("Building AT");
329 domain = image.domain();
330 K.init( domain.lowerBound(), domain.upperBound(), true );
331 AT.init( K );
332 AT.addInput( image, [] (unsigned char c ) { return ((double) c) / 255.0; } );
333 trace.endBlock();
334 }
335
336 //---------------------------------------------------------------------------
337 if ( snr && color_image )
338 {
339 trace.beginBlock("Reading ideal PPM image");
340 ColorImage image = PPMReader<ColorImage>::importPPM( isnr );
341 trace.endBlock();
342 AT.addInput( image, [] ( Color c ) -> double { return ((double) c.red()) / 255.0; }, true );
343 AT.addInput( image, [] ( Color c ) -> double { return ((double) c.green()) / 255.0; }, true );
344 AT.addInput( image, [] ( Color c ) -> double { return ((double) c.blue()) / 255.0; }, true );
345 }
346 else if ( snr && grey_image )
347 {
348 trace.beginBlock("Reading ideal PGM image");
349 GreyLevelImage image = PGMReader<GreyLevelImage>::importPGM( isnr );
350 trace.endBlock();
351 AT.addInput( image, [] (unsigned char c ) { return ((double) c) / 255.0; }, true );
352 }
353
354 //---------------------------------------------------------------------------
355 // Prepare zoomed output domain
356 Domain out_domain( pix_sz * domain.lowerBound(),
357 pix_sz * domain.upperBound() + Point::diagonal( pix_sz ) );
358 //---------------------------------------------------------------------------
359 AT.setUFromInput();
360 double g_snr = snr ? AT.computeSNR() : 0.0;
361
362 if ( inpainting_mask.size() > 0 )
363 {
364 string fm = inpainting_mask;
365 trace.beginBlock("Reading inpainting mask");
366 GreyLevelImage mask = GenericReader<GreyLevelImage>::import( fm );
367 trace.endBlock();
368 Calculus::PrimalForm2 m( AT.calculus );
369 for ( Calculus::Index index = 0; index < m.myContainer.rows(); index++)
370 {
371 auto cell = m.getSCell( index );
372 double col = ((double) mask( K.sCoords( cell ) )) / 255.0;
373 m.myContainer( index ) = col > 0.0 ? 1.0 : 0.0;
374 }
375 AT.setAlpha( a, m );
376 AT.setUFromInputAndMask();
377 if ( grey_image )
378 {
379 ostringstream ossGM;
380 ossGM << boost::format("%s-g-mask.pgm") %f2;
381 GreyLevelImage image_mg( domain );
382 const Calculus::PrimalForm2 mg = functions::dec::diagonal( m ) * AT.getG( 0 );
384 ( AT.calculus, mg, image_mg, 0.0, 1.0, 1 );
385 PGMWriter<GreyLevelImage>::exportPGM( ossGM.str(), image_mg );
386 }
387 else if ( color_image )
388 {
389 ostringstream ossGM;
390 ossGM << boost::format("%s-g-mask.ppm") %f2;
391 ColorImage image_mg( domain );
392 const Calculus::PrimalForm2 mg0 = functions::dec::diagonal( m ) * AT.getG( 0 );
393 const Calculus::PrimalForm2 mg1 = functions::dec::diagonal( m ) * AT.getG( 1 );
394 const Calculus::PrimalForm2 mg2 = functions::dec::diagonal( m ) * AT.getG( 2 );
396 ( AT.calculus, mg0, mg1, mg2, image_mg, 0.0, 1.0, 1 );
397 PPMWriter<ColorImage, functors::Identity >::exportPPM( ossGM.str(), image_mg );
398 }
399 }
400 else
401 AT.setAlpha( a );
402
403 trace.info() << AT << std::endl;
404 double n_v = 0.0;
405 double eps = 0.0;
406 while ( l1 >= l2 )
407 {
408 trace.info() << "************ lambda = " << l1 << " **************" << endl;
409 AT.setLambda( l1 );
410 for ( eps = e1; eps >= e2; eps /= er )
411 {
412 trace.info() << " ======= epsilon = " << eps << " ========" << endl;
413 AT.setEpsilon( eps );
414 int n = 0;
415 do {
416 trace.progressBar( n, nbiter );
417 AT.solveU();
418 AT.solveV();
419 AT.checkV();
420 n_v = AT.computeVariation();
421 } while ( ( n_v > 0.0001 ) && ( ++n < nbiter ) );
422 trace.progressBar( n, nbiter );
423 trace.info() << "[#### last variation = " << n_v << " " << endl;
424 }
425 if ( grey_image )
426 {
427 if ( verb > 0 ) trace.beginBlock("Writing u[0] as PGM image");
428 ostringstream ossU, ossV, ossW;
429 ossU << boost::format("%s-a%.5f-l%.7f-u.pgm") % f2 % a % l1;
430 ossV << boost::format("%s-a%.5f-l%.7f-u-v.pgm") % f2 % a % l1;
431 ossW << boost::format("%s-a%.5f-l%.7f-u-v.ppm") % f2 % a % l1;
432 const Calculus::PrimalForm2 u = AT.getU( 0 );
433 const Calculus::PrimalForm1 v = AT.M01 * AT.getV();
434 // Restored image
435 GreyLevelImage image_u( domain );
437 ( AT.calculus, u, image_u, 0.0, 1.0, 1 );
438 PGMWriter<GreyLevelImage>::exportPGM( ossU.str(), image_u );
439 // Zoomed restored image with discontinuities (in black).
440 GreyLevelImage image_uv( out_domain );
442 ( AT.calculus, u, image_uv, 0.0, 1.0, pix_sz );
444 ( AT.calculus, v, image_uv, 0.0, 1.0, pix_sz );
445 PGMWriter<GreyLevelImage>::exportPGM( ossV.str(), image_uv );
446 // Zoomed restored image with discontinuities (in specified color).
447 ColorImage cimage( out_domain );
449 ( AT.calculus, u, u, u, cimage, 0.0, 1.0, pix_sz );
451 ( AT.calculus, v, cimage, color_v, 0.0, 1.0, pix_sz );
452 PPMWriter<ColorImage, functors::Identity >::exportPPM( ossW.str(), cimage );
453 if ( verb > 0 ) trace.endBlock();
454 }
455 else if ( color_image )
456 {
457 if ( verb > 0 ) trace.beginBlock("Writing u[0,1,2] as PGM image");
458 ostringstream ossU, ossV;
459 ossU << boost::format("%s-a%.5f-l%.7f-u.ppm") % f2 % a % l1;
460 ossV << boost::format("%s-a%.5f-l%.7f-u-v.ppm") % f2 % a % l1;
461 const Calculus::PrimalForm2 u0 = AT.getU( 0 );
462 const Calculus::PrimalForm2 u1 = AT.getU( 1 );
463 const Calculus::PrimalForm2 u2 = AT.getU( 2 );
464 const Calculus::PrimalForm1 v = AT.M01 * AT.getV();
465 // Restored image
466 ColorImage image_u( domain );
468 ( AT.calculus, u0, u1, u2, image_u, 0.0, 1.0, 1 );
469 PPMWriter<ColorImage, functors::Identity >::exportPPM( ossU.str(), image_u );
470 ColorImage image_uv( out_domain );
472 ( AT.calculus, u0, u1, u2, image_uv, 0.0, 1.0, pix_sz );
474 ( AT.calculus, v, image_uv, color_v, 0.0, 1.0, pix_sz );
475 PPMWriter<ColorImage, functors::Identity >::exportPPM( ossV.str(), image_uv );
476 if ( verb > 0 ) trace.endBlock();
477 }
478 // Compute SNR if possible
479 if ( snr )
480 {
481 double u_snr = AT.computeSNR();
482 trace.info() << "- SNR of u = " << u_snr << " SNR of g = " << g_snr << endl;
483 }
484 l1 /= lr;
485 }
486 return 0;
487}
void primalForm1ToGreyLevelImage(const Calculus &calculus, const typename Calculus::PrimalForm1 &v, Image &image, double cut_low=0.0, double cut_up=1.0, int pixel_size=1)
void form2ToGreyLevelImage(const Calculus &calculus, const AnyForm2 &u, Image &image, double cut_low=0.0, double cut_up=1.0, int pixel_size=1)
void threeForms2ToRGBColorImage(const Calculus &calculus, const AnyForm2 &u0, const AnyForm2 &u1, const AnyForm2 &u2, Image &image, double cut_low=0.0, double cut_up=1.0, int pixel_size=1)
void primalForm1ToRGBColorImage(const Calculus &calculus, const typename Calculus::PrimalForm1 &v, Image &image, Color color, double cut_low=0.0, double cut_up=1.0, int pixel_size=1)
DGtal::LinearOperator< Calculus, dim, duality, dim, duality > diagonal(const DGtal::KForm< Calculus, dim, duality > &kform)
Definition ATu0v1.h:57
Aim: This class solves Ambrosio-Tortorelli functional in a plane for u a (vector of) 2-form(s) and v ...
Definition ATu2v0.h:75
DiscreteExteriorCalculus< 2, 2, LinearAlgebra > Calculus