Wednesday, July 3, 2013

Activity 6 - Enhancement by Histogram Manipulation

This activity, as the title suggests, involves playing around with the histogram of a grayscale image to correct or enhance the image. In our case, we chose a dark image and we want to enhance the image by correcting the brightness and contrast levels so that more detail will be visible.

So here's what I did. First I chose my image. I have a sunset image of Dresden, Germany (from my adviser Dr. Rene Batac who's taking up his postdoc there) as shown below:
Figure 1. Sunset in Dresden. My adviser sent this to me
because he knew how much I loved sunsets, and it was a
particularly beautiful sunset.

As you can see, the foreground is dark while the background is light, and so I cropped the image to only select the lower half of the sunset image,
Figure 2. Lower half of the sunset image above. The foliage
and a few buildings are barely visible in this half.
and used this image for the activity today. The goal now is to adjust the brightness and contrast levels to make the foreground visible.

To enhance my image, here are the steps I did. First, I converted the image to grayscale using Scilab and took the histogram of the gray values. Normalizing the histogram, I would obtain the probability density function (PDF) of my image. Strictly speaking, a PDF describes the probability that a variable may take on a specific value, but here the PDF of the image describes the likelihood that a gray value (from 0-255) will appear on the image. As we can see in the images below, the PDF peaks around the lower values, signifying that the image is mostly dark-pixeled. Another related graph is the cumulative distribution function (CDF) of the image, which describes the likelihood that a gray value will be less than or equal to some given gray value.
For my image, the CDF is rapidly increasing along low gray values and tapers of at 1. This is expected, as the probability cannot be more than 1.
Figure 3. from left to right: (a) histogram; (b) PDF; and (c) CDF of the grayscaled
image shown in Figure 2. The gray values are concentrated at the lower end.
The next task is to correct the image by applying a linear CDF onto the original image. To do this, I first created a uniformly distributed PDF and took the corresponding CDF using Scilab. For each CDF value in the image, I need to find the corresponding image pixel in the linear CDF. My first idea was to use find() and return the index of the CDF value. However this does not work, as the CDF values in the image and in the linear CDF I constructed do not correspond. The trick is to use the mathematical equations and treat the CDF as a continuous function, rather than a matrix.