vil_histogram_equalise.cxx
Go to the documentation of this file.
2 //:
3 // \file
4 // \brief Apply histogram equalisation to given image
5 // \author Tim Cootes
6 
8 #include <vil/vil_math.h>
9 
10 
11 //: Apply histogram equalisation to given byte image
13 {
14  std::vector<double> histo(256);
15  vil_histogram_byte(image,histo);
16 
17  // Create cumulative frequency curve
18  double sum=0.0;
19  for (unsigned i=0;i<256;++i) { sum+=histo[i]; histo[i]=sum; }
20 
21  // Parameters of mapping
22  int lo = 0;
23  // Find smallest value in image
24  while (histo[lo]==0) lo++;
25  double x0 = histo[lo];
26  double s =255.1/(sum-x0); // Smallest values get mapped to zero
27 
28  std::vector<vxl_byte> lookup(256);
29  vxl_byte* lup = &lookup[0];
30  for (unsigned i=0;i<256;++i) { lup[i]= vxl_byte(s*(histo[i]-x0)); }
31 
32  unsigned ni = image.ni(),nj = image.nj(),np = image.nplanes();
33  std::ptrdiff_t istep=image.istep(),jstep=image.jstep(),pstep = image.planestep();
34  vxl_byte* plane = image.top_left_ptr();
35  for (unsigned p=0;p<np;++p,plane += pstep)
36  {
37  vxl_byte* row = plane;
38  for (unsigned j=0;j<nj;++j,row += jstep)
39  {
40  vxl_byte* pixel = row;
41  for (unsigned i=0;i<ni;++i,pixel+=istep) *pixel = lup[*pixel];
42  }
43  }
44 }
45 
46 //: Apply histogram equalisation to given float image
48 {
49  unsigned n_bins = 4000;
50  std::vector<double> histo(n_bins);
51 
52  // Find smallest and largest values in image
53  float min_v, max_v;
54  vil_math_value_range(image, min_v, max_v);
55 
56  // Generate histogram
57  vil_histogram(image, histo, min_v, max_v, n_bins);
58 
59  // Create cumulative frequency curve
60  double sum = 0.0;
61  for (unsigned i=0; i<n_bins; ++i) { sum+=histo[i]; histo[i]=sum; }
62 
63  // Get mapping parameters and generate lookup tables
64  int lo = 0;
65  while (histo[lo]==0) lo++;
66  double x0 = histo[lo];
67  // To map bins to 256 output value range (smallest values gets mapped to zero)
68  double s_b2o =255.1/(sum-x0);
69  std::vector<unsigned> lookup_b2o(n_bins);
70  unsigned* lup_b2o = &lookup_b2o[0];
71  for (unsigned i=0;i<n_bins;++i) { lup_b2o[i]=unsigned(s_b2o*(histo[i]-x0)); }
72  // To map input values to bins
73  double s_i2b = double(n_bins-1)/(double(max_v-min_v));
74  std::vector<unsigned> lookup_i2b(max_v-min_v+1);
75  unsigned* lup_i2b = &lookup_i2b[0];
76  for (unsigned i=0;i<=(max_v-min_v);++i) { lup_i2b[i]=unsigned(s_i2b*i); }
77 
78  // Update image values
79  unsigned ni = image.ni(), nj = image.nj(), np = image.nplanes();
80  std::ptrdiff_t istep=image.istep(), jstep=image.jstep(), pstep = image.planestep();
81  float* plane = image.top_left_ptr();
82  for (unsigned p=0; p<np; ++p, plane+=pstep)
83  {
84  float* row = plane;
85  for (unsigned j=0;j<nj;++j,row += jstep)
86  {
87  float* pixel = row;
88  for (unsigned i=0; i<ni; ++i,pixel+=istep)
89  *pixel = lup_b2o[lup_i2b[(unsigned)(*pixel-min_v)]];
90  }
91  }
92 }
Apply histogram equalisation to images.
void vil_histogram_equalise(vil_image_view< vxl_byte > &image)
Apply histogram equalisation to given byte image.
Various mathematical manipulations of 2D images.
Concrete view of image data of type T held in memory.
Definition: vil_fwd.h:13
std::ptrdiff_t jstep() const
Add this to your pixel pointer to get next j pixel.
unsigned ni() const
Width.
unsigned nj() const
Height.
void vil_histogram(const vil_image_view< T > &image, std::vector< double > &histo, double min, double max, unsigned n_bins)
Construct histogram from pixels in given image.
Definition: vil_histogram.h:20
std::ptrdiff_t planestep() const
Add this to your pixel pointer to get pixel on next plane.
T * top_left_ptr()
Pointer to the first (top left in plane 0) pixel.
unsigned nplanes() const
Number of planes.
void vil_histogram_byte(const vil_image_view< vxl_byte > &image, std::vector< double > &histo)
Construct histogram from pixels in given image of bytes.
Construct histogram from pixels in given image.
std::ptrdiff_t istep() const
Add this to your pixel pointer to get next i pixel.
void vil_math_value_range(const vil_image_view< T > &view, T &min_value, T &max_value)
Compute minimum and maximum values over view.
Definition: vil_math.h:28