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