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')

Monday, June 23, 2008

Activity 3: Image types and basic enhancements


This is a gray scale image converted originally from a true color image. The image properties are:
Filename-C:\Documents and Settings\Instru\Desktop\gs1n.jpg Filesize-602 Filesize-1871 kb
Format-JPEG
width-128 pixels
height-92 pixels,
depth- 8
Storage type-indexed
number of colors-256
resolution unit-inch
Xresolution-0.
Yresolution-0.
Source:blogs.mathworks.com


This is a binary image converted originally from a true color image. The image threshold I used is 0.5. The image properties are:
Filename-C:\Documents and Settings\AP186user02\Desktop\vase_bw.png Filesize-602 kb
Format-PNG
width-113 pixels
height-102 pixels
depth- 16
Storage type-true color
number of colors-0.
resolution unit-centimeter
Xresolution-0.
Yresolution-0.
Source:www.aimatshape.net


This is a true color image with the following properties:
Filename-C:\Documents and Settings\Instru\Desktop\true_color.jpg Filesize-3405 kb
Format-JPEG
width-128 pixels
height-128 pixels
depth- 8
Storage type-true color
number of colors-0.
resolution unit-centimeter
Xresolution-0.
Yresolution-0.
Source:asymptotia.com


This is a indexed image converted originally from a true color image. The image threshold I used is 0.5. The image properties are:
Filename- C:\Documents and Settings\Instru\Desktop\vase_indexed.bmp Filesize-6328 kb
Format-BMP
width-113 pixels
height-102 pixels
depth- 8
Storage type-indexed
number of colors-16.
resolution unit-centimeter
Xresolution-1.718D+09
Yresolution-1.718D+09
Source:www.aimatshape.net

The conversion from one image type to another was done using Paint. The true color image is saved to either Monochrome bitmap for binary, 16 Color bitmap for indexed and 256 Color bitmap for gray scale.

The next task is to calculate the area of a scanned image.













a b c


The original 24 Bit image (a) is converted to a 256 Color bitmap image (b) via Paint. The binary image (c) is the thresholded image of the gray scale image of the scanned image. The threshold I used is 150/max(pixel value of the image). The figure below is the histogram of the gray scale converted image of (a).

The values cluster at the white region and some in the mid-gray scale values. The separate the ROI (mostly white)from the background (mostly black), we should threshold the values to be 1 if the pixel gray level is above 150/(max pixel value of the image) and make the values 0 if it is below the threshold. On the code, the pixel values are normalized.





The area I got from the binary image converted from the gray scale image which originated from the scanned image is 135200. check this, I also calculated the dame binary image from the gray scale image thresholded by paint and I got the same result.

The Codes:
For hist: Courtesy of Jeric;
thresh=150.
im=imread('C:\Documents and Settings\Instru\Desktop\gs2.bmp');
imn=im2bw(im,thresh/max(im));
[x,y]=follow(imn);
lx=length(x);
ly=length(y);
x1(1)=x(lx);
x1(2:lx)=x(1:lx-1);
y1(1)=y(ly);
y1(2:ly)=y(1:ly-1);
area=abs(0.5*sum(x1.*y-x.*y1))

For area of the gray scale image;
thresh=150.
im=imread('C:\Documents and Settings\Instru\Desktop\gs2.bmp');
imn=im2bw(im,thresh/max(im));
[x,y]=follow(imn);
lx=length(x);
ly=length(y);
x1(1)=x(lx);
x1(2:lx)=x(1:lx-1);
y1(1)=y(ly);
y1(2:ly)=y(1:ly-1);
area=abs(0.5*sum(x1.*y-x.*y1))


For the gray scale image;
im=imread('C:\Documents and Settings\Instru\Desktop\binary_paint.bmp');
im=im-1;
[x,y]=follow(im);
lx=length(x);
ly=length(y);
x1(1)=x(lx);
x1(2:lx)=x(1:lx-1);
y1(1)=y(ly);
y1(2:ly)=y(1:ly-1);
area=abs(0.5*sum(x1.*y-x.*y1))

I will give myself points 10. because I managed to convert the images to different types. Also, I was able to validate the area via Greens Theorem using my choice of threshold and surprisingly,it is equal to the area of the same image thresholded by paint to binary.

Wednesday, June 18, 2008

activity 2: area estimation for images with defined edges












The figures I used is a square with a pixel area of 96x97, a 155x84 pixels rectangle and a circle with radius of 48 pixels which I counted manually by locating the edges' pixel coordinates. Using functions in the siptoolbox, the image was converted easily into a binary array. Then manipulating this values, we can locate the edge coordinates of the image (or its contour coordinates) using the function 'follow'. A matrix shifted forward by one element has to be created as a requirement in the Green's Theorem equation. Using Green's Theorem, the area of the image was easily computed.


Results: The theoretical area of the figures I used are 9312 pixels, 13020 pixels and 7238.22 pixels for square, rectangle and circle respectively. Their corresponding areas calculated via green's Theorem are 9216 pixels,13104 pixels and 7208 pixels respectively. Their corresponding discrepancies are 1.03%, 0.65% and 0.42% respectively. In that case, I can asses that I managed to compute for the images areas to a good degree of accuracy. Discrepancies arise due to difference in pixel locations of the binary image and the actual image. There is a small shift in the coordinates that will affect the contour coordinates.

Rating: I would like to acknowledge my collaborator Mr. Carl Nicollo Fabros for helping me to use what functions needed. This tme I will give myself a grade of 10.0 because based from the results I got, I am confident that I did a good job.

The Code:
Square:
/to load the image into binary array
I=imread('C:\Documents and Settings\AP186user02\Desktop\square2.jpeg');

//to get the coordinates of the contour
[x,y]=follow(I);

//to shift the x and y array
lx=length(x);
ly=length(y);
x1(1)=x(lx);
x1(2:lx)=x(1:lx-1);
y1(1)=y(ly);
y1(2:ly)=y(1:ly-1);

//actual pixels counted
t_area=97*96

//to compute for the area and %error using Green theorem
area=abs(0.5*sum(x1.*y-x.*y1))
da=100*(t_area-area)/t_area

Rectangle
//to load the image into binary array
I_rect=imread('C:\Documents and Settings\AP186user02\Desktop\rect.jpeg');

//to get the coordinates of the contour
[x,y]=follow(I_rect);

//to shift the x and y array
lx=length(x);
ly=length(y);
x1(1)=x(lx);
x1(2:lx)=x(1:lx-1);
y1(1)=y(ly);
y1(2:ly)=y(1:ly-1);

//actual pixels counted
t_area=155*84

//to compute for the area and %error using Green theorem
area=abs(0.5*sum(x1.*y-x.*y1))
da=100*(t_area-area)/t_area

Circle
//to load the image into binary array
I_circle=imread('C:\Documents and Settings\AP186user02\Desktop\circle.jpeg');

//to get the coordinates of the contour
[x,y]=follow(I_circle);

//to shift the x and y array
lx=length(x);
ly=length(y);
x1(1)=x(lx);
x1(2:lx)=x(1:lx-1);
y1(1)=y(ly);
y1(2:ly)=y(1:ly-1);

//actual pixels counted
r=48
t_area=%pi*r*r

//to compute for the area and %error using Green theorem
area=abs(0.5*sum(x1.*y-x.*y1))
da=100*(t_area-area)/t_area


----End of Blog------

Wednesday, June 11, 2008

AP 186 activity 1



This is my first blog ever.. hehe. At kelangan academic ang mood (tsk tsk)... here it goes...

AP 186 Activity 1: Digital Scanning
After this activity, we should be able to reconstruct digitally scanned hand drawn plots using ratio and proportion by locating the exact pixel location of relevant points and the scale of the plot.

Self Evaluation: I'll give myself 9.0 points. It took me 1 hour to finish the reconstruction of plot. That's quite long. Locating points' pixel values is easy, even the reconstruction. I just had hard time on reproducing the same grid point due to wrong pixel to cm ratio I used. But I was able to figure it out. I managed to finish the activity on my own.

Reference:
Canadian journal of Physics
Vol. 21, 1951