Histogram Equalization (HE)

 

 

Images having high contrast are corrected by HE technique. This technique provides histogram spectrum to spread colors equally either in entire image or in particular region of image. HE makes dark colors even darker and bright colors more bright approaching to extreme colors (0 darkest or 255 brightest). Darkening is only valid for little dark colors. HE technique is formulated in equation (1) and (2).

 

                                                                       (1)

 

                                                                 (2)

 

            n: Total number of pixels, (# of rows * # of columns)

            nj: j. histogram equivalent of a pixel color.

            Sk: Ratio of usage from zero (black) color to k. color position.
            : New pixel value calculated by histogram equalization algorithm.

 

 

               

(a)                                                                              (b)

 

                 

(c)                                                                              (d)

Figure 1. Pills (a) original image, (b) original histogram, (c) HE applied image, (d) histogram.

 

 

                 

(a)                                                                  (b)

Figure 2. Image baboon, (a) before HE, (b) after HE.

 

 

 

C Source Code

 

            Below there is only histogram equalization procedure. The complete program can be found here.

 

/********************************************/
/*  HISTOGRAM EQUALIZATION of A GRAY IMAGE  */
/********************************************/
int Hist_Eq(unsigned long hist[], long width, long height)
{

   double s_hist_eq[256]={0.0}, sum_of_hist[256]={0.0};
   long i, j, k, n, state_hst[256]={0};

   n = width * height;

   for (i=0;i<256;i++)  // pdf of image
   {
      s_hist_eq[i] = (double)hist[i]/(double)n;
   }
   sum_of_hist[0] = s_hist_eq[0];
   for (i=1;i<256;i++)        // cdf of image
   {
      sum_of_hist[i] = sum_of_hist[i-1] + s_hist_eq[i];
   }

   for(i=0;i<height;i++)
   {
      for(j=0;j<width;j++)
      {
         k = img_data[i][j];
         img_data[i][j] = (unsigned char)round( sum_of_hist[k] * 255.0 );
      }
   }

   return 0;
}

 
Copyright by Chasan Chouse