Sunday, June 29, 2008

Activity 4: Image Enhancement by Histogram Manipulation

For this activity, I used the following image from : http://www.inkjetart.com/4990/48bit/Tetons_before2.jpg
The normalized Image Histogram corresponding to the grayscale probability distribution(PDF), shown below shows that the image has poor contrast since its histogram is concentrated along higher pixel values. Also shown below is the cumulative distribution function (CDF) obtained by getting the cumulative sum of the Y axis of the normalized histogram.

The Following code was used to obtain the PDF, CDF of the original image and also to perform backprojection into the new image as well as to obtain its corresponding CDF and PDF.
-----------------------------------------------------------------
I = gray_imread("C:\Users\RAFAEL JACULBIA\Documents\subjects\186\activity4\\final\tetons_before2.bmp")*255;

Isize = size(I);
//histogram routine
x = matrix(0:255,[1,256]);
y = x.*0;

for i = 1:Isize(1)
for j = 1:Isize(2)
y(I(i,j)+1) = y(I(i,j)+1)+1;
end
end
h = scf(1) //show image
imshow(I/max(I))

h=scf(2) //plot histogram
y = y/sum(y);
plot2d3(x, y)
//end histogram routine

ys = cumsum(y)/max(cumsum(y));
h=scf(3) //plot cdf
plot(x,ys)

//image enhancement
for i=1:Isize(1)
for j = 1:Isize(2)
newI(i,j)=ys(I(i,j)); //match y values of cdf of Image with enhanced image
end
end
//end image enhancement

imwrite(newI,"C:\Users\RAFAEL JACULBIA\Documents\subjects\186\activity4\final\newI.bmp");
newI = gray_imread("C:\Users\RAFAEL JACULBIA\Documents\subjects\186\activity4\final\newI.bmp")*255;

h = scf(4)
imshow(newI/max(newI))

newX = matrix(0:255,[1,256]);
newY = newX.*0;
sizenew = size(newI);
for i = 1:sizenew(1)
for j = 1:sizenew(2)
newY(newI(i,j)+1) = newY(newI(i,j)+1)+1;
end
end
h = scf(5)
newY = newY/sum(newY);
plot2d3(newX, newY)

newYs = cumsum(newY)/max(cumsum(newY));
h=scf(6)
plot(newX,newYs)

-------------------------------------------------------------------


After backprojection the following image is obtained. Original image (left) was included for comparison.
The PDF and CDF obtained from the enhanced image is given below:
It can be clearly seen from that the histogram is well spread suggesting a good contrast image. The CDF also is a straight line which is another indicator of a good contrast image. The small deviations of the CDF from being a perfectly straight line may have been due to the rounding off of values when the image when it was converted to grayscale using gray_imread(image)*255, the same result is obtained if I use imread(image) and convert to grayscale using im2gray(image)*255.

If we use specified final CDF, the enhancement part of the code will be changed as follows:
----------------------------------------------------
z = [0:255];
g = (tanh(5*(z-128)/255))+1;
g = g/max(g);

for i=1:Isize(1)
for j = 1:Isize(2)
sub = abs(ys(I(i,j))-g);
sub = find(sub==min(sub),1);
newI(i,j) = sub;
end
end
------------------------------------------------------

After the code is implemented, the following image is obtained. Left is the original image center is for linear CDF and rightmost is for the hyperbolic tangent CDF.

For this image, the following PDF and CDF is obtained. For the above CDF, black is the desired CDF and blue is the obtained CDF. Adjusting the k value from 5 to 8 gives a better fit of the desired CDF, as shown below.

In my observation, the linear response CDF provides a higher contrast compared with the nonlinear CDF. However the linear CDF sacrifices some detail on the image, for instance the leaves near the edge is more clear for the nonlinear CDF.

I will give myself a score of 10 for this activity since I was able to have a good image enhancement.

Many thanks to the following people who directly or indirectly, helped me a lot in this activity.
Eduardo David
Abraham Latimer Camba
Jeric Tugaff
Elizabeth Ann Prieto

No comments: