diffusionVcycle.cc

  1 const char *help = "\
  2 progname: diffusionVcycle.cc\n\
  3 code2html: This program reads a pgm image (grayscale) and normalizes the lighting conditions according to the Gross and Brajovic algorithm (resolution with multigrid V-cycle).\n\
  4 \n\
  5 A diffusion process is applied to the input image, in order to find the luminance estimate.\n\
  6 The discretized version of the PDE describing the diffusion process is solved using\n\
  7 a multigrid v-cycle approach. The input image is then divided by the luminance estimate\n\
  8 so as to find the reflectance estimate (illumination free)\n\
  9 'Gross and Brajovic [2003] - Preprocessing algorithm for Illumination Invariant Face Recognition'\n\
 10 \n\
 11 version: Torch3 vision2.0, 2004-2005\n\
 12 (c) Guillaume Heusch (heusch@idiap.ch)\n";
 13 
 14 #include "ImageGray.h"
 15 #include "DiskXFile.h"
 16 #include "CmdLine.h"
 17 #include "ipVcycle.h"
 18 #include "ipHistoEqual.h"
 19 #include "Timer.h"
 20 
 21 using namespace Torch;
 22 
 23 int main(int argc, char **argv)
 24 {
 25   
 26   char *image_filename;
 27   char *out_filename;
 28  
 29   // relative importance of the smoothness constraint
 30   real lambda;
 31 
 32   // number of grids in the V-cycle
 33   int n_grids;
 34 
 35   /* type of diffusion performed (coefficients)
 36   - 0 = isotropic
 37   - 1 = inverse Weber contrast 
 38   */
 39   int type;
 40   
 41   bool verbose;
 42   bool light; // if you want to save the estimated light
 43   bool histo; // if you want to apply equalization on the normalized image
 44 
 45  
 46   // --------------------- COMMAND LINE -------------------------------------------------------------------------
 47   CmdLine cmd;
 48   cmd.setBOption("write log", false);
 49   cmd.info(help);
 50 
 51   cmd.addText("\nArguments:");
 52   cmd.addSCmdArg("image_filename", &image_filename, "input image filename");
 53   cmd.addSCmdArg("out_filename", &out_filename, "processed image filename");
 54 
 55   cmd.addText("\nOptions:");
 56   cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 57   cmd.addRCmdOption("-lambda", &lambda, 0.5, "relative importance of the smoothness constraint");
 58   cmd.addICmdOption("-n_grids", &n_grids, 5, "number of levels (WARNING: dependant on input image)");
 59   cmd.addICmdOption("-type", &type, 1, "type of diffusion: 0 = Isotropic, 1 = Anisotropic (Weber)");
 60   cmd.addBCmdOption("-light", &light, false, "saves the light estimate image");
 61   cmd.addBCmdOption("-histo", &histo, false, "performs histogram equalization on the result");
 62  
 63   cmd.read(argc, argv);
 64   
 65   Allocator *allocator = new Allocator;
 66   
 67   Timer timer;
 68 
 69   // ----------------------- LOAD IMAGE --------------------------------------------------------------------------
 70   DiskXFile *image_file = NULL;
 71   Image *image_in = NULL;
 72 
 73   image_in = new(allocator) ImageGray();
 74   image_in->setBOption("verbose", verbose);
 75 	
 76   image_file = new(allocator) DiskXFile(image_filename, "r");
 77   image_in->loadXFile(image_file);
 78 
 79   if(verbose)
 80     {
 81       print("Image info:\n");
 82       print("   width = %d\n", image_in->width);
 83       print("   height = %d\n", image_in->height);
 84       print("   format = %s (%d)\n", image_in->coding, image_in->n_planes);
 85     }
 86   
 87   
 88 
 89   // ---------------------- PROCESSING IMAGE MACHINE ------------------------------------------------------------	
 90   ipCore *vcycle = NULL;
 91   vcycle = new(allocator) ipVcycle(lambda, n_grids, type, image_in->width, image_in->height, "gray");
 92   vcycle->setBOption("verbose", verbose);
 93   vcycle->process(image_in);
 94 
 95   ipCore *histoEq = NULL;
 96    
 97   if (histo) {   
 98     histoEq = new(allocator) ipHistoEqual(image_in->width, image_in->height, "gray");
 99     histoEq->process(vcycle->seq_out);
100   }
101 
102 
103 
104   // ----------------------- SAVE IMAGE(S)  --------------------------------------------------------------------------
105   Image *image_out = NULL;
106   image_out = new(allocator) ImageGray();
107   image_out->setBOption("verbose", verbose);
108 
109    if (histo)
110     image_out->copyFrom(image_in->width, image_in->height, histoEq->seq_out->frames[0], "gray", 255);
111   else
112     image_out->copyFrom(image_in->width, image_in->height, vcycle->seq_out->frames[0], "gray", 255);
113  
114   image_out->updatePixmapFromData();
115   image_out->save(out_filename);
116 
117   print("time elapsed: %g\n", timer.getTime());
118 
119   if (light) {
120     image_out->copyFrom(image_in->width, image_in->height, vcycle->seq_out->frames[1], "gray", 255);
121     image_out->updatePixmapFromData();
122     image_out->save("light_estimate.pgm");
123   }
124   
125   delete allocator;
126   	
127   return(0);	
128 }