Sunday, June 29, 2008

Image Enhancement

June 30, 2008
11:51 am


The goal for this activity is to enhance the contrast of an image by manipulating its cumulative distribution function (CDF). The standard way of doing this is by applying a transformation to the image such that its (CDF) is a straight line. We do this by replacing the gray level values of each pixel by the CDF value of its gray level. This is called histogram equalization. This is to distribute the intensity over the whole range of gray level values, i.e., 0-255. We may also create our own image enhancement protocols. I did it by replacing the gray level values of each pixelin the original image by the squared CDF value of its gray level.



Images and Their Corresponding Histograms



a) original image




















b) histogram-equalized image






















c) custom enhanced by replacing the original pixel values by the CDF value squared of the original























The CDF for (c) takes the shape of a quadratic function that opens to the right. It is fitted to the function y = sqrt(x/255).


Codes:


This code. It aims to perform histogram equalization and get the probability distribution function and cumulative distribution function of both the original and the enhanced. Its histogram-getting part is taken from Mr. Tugaff's code.


A=imread('sunflower3.jpg');


A = A-1; //because the image that would be read would have pixel values from 1 to 256.


val=[];


num=[];


counter=1;
for i=0:1:255


[x,y]=find(A==i);


val(counter)=i;


num_(counter)=length(x);


counter=counter+1;


end


num = num_./((size(A,1))*size(A,2));


plot(val, num_);


cdf = [];c


cdf(1) = num(1);


for i = 2:256


cdf(i) = num(i) + cdf(i-1);


end


plot(val,cdf)


dimensions = (size(A,1)*size(A,2));


B = zeros(size(A,1),size(A,2));


for i = 0:1:255


[x,y] = find(A==i);


for k=1:length(x)


B(x(k),y(k)) = (cdf(i+1) - min(cdf))/(dimensions -min(cdf));


end


end


B = B/max(max(B));


B = round(B*255);


counter = 1;


val2=[];


num = [];


num2_ = [];


for i=0:1:255


[x,y]=find(B==i);


val2(counter)=i;


num2_(counter)=length(x);


counter=counter+1;


end


num2 = num2_./((size(B,1))*size(B,2));


plot(val2, num2_);


cdf2 = [];


cdf2(1) = num2(1);


for i = 2:256


cdf2(i) = num2(i) + cdf2(i-1);


end


plot(val2,cdf2)


To get the images for section (c), custom enhancement, we simply this part of the above code


B = zeros(size(A,1),size(A,2));
for i = 0:1:255
[x,y] = find(A==i);
for k=1:length(x)
B(x(k),y(k)) = (cdf(i+1) - min(cdf))/(dimensions -min(cdf));
end


by


B = zeros(size(A,1),size(A,2));
for i = 0:1:255
[x,y] = find(A==i);
for k=1:length(x)
B(x(k),y(k)) = (cdf(i+1) - min(cdf))*(cdf(i+1) - min(cdf))/(dimensions -min(cdf));
end


SELF-EVALUATION RATING: 10/10 because I got a straight line for the CDF of the histogram-equalized image.

Credits:


Orignal sunflower photo was taken from: Christopher Maxwell, PhotographerWedding Photography: Black and White Info, http://www.christophermaxwell.com/black-white.htm


The part of the above code that get the histogram of an image is lifted from a code by Mr. Jeric Tugaff.

No comments: