Monday, July 6, 2009

Activity 4: Enhancement by Histogram Manipulation

For this activity, a poor contrast image is enhanced by histogram manipulation by following the steps below. The code (if applicable) used and the results accompany the steps.

1. Get an image from the internet or from own collection. Crop if necessary.

I=imread('C:\Documents and Settings\mimie\Desktop\New Folder (2)\activity 4 images\crop.bmp');

2. Change image to grayscale and get its histogram and corresponding Cumulative Distribution Function (CDF).
I=im2gray(I);
gcrop=I/max(I);
imwrite(gcrop, "C:\Documents and Settings\mimie\Desktop\New Folder (2)\activity 4 images\gcrop.bmp");

xsize=size(I,1);
ysize=size(I,2);

x = matrix(0:255,[1,256]);
y = x.*0;
a = 1;

for i = 0:255
m = find(I== i);
y(a) = length(m);
a = a + 1;
end

cs=cumsum(y);
csnorm=cs/max(cs);


3. Backproject the CDF of original image to a linear CDF. Get resulting image, its histogram and its cdf.

//linear desired cdf

for i=1:xsize
for j=1:ysize
n=find(x==I(i,j));
newI(i,j)=cs(n);
end
end

4. Backproject to a nonlinear CDF and get resulting image, histogram, and CDF of reconstructed image.
//nonlinear

z = [0:255];
g = tanh(30.*(x-128)./255);
g = (g - min(g))./2;

plot(x,g);
for i = 1:xsize
for j = 1:ysize
n1 = find(x == I(i,j)); //get index of x from image
y1 = csnorm(n1); //get corresponding y value
n2 = find(g<=y1); //equate with y in desired cdf
newI2(i,j) = csnorm(n2(max(n2)));
end
end

The original image has poor contrast since its pixel values does not cover the entire 0-255 values. By backprojecting its original CDF to a linear CDF with pixel values from 0-255, a better contrast is obtained. When we say backproject, it means that it follows the new CDF, but not perfectly, of course. This can be seen from the CDF of the reconstructed image using a desired linear CDF. When the nonlinear CDF (for this case, hyperbolic tangent) is used, the quality of the image degraded. Notice that the histogram of the reconstructed image is narrower than that of original image. Hence a poorer image contrast was obtained. We can conclude in this activity that image contrast of an image can either be improved or degraded depending on the CDF to which these images are backprojected.

For this activity, I give myself an 7/10. Thank you to Ma'am Jing, Ma'am Gay, Raffy, Kaye, and Neil for the help.

***
  • Note that to get the histogram and CDF of the reconstructed image, we have to multiply the image to 255 since we had it normalize to 255 before displaying it.
  • to change the color of plot to green : plot(x,y,,['g']) may apply to other colors as well :)
References
1. Dr. Maricor Soriano. Activity 4 manual.
2. image taken from: http://yrekahigh.com/Portals/1/YrekahighPics/Misc_%20Pictures/Old%20Yreka%20High.bmp

No comments:

Post a Comment