Monday, July 11, 2011

Enhancement by Histogram Manipulation

The matrix of a grayscale image is just an ordered distribution of gray pixels (from 0 to 1) -- like the one below.


A dark grayscale image and the distribution of its gray pixels.
A dark image therefore has most of its pixels concentrated near 0, in the darker regions. The distinction between objects in the image are formed only by the contrast between adjacent pixels. If we maintain this, but have the pixels equally distributed from 0 to 1, we can produce a brighter image. To redistribute the pixels, we must examine the image's cumulative distribution function (CDF). See below.
The CDF of the original image to be converted into a y = x CDF (pixels are renumbered from 0-255 instead of 0.0-1.0).  
Notice that the CDF of the original image increases quickly at the first few dark gray pixels and slows down at the latter half. In other words, the greater number of pixels are concentrated in the darker region (near 0) and almost none at the light regions (near 1 or 255). We can change this distribution to a more uniform distribution by transforming the original CDF to a y = x CDF. This can be done by reassigning pixel numbers in the original image to new ones that would give the desired CDF. It's something like this:


that is, for each pixel value, match its CDF value with that of the desired CDF and determine its pixel value in that CDF. Mathematically, since you have the y value for the desired CDF (same value from the original one), you can solve for the x value of the new CDF by using its inverse. Here, I used a y = x CDF so the inverse would just be x = y or new pixel value = CDF value. Of course, I still had to apply normalization and convert it to the range 0-255 by rounding off. Shown below are the histograms and CDFs for the original and processed image:

The original and converted histogram for a y = x CDF.
Superimposed plots of the CDF of the original and transformed images.
The first part of the CDF look like steps because the frequency of pixels with certain pixel values are discrete. For the range 0-255, the pixels only had to be redistributed (compare CDF with transformed histogram). So each pixel value maintain the same frequency, but the pixel values themselves have to be changed to follow the  desired CDF. Notice that the histogram outlines are the same, only that the histogram of the transformed image is somewhat stretched.

And now, the picture:

Transformed image using a y=x CDF.
Brighter, no? :D

If we try using a y = x2 CDF (a bit steeper near the end), the image becomes

Transformed image using a y = x2 CDF.
a lot brighter! *.*

And here's the grayscale image manipulated using GIMP in pretty much the same process:


CDF of image easily manipulated 'manually' using GIMP.
I'm not very good at these kinds of things. -.-

So what's the best CDF to use? Hmm. I think this would depend on the image itself or perhaps on the person looking at the image -- a bit on the preference. Or in analogy to the way the eyes perceive colors, perhaps there are certain regions in the 0-255 grayscale that the eyes want to see more of. Actually, this technique may also be used for colored images as well. If we want to see more colors in a certain region of the spectrum, make the CDF steeper in that region. That is, concentrate the colors more on the desired regions by manipulating the histogram.








10 points because I learned A LOT. Minus everything for posting late. I'm sorry I'm late. >.<