Wednesday, June 25, 2008

A4: Enhancement by Histogram Manipulation

The objective of this activity is to enhance the quality of an image by manipulating the histogram of gray scale.














The left figure is the PDF of the original gray scale image on the middle and the corresponding CDF id on the rightmost figure. The original image is a good sample for enhancement because its gray scale is not well distributed throughout the gray scale values evident in the peaks at some region.

This is the reconstructed image based from the CDF of the original image. The new pixel values are the corresponding y-values of the original pixel values as projected in the CDF. The contrast was enhanced because the reconstruted image's PDF is well distributed throughout the gray scale and the CDF is similar to a straight line CDF of a uniform distribution PDF.













The figures above id the PDF (left) and CDF(right) of the reconstructed image. Based from this information, we can see that we had enhanced the image quality by spreading more the distribution of the gray scale in the image. A uniformly distributed gray scale image has a CDF that is linearly increasing. The goal of this activity is to approximate that property. As can be seen from my results, the reconstructed image's CDF id linear and increasing up to the limit 1.



This is the reconstructed image based from the deseried CDF the system has to mimic. I used a pice-wise CDF similar to the eye response whose slope (see the values on the code) decreases for different regions in the gray scale. The output is quite good, a better contrast of the ROI from the background due to the diffrence of response for the black, intermediate and white regions.




This is the desired CDF I used, a piece-wise function with different response at different regions of the gray scale values. This is similar to the eye which decreases its response for whiter region.





Rating: I will give myself 10. points for the effort and for the good output of this activity


The CODES:

1. PDF and CDF reconstruction
//this program outputs the PDF and corresponding CDF
//from the grayscale distribution of an image
im=imread('C:\Documents and Settings\AP186user02\Desktop\xray_new.bmp');
imn=im./max(im);

//this part outputs the PDF
val=[];
num=[];
counter=1;
for i=0:1:255
[x,y]=find(im==i); //finds where im==i
val(counter)=i;
num(counter)=length(x); //find how many pixels of im have value of i
counter=counter+1;
end
num=num./sum(num);
plot(val, num); //plot. :)

//this part outputs the CDF
mn=min(im);
mx=max(im);
c=1;
valx=0;
xval=[];
yval=[];
for i=mn:1:mx
yval(c)=valx+num(i);
valx=yval(c);
xval(c)=i;
c=c+1;
end
//plot(xval, yval);

2.The RECONSTRUCTION

//this program reconstruct an image from the CDF of that image
//to a desired uniformly distributed grayscale value distribution
im=imread('C:\Documents and Settings\gpedemonte\Desktop\xray2_gs.jpeg');
max(im);
imn=im./max(im);
val=[];
num=[];

//this part outputs the PDF
counter=1;
for i=0:1:255
[x,y]=find(im==i); //finds where im==i
val(counter)=i;
num(counter)=length(x); //find how many pixels of im have value of i
counter=counter+1;
end
num=num./sum(num);

//this part outputs the CDF
mn=min(im);
mx=max(im);
c=1;
valx=0;
xval=[];
xy=[]
yval=[];
for i=mn:1:mx
yval(c)=valx+num(i);
valx=yval(c);
xval(c)=i;
xy(xval)=yval;
c=c+1;
end

imnew=[,];
lx=size(im,1);
ly=size(im,2);
for i=1:1:lx
for j=1:1:ly
imnew(i,j)=xy(im(i,j));
end
end
imnew=imwrite(imnew,'C:\Documents and Settings\gpedemonte\Desktop\xray_recon2.jpeg')

3. The MIMICING with desired CDF

//this program mimics the non-linear response of the eye
//for imaging objects

im=imread('C:\Documents and Settings\AP186user02\Desktop\xray_new.bmp');
imn=im./max(im);

//to create a piece-wise cdf
n1=120;
n2=200;
m1=2.;
m2=1.;
m3=0.5;
cdf=[]
for i=1:n1
cdf(i)=(m1*i/255);
end
for i=(n1+1):n2
cdf(i)=cdf(n1-1)+(m2*(i-n1)/255);
end
for i=(n2+1):255
cdf(i)=cdf(n2-1)+(m3*(i-n2)/255);
end
cdf=cdf./max(cdf);
//plot(cdf)
cdfs=size(cdf)

//to create an exponential cdf
//z=[0:255];
//k=0.4;
//G=1./(1+exp(-k*(z-128)/255));

lx=size(im,1);
ly=size(im,2);
imnew=[,];
for i=1:1:110
for j=1:1:82
imnew(i,j)=cdf(im(i,j));
end
end
//imnew
imnew=imwrite(imnew,'C:\Documents and Settings\gpedemonte\Desktop\xray_recon_mimic.jpeg')

No comments: