Home > Image Processing > 5. Image Enhancement by Histogram Manipulation

5. Image Enhancement by Histogram Manipulation

An image histogram is a representation of the frequency distribution of values in an image.  It plots the number of pixels there are containing particular tonal values.  Photographers oftentimes use histogram manipulation in order to restore the proper contrast of the images or simply depict a particular mood on the scenery.

If Px(i) is the image histogram for pixel value i, the cumulative distribution function (or CDF) for all pixel values is expressed as

Images are easily modified and best manipulated using cumulative frequencies.  Backprojection is referred to as the method of manipulating an image’s CDF to conform a desired distribution.  This is performed by tracing back the frequency of a particular pixel tone towards the desired CDF and then replace it with its new corresponding value.

Figure 1. Image histogram manipulation by means of backprojection.

For instance we have the image below as an example of a poor-contrast image, with its image histogram and CDF shown on the attached plots.

Figure 2. An example of a dark photograph (in grayscale). Courtesy of M. Beslac.

Using Scilab and the SIVP toolbox, we can improve the image by implementing histogram manipulation through the backprojection algorithm.  First, we use the built-in function imhist() to obtain the image histogram from the grayscale image.  We used plot2d3() to allow us to plot our data in vertical bars.

I = imread('fakefree.jpg');
J = rgb2gray(I);

[counts, cells] = imhist(J);      // defaults to 256 bins
scf(0); plot2d3(cells, counts, rect=[0,0, 256, max(counts)]);
 // rect=[xmin, ymin, xmax, ymax]

We then utilize the function cumsum(), which returns an array corresponding to the cumulative distribution. Note that in our implementation we perform a normalization for the CDF.

cumulative_sum = cumsum(counts);
cumsum_max = max(cumulative_sum);
cdf = cumulative_sum/cumsum_max;  // normalized cumsum

scf(1); plot2d(cells, cdf, rect=[0, 0, 256, max(cdf)]);

For better coding practices, we create a self-defined function to solve for the x-values of the traced frequencies on our CDF plot. If for instance we desire to imitate a linear function obeying the equation y = (1/255)*x, we design our function to do an inverse of this relation. (This code snippet of the function may be placed at the beginning of the code or in a separate file.)

function inv_linearCDF = invLinearCDF(y)
  y = y*255;                          // denormalization
  inv_linearCDF = round(y);           // linear_cdf = (1/255)*x

Note that we performed denormalization on the CDF in order to work with actual values, and used the function round() for discrete approximations.  Next, we create a second image matrix – a copy of the original – where we will later apply the changes due to our manipulation. Iterating towards each pixel value, we retrace the individual frequencies and replace them with new pixel values on the second image.

K = J;      // a copy of the original image

for i = 1:256
  new_x = invLinearCDF(cdf(i));
  K (find(J==i-1)) = new_x;    // i-1 referring to pixel values (range:0-255)

Below is the new image now approximately conforming to the linear distribution.

Figure 3. Histogram manipulated image via backprojection towards a linear CDF.

We can perform the same method with non-linear distributions such as the parabolic function, implemented similar to the one below.

function inv_parabolicCDF = invParabolicCDF(y)
  y = y*255^2;                         // denormalization
  inv_parabolicCDF = round(sqrt(y));   // parabolic_cdf(x) = x^2

We may also apply this with a logarithmic function:

function inv_logarithmicCDF = invLogarithmicCDF(y)
  y = y*log(255);                      // denormalization
  inv_logarithmicCDF = round(exp(y));  // logarithmic_cdf(x) = log(x)

The following are the image results of the previous two non-linear functions, including their manipulated histograms and cumulative frequencies. We can observe how the images are similar to an overexposed and underexposed image.

Figure 4. Image backprojected towards (1) a parabolic CDF; (2) a logarithmic CDF.

Photographers usually use image processing softwares to easily perform histogram manipulation on photographs. GIMP or the GNU Image Manipulation Program (a FOSS graphics editor), offers this capability via graphical interface.  To navigate to this function, go to Colors > Curves.  Make sure the image is initial set to grayscale by choosing Image > Mode > Grayscale.  Here is a snapshot of an actual curve manipulation on GIMP.

Figure 5. Desktop snapshot of GIMP running the curve manipulation feature.

Now, being able to adjust images via histogram manipulation, we can achieve improved images with better brightness and color contrast.

For this segment, I would rate myself 11 for understanding the important ideas and, in addition, for exploring a software that applies the image enhancement method.

[1] Esporlas, C., 2009. Histogram backprojection. Applied Physics 186.
[2] Photoxels, 2010. Digital photography tutorial: Histogram.
[3] Soriano, M., 2010. Image enhancement by histogram manipulation. Applied Physics 186.
[4] Wikipedia, 2010. Histogram equalization.
[5] Wikipedia, 2010. Image histogram.

  1. Arvin
    July 10, 2011 at 2:23 pm


    Do you have the source doe for this activity? If you do, can you send it to me?

    Thank you. You have a very interesting blog.

    • theiszm
      July 20, 2011 at 9:57 am

      Hi Arvin, I had the code sent to your email just now. Enjoy. 🙂

      • Carter
        March 3, 2015 at 8:00 am

        It seems to late but just I leave the comments
        Would you please send me source code?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: