Wednesday, July 1, 2009

ACTIVITY 4 Enhancement by Histogram Manipulation


For this activity, we tried to enhance grayscale images with poor contrast. For an 8-bit image, its gray levels can range from 1 to 256. However, those with poor contrast have gray levels which occupy only a small portion of the available range of values. To have a better understanding, we create a histogram of our image. Consider the probability distribution function (PDF) of the original image in Figure 1 (click on the images for a better view). Notice that it covers only the range 75-125 out of the 1-256 possible range. This implies that the image has poor contrast. Another way to show that the image has poor contrast is by creating a cumulative distribution function (CDF). If the CDF is linear from 1 to 256, then the gray levels of the image are of "uniform density" in the entire available range of values.

So, how did we enhance the poorly contrasted images? As mentioned above, the contrast of the image has something to do with its histogram. A narrow histogram indicates an image with poor contrast and low visibility while a widely distributed histogram means the image has good contrast and high visibility. Thus, by manipulating the histogram, we would be able to make some changes on the contrast of the image. For this activity, we will be using the technique histogram equalization. This technique allows the transformation of a narrow histogram into a widely distributed histogram. Widely distributed signifies an approximately uniform histogram. In doing so, we are also able to to stretch the dynamic range of gray levels of the image. This means the lighter pixels of the image can still be lighter and the relatively darker pixels can be even darker. Hence, the contrast of the image is improved or increased resulting into an enhanced image.

Basically, what we did is we assigned each pixel a new value; however, this value is dependent on the original value. The relation between the new value and the original value is based on the CDF. As mentioned above, a uniform histogram produces a linear CDF. So, we generated the CDF of the original image by getting the integral of its histogram and created a linear line with pixel values from 1 to 256 and CDF values from 0 to 1. The pixel value in the original image is mapped onto the equivalent pixel value in the linear line, i.e. same CDF value. The new image now contains information which produces a contrast-enhanced image, specifically histogram- equalized image. It must be noted that the assumption for this technique is that the information contained in the image is related to the probability of occurence of the pixel values as illustrated in the image histogram. I
t is expected that the histogram of the new image can be approximated as uniform with gray levels ranging from 1 to 256 and that the CDF is linear.

Listed below are the important lines of the code:

1.) Reads and shows the image.
1 image = imread('F:\Documents\AP186\activity 4\grayscale10.jpg');
2 subplot(331)
3 imshow((image-1)/255);
4 title('Original Image', 'fontsize', 3)

2.) Creates a histogram or probability distribution function (PDF) of the image.
5 s = size(image);
6 maximum = max(image);
7 minimum = min(image);
8 histogram = zeros(1,256);
9 for i = minimum:maximum,
10 var = (image==i)*1;
11 histogram(i) = sum(var);
12 end
13 histogram = histogram/(s(1)*s(2));
14 subplot(332)
15 plot2d((1:256),histogram);
16 title('PDF', 'fontsize', 3)

3.) Creates a cumulative distribution function (CDF) -- cumulative sum of the image histogram
17 cdf = cumsum(histogram);
18 cdf = cdf/max(cdf);
19 subplot(233)
20 plot2d((1:256),cdf)
21 title('CDF', 'fontsize', 3)

4.) Assigning of new values to the pixels using the linear CDF (equation of the line/relationship between the original value and the new value: Pixel Value = CDF Value *255 +1).
23 newimage = image;
24 for j = minimum:maximum,
25 old = cdf(j);
26 new = old*255 + 1;
27 w = find(image==j);
28 newimage(w)= new;
29 end

The histogram and CDF of the new image were established using the same lines listed above for (2) and (3). Figures 1, 2 and 3 show some examples illustrating the technique histogram equalization in order to enhace the contrast of a poorly contrasted image. The first row shows the orginal image and its histogram, as well as its CDF. In the second row is the new image, its histogram and CDF. As you can see, the histogram of the original image is initially narrow. Moreover, the CDF increases over a small range of values only. The image has poor contrast and low visibility; the details of the image are not that clear. After performing the histogram equalization technique, the light and dark pixels are now evident. This means the contrast of the image is enhanced, as well as its visibility. As expected, the dynamic range of pixel values or gray levels is increased occupying almost the entire available range of values. The histogram is not really perfectly uniform but more or less of uniform density. This is why an approximately linear CDF is generated.

Figure 1. http://homepages.inf.ed.ac.uk/rbf/HIPR2/histeq.htm


Figure 2. http://fourier.eng.hmc.edu/e161/lectures/contrast_transform/node3.html


Figure 3. http://homepages.inf.ed.ac.uk/rbf/HIPR2/histeq.htm

As an additional exercise, we were asked to create a CDF that can mimic the human eye response, which is nonlinear. I chose the logarithmic function to do this and the third row of the figures above shows the new image after the transformation. This time, the equation used is Pixel Value = EXP(LOG(256)*CDF Value) instead of the equation of the line to generate the new image. Note: Logarithmic is used in converting pixel value to CDF value, so, exponential must be used for CDF value to pixel value. It can be seen that image is now relatively darker as compared to the histogram-equalized image. The histogram is more or less uniform in the available range of values (widely distributed) but it also peaks on the dark portions (extremely low pixel values). This explains why the image is darker although the contrast of the image is still enhanced. The result makes sense since the mapping CDF used, which is logarithmic, is a function that discriminates brightness. As you can see in the generated CDF plot, the CDF value rapidly increases for low pixel vales and then the increase slows down at higher values.
Another nonlinear functions can be used to mimic human eye response like Gaussian. It is possible that these functions have a better effect in the image after transformation.

I would give myself a grade of 10 for this activity because I believe I performed well in enhancing a poorly contrasted image depending on the function (linear and logarithmic) used, as well as creating a histogram and CDF plot of the image. I am proud of myself because I was able to create a program with not much help from others. I would like to thank Mr. Luis C. Buno III, Mr. Jaya Combinido, Ms. Cherry Palomero, Mr. Miguel Sison and Mr. Jayson Villangca for answering some of my questions, which helped me a lot in successfully doing this activity.

Reference: Image Processing by Tinku Acharya and Ajoy K. Ray








1 comment: