Tuesday, June 30, 2009

Activity 4 - Image Enhancement by Histogram Manipulation

Histogram manipulation is an image processing method that is used to enhance the contrast and improve the quality of an image. [1]

In this activity, a poor contrast image found in the web is to be enhanced by manipulating a property
. Initially, the image has poor contrast, and the details are not well resolved.

Original image [2]



Left: Histogram of original image. Right: Cumulative distribution function of original image. We can see that in the histogram, the grayscale values of the pixels vary only over a small range, that is why the image is mostly gray and has poor contrast. The CDF is non-linear with a high gradient in the middle, which means the values of the histogram are mostly in the middle.

To enhance this image, we will tinker with its histogram, or its Probability Density Function (PDF). The Cumulative Distribution Function (CDF) will be projected to a desired CDF to get the desired grayscale values for a pixel. This technique is called histogram backprojection. In this technique, the desired CDF serves as a lookup table for manipulation of the histogram. [3]

Histogram Backprojection
To do this, pixel per pixel, we get the grayscale value of the original image. The value of the CDF for this grayscale value is checked in the desired CDF, and the corresponding desired grayscale value for that CDF value replaces the original grayscale value in the image.


Step by step, this is how histogram backprojection is implemented:
  1. For each pixel, get the grayscale value and obtain its CDF value.
  2. Get the desired grayscale value for this CDF value from the desired CDF. This can be done when you know the inverse function of your desired CDF. Initially, the desired CDF is linear.
  3. Replace the grayscale value of the current pixel by the calculated desired grayscale value.
*Note: If the inverse is not an integer, it can be rounded off to the nearest integer.

The result:

Before and after Histogram Backprojection. The contrast is better, and the details are now clear. The outline of the door of the car in the foreground is clearer, as well as the details of the car in the middle and the details on the wall.


Histogram and Cumulative Distribution Function of processed image. Now, the histogram is well-spread. The values are not concentrated in the middle, and the range of possible grayscale values include the extreme values.The backprojection was correctly implemented because the desired linear CDF was achieved.



Nonlinear Cumulative Distribution Function
Next, a non-linear CDF is now desired. The same procedure as above was done for the desired non-linear CDF. For this example, the desired CDF was y = x^2.

Parabolic desired CDF. The values are higher in the white region than in the black region, which should result in a whiter image.

This equation is quickly increasing for the higher grayscale values, so we expect this to be whiter than the original image as well as the image processed with a linear desired CDF.


Before and after Histogram Backprojection for parabolic desired CDF. The resulting image is whiter than the original image, as well as the previous resulting image. This is because a parabolic CDF means the desired histogram is more concentrated on the white region, resulting in a white image.


Histogram and Cumulative Distribution Function of processed image. Again, the histogram is now spread over the possible grayscale values, and have more values in the right side of the plot (whiter region). The CDF matched the desired CDF.

Taking the histogram of the resulting image shows that indeed, there are more pixels with high grayscale values (values in the whiter region). The CDF matches that of the desired parabolic CDF, which means the procedure was done correctly.

I give myself 10 points for this activity. The images were enhanced using backprojection, and the resulting images had more contrast than the original one. The program can be used to manipulate images using any desired Cumulative Distribution Function.

I thank Miguel Sison and Jaya Combinido for useful discussions.


The code:
Img = round(gray_imread("C:\Documents and Settings\Yayay\My Documents\AP186\Activity 4\cargray.jpg")*256);

scf(1);
imshow(Img,[]);

[x,y] = size(Img);
img = matrix(Img, 1 ,x*y);

// histogram and CDF of image
xvals = linspace(0,255,256);
f = tabul(img);
f(:,1)=f(:,1)+1; //removes zero as an index in next calculations
hist = zeros(1,256);
for i=1:length(f(:,1)), hist(f(i,1)) = f(i,2), end;
CDF = cumsum(hist);
scf(2);
plot(xvals, hist);
scf(3);
plot(xvals,CDF);

// equation for inverse of ideal function
function invCDF = invIdeal(px)
invCDF = px^(0.5);
endfunction

// histogram backprojection
for i=1:(x*y),
px = round(invIdeal(CDF(img(i))));
img(i) = px;
end;
img = round(img*255/max(img)); //resizing x-axis to 0-255

scf(4);
newimg = matrix(img, x,y);
imshow(newimg,[]);

// histogram and CDF of new image
fnew = tabul(img);
histnew = zeros(1,256);
fnew(:,1)=fnew(:,1)+1;
for j=1:length(fnew(:,1)),
histnew(fnew(j,1)) = fnew(j,2);
end;
CDFnew = cumsum(histnew);
scf(5);
plot(xvals, histnew);
scf(6);
plot(xvals, CDFnew);


[1] A4 - Image Enhancement by Histogram Manipulation 2009.pdf (by Dr. Maricor Soriano)
[2]
http://www.generation5.org/content/2004/histogramEqualization.asp
[3] http://en.wikipedia.org/wiki/Histogram_equalization

0 comments:

Post a Comment