core/vil/algo/vil_histogram_equalise.cxx
Go to the documentation of this file.
00001 #include "vil_histogram_equalise.h"
00002 //:
00003 //  \file
00004 //  \brief Apply histogram equalisation to given image
00005 //  \author Tim Cootes
00006 
00007 #include <vil/algo/vil_histogram.h>
00008 
00009 
00010 //: Apply histogram equalisation to given image
00011 void vil_histogram_equalise(vil_image_view<vxl_byte>& image)
00012 {
00013   vcl_vector<double> histo(256);
00014   vil_histogram_byte(image,histo);
00015 
00016   // Create cumulative frequency curve
00017   double sum=0.0;
00018   for (unsigned i=0;i<256;++i) { sum+=histo[i]; histo[i]=sum; }
00019 
00020   // Parameters of mapping
00021   int lo = 0;
00022   // Find smallest value in image
00023   while (histo[lo]==0) lo++;
00024   double x0 = histo[lo];
00025   double s =255.1/(sum-x0);  // Smallest values get mapped to zero
00026 
00027   vcl_vector<vxl_byte> lookup(256);
00028   vxl_byte* lup = &lookup[0];
00029   for (unsigned i=0;i<256;++i) { lup[i]= vxl_byte(s*(histo[i]-x0)); }
00030 
00031   unsigned ni = image.ni(),nj = image.nj(),np = image.nplanes();
00032   vcl_ptrdiff_t istep=image.istep(),jstep=image.jstep(),pstep = image.planestep();
00033   vxl_byte* plane = image.top_left_ptr();
00034   for (unsigned p=0;p<np;++p,plane += pstep)
00035   {
00036     vxl_byte* row = plane;
00037     for (unsigned j=0;j<nj;++j,row += jstep)
00038     {
00039       vxl_byte* pixel = row;
00040       for (unsigned i=0;i<ni;++i,pixel+=istep) *pixel = lup[*pixel];
00041     }
00042   }
00043 }