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