Tuesday, July 1, 2008

Activity 4: Enhancement by Histogram Manipulation

In this activity, we are asked to enhance a poor contrast image by histogram manipulation and backprojection. First, a poor contrast image was obtained from the internet and its histogram or probability distribution function (PDF) was calculated using Scilab. These are shown below. An image is considered to be of poor contrast if its histogram is not well spread out across the gray level range.

source:http://homepages.inf.ed.ac.uk/rbf/HIPR2crimmins.htm








Next, we solve for the image's cumulative distribution function (CDF) by using the cumsum function in Scilab. For example, if the area of the image is 256x256 and H is the histogram function, then cdf = cumsum([H/(256*256)]); and we plot it. The CDF is just the sum of the histogram values as the gray level increases. In essence, the PDF and CDF give a complete description of the probability distribution of a random variable. The original image's CDF here is crooked but the minimum and maximum values are at the same gray levels as the PDF.

We now proceed in backprojecting the image pixel values, pixel per pixel, by finding its corresponding y-value in the CDF and assigning these values to the enhanced image.




im = imread('gray.jpg');

//Plot histogram
val = [];
num = [];
c = 1;
for i = 0:1:255
[x,y] = find(im == i);
val(c) = i;
num(c) = length(x);
c = c + 1;
end
//plot(val,num/(length(im)));
//title('Normalized histogram of image');
//xlabel('grayscale values');

//Plot CDF
cdf = cumsum([num/(length(im))]);
//plot(cdf);
//title('Normalized CDF');
//xlabel('grayscale values');

//Backprojection
newim = [];
for ii = 1:1:size(im,1)
for jj = 1:1:size(im,2)
newim(ii,jj) = cdf(im(ii,jj));
end
end
imwrite(newim, 'newim.jpg');

Here is the resulting enhanced image.
ORIGINAL & ENHANCED

In theory, the histogram of the enhanced image should be more spread out across the gray levels, indicating good contrast. Due to this, the resulting CDF of the enhanced image should resemble a line with increasing slope. These graphs are shown below.
















As expected, the histogram is well spread and the CDF looks fairly like a line.
However, the image is observed to be highly pixelated and this is due to low resolution of the images (111 x 111). More computational power will be required for images with higher resolution.
____________________________________________________________________

Another method of image enhancement using histogram manipulation is by mapping the original image's gray levels into a new set of gray levels such that a desired CDF is obtained. We can do this by (1) finding the CDF value from pixel grayscale, (2) trace this value in desired CDF, (3) replace pixel value by grayscale value having this CDF value in desired CDF.
The desired CDF can be any function so long as the y-values are from 0 to 1. I will use the function G(z) = z^2 where z ranges from 0 to 255. The code for enhancing the image is also shown below.













//function of desired CDF
z = [0:255];
G = z^2;

G = G./max(G);

//method implementation
newim = [];
for ii = 1:1:size(im,1)

for jj = 1:1:size(im,2)
E(ii,jj) = cdf(im(ii,jj));

end

end


//interpolation
newim = interp1(G,z,E);
imshow(newim, [0 255]);

newim = round(newim);

//plot histogram
value = [];
number = [];

c1 = 1;

for i = 0:1:255
[x,y] = find(newim == i);
value(c1) = i;

number(c1) = length(x);

c1 = c1 + 1;

end

//plot(value, number/(length(newim)));

//title('Histogram of enhanced image');

//xlabel('grayscale values');


//plot CDF
cdf = cumsum([number/(length(newim))]);
plot(cdf);

title('Enhanced CDF');
xlabel('grayscale values');



And here is the result:
ORIGINAL, 1ST METHOD ENHANCEMENT, 2ND METHOD ENHANCEMENT

Shown below are the histogram and CDF of the enhanced image in the 2nd method. The PDF is also well spread across the graylevel range while the CDF followed the desired CDF, which is a parabola (G(z) = z^2).

The enhanced image from the 1st method is darker than in the 2nd method. In terms of contrast, the 1st method is better. Any of the 2 methods can be used for any other poor contrast image in attempting to enhance it. I cannot decide as of the moment which of the 2 is "better" per se.














I give myself 10 points for this activity since the images were enhanced significantly compared to the original image. This can be shown not only in the images themselves but also from the PDF and CDF of the enhanced images. Thanks to Jeric Tugaff for help in implementing the 2nd method in this activity.

0 comments: