Sunday, June 29, 2008

Activity 4: Image Enhancement by Histogram Manipulation

For this activity, I used the following image from : http://www.inkjetart.com/4990/48bit/Tetons_before2.jpg
The normalized Image Histogram corresponding to the grayscale probability distribution(PDF), shown below shows that the image has poor contrast since its histogram is concentrated along higher pixel values. Also shown below is the cumulative distribution function (CDF) obtained by getting the cumulative sum of the Y axis of the normalized histogram.

The Following code was used to obtain the PDF, CDF of the original image and also to perform backprojection into the new image as well as to obtain its corresponding CDF and PDF.
-----------------------------------------------------------------
I = gray_imread("C:\Users\RAFAEL JACULBIA\Documents\subjects\186\activity4\\final\tetons_before2.bmp")*255;

Isize = size(I);
//histogram routine
x = matrix(0:255,[1,256]);
y = x.*0;

for i = 1:Isize(1)
for j = 1:Isize(2)
y(I(i,j)+1) = y(I(i,j)+1)+1;
end
end
h = scf(1) //show image
imshow(I/max(I))

h=scf(2) //plot histogram
y = y/sum(y);
plot2d3(x, y)
//end histogram routine

ys = cumsum(y)/max(cumsum(y));
h=scf(3) //plot cdf
plot(x,ys)

//image enhancement
for i=1:Isize(1)
for j = 1:Isize(2)
newI(i,j)=ys(I(i,j)); //match y values of cdf of Image with enhanced image
end
end
//end image enhancement

imwrite(newI,"C:\Users\RAFAEL JACULBIA\Documents\subjects\186\activity4\final\newI.bmp");
newI = gray_imread("C:\Users\RAFAEL JACULBIA\Documents\subjects\186\activity4\final\newI.bmp")*255;

h = scf(4)
imshow(newI/max(newI))

newX = matrix(0:255,[1,256]);
newY = newX.*0;
sizenew = size(newI);
for i = 1:sizenew(1)
for j = 1:sizenew(2)
newY(newI(i,j)+1) = newY(newI(i,j)+1)+1;
end
end
h = scf(5)
newY = newY/sum(newY);
plot2d3(newX, newY)

newYs = cumsum(newY)/max(cumsum(newY));
h=scf(6)
plot(newX,newYs)

-------------------------------------------------------------------


After backprojection the following image is obtained. Original image (left) was included for comparison.
The PDF and CDF obtained from the enhanced image is given below:
It can be clearly seen from that the histogram is well spread suggesting a good contrast image. The CDF also is a straight line which is another indicator of a good contrast image. The small deviations of the CDF from being a perfectly straight line may have been due to the rounding off of values when the image when it was converted to grayscale using gray_imread(image)*255, the same result is obtained if I use imread(image) and convert to grayscale using im2gray(image)*255.

If we use specified final CDF, the enhancement part of the code will be changed as follows:
----------------------------------------------------
z = [0:255];
g = (tanh(5*(z-128)/255))+1;
g = g/max(g);

for i=1:Isize(1)
for j = 1:Isize(2)
sub = abs(ys(I(i,j))-g);
sub = find(sub==min(sub),1);
newI(i,j) = sub;
end
end
------------------------------------------------------

After the code is implemented, the following image is obtained. Left is the original image center is for linear CDF and rightmost is for the hyperbolic tangent CDF.

For this image, the following PDF and CDF is obtained. For the above CDF, black is the desired CDF and blue is the obtained CDF. Adjusting the k value from 5 to 8 gives a better fit of the desired CDF, as shown below.

In my observation, the linear response CDF provides a higher contrast compared with the nonlinear CDF. However the linear CDF sacrifices some detail on the image, for instance the leaves near the edge is more clear for the nonlinear CDF.

I will give myself a score of 10 for this activity since I was able to have a good image enhancement.

Many thanks to the following people who directly or indirectly, helped me a lot in this activity.
Eduardo David
Abraham Latimer Camba
Jeric Tugaff
Elizabeth Ann Prieto

Wednesday, June 25, 2008

Activity 3: Image Types and Basic Image Manipulation

I only used one image for the first part of this activity and saved it in different formats using GIMP.


For true color the image the image format used is jpeg, as originally obtained from my source [1]. For binary the same image was saved as monochrome bitmap but threshold was performed using gimped. For the indexed image, the same image was saved as gif. Lastly for the Grayscale, gif format was also used.

The following attributes was found using imfinfo of scilab:
Truecolor
FileName: C:\Documents and Settings\semicon\Desktop\activity3\final\leaf_truecolor.jpg
FileSize: 17462
Format: JPEG
Width: 256
Height: 192
Depth: 8
StorageType: truecolor
NumberOfColors: 0
ResolutionUnit: inch
XResolution: 96.000000
YResolution: 96.000000

Binary
FileName: C:\Documents and Settings\semicon\Desktop\activity3\final\leaf_binary.bmp
FileSize: 6206
Format: BMP
Width: 256
Height: 192
Depth: 8
StorageType: indexed
NumberOfColors: 2
ResolutionUnit: centimeter
XResolution: 37.800000
YResolution: 37.800000

Indexed
FileName: C:\Documents and Settings\semicon\Desktop\activity3\final\leaf_indexed.gif
FileSize: 42446
Format: GIF
Width: 256
Height: 192
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: centimeter
XResolution: 72.000000
YResolution: 72.000000

Grayscale
FileName: C:\Documents and Settings\semicon\Desktop\activity3\final\leaf_gray.gif
FileSize: 53263
Format: GIF
Width: 256
Height: 192
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: centimeter
XResolution: 72.000000
YResolution: 72.000000

For the second part of the activity we are to measure the area of a flat image. The image I used is an ATM card shown below. Also shown is the histogram of the said image obtained using GIMP.
















The following information on the image was obtained using imfinfo function of scilab:
FileSize: 95057
Format: PNG
Width: 504
Height: 323
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: centimeter
XResolution: 118.110000
YResolution: 118.110000

It can be seen from the histogram that the region of interest (lower pixel values) is well separated from the background (higher pixel values).

Thresholding was done using the im2bw command of scilab using 0.6 as the threshold value. After thresholding, the following image and histogram was obtained:















As seen from the histogram, Only 2 pixel values is present, o and 255. This proves that the image is now binary. However using the imfinfo function of scilab, I obtained the following information:

FileName: C:\Documents and Settings\semicon\Desktop\ATM\atmBW.bmp
FileSize: 163870
Format: BMP
Width: 504
Height: 323
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: centimeter
XResolution: 28.340000
YResolution: 28.340000

Obviously, scilab imfinfo believes that the image is not yet binary. But I tried to save it in a different image format (gif) the number of colors changed to 2:

FileName: C:\Documents and Settings\semicon\Desktop\ATM\atmBW.gif
FileSize: 4505
Format: GIF
Width: 504
Height: 323
Depth: 8
StorageType: indexed
NumberOfColors: 2
ResolutionUnit: centimeter
XResolution: 72.000000
YResolution: 72.000000
Apparently, Scilab prefers to save images in gif format for binary images.

I integrated the area calculation routine from the previous activity with the image conversions of this activity. I obtained an area of 160759, the theoretical area is 162792 giving me a percentage error of 1.25%

For this activity I will give myself a score of 10 because I was able to obtain a good image threshold and for the area calculation I was able to get a very small error of 1.25%

The Scilab code I used for this activity is given below:
-------------------------------------------------------
I = imread('C:\Documents and Settings\semicon\Desktop\ATM\atm.bmp');
//I = im2gray(I);
I = im2bw(I,0.6);
I = abs(I-1);
imwrite(I,'C:\Documents and Settings\semicon\Desktop\ATM\atmBW.bmp');

[x,y] = follow(I,8);
sizex = size(x); //obtain matrix size
sizey = size(y);

x1 = x; //define new variables x1 and y1
y1 = y;

x1(2:sizex(1)) = x(1:sizex(1)-1); //shift x1
x1(1) = x(sizex(1));
y1(2:sizey(1)) = y(1:sizey(1)-1); //shift y1
y1(1) = y(sizey(1));

dif = x.*y1-y.*x1; //green's theorem
A = abs(0.5*sum(dif))
------------------------------------------------------
Eduardo David and Elizabeth Ann Prieto helped me with this activity.

the image source (leaf) is:
http://www.pdphoto.org/jons/pictures2/leaf_1_bg_010503.jpg

Wednesday, June 18, 2008

Activity 2

For this activity, I will give myself a grade of 9.
I was able to Implement Green's Theorem on a square, circle and rectangle image shown belowI was able to obtain the following Areas and computed for the percentage errors:
Image // Computed // Actual // Error(%)
square 4536 4900 8
circle 3422 3525 3
rectangle 3318 3526 6

I used the following code in scilab to obtain the area
-----------------------------------------------------------
I = imread("100rectangle.png"); //filename
i = im2bw(I,1); //convert to binary

[x,y] = follow(i);
sizex = size(x); //obtain matrix size
sizey = size(y);

x1 = x; //define new variables x1 and y1
y1 = y;

x1(2:sizex(1)) = x(1:sizex(1)-1); //shift x1
x1(1) = x(sizex(1));
y1(2:sizey(1)) = y(1:sizey(1)-1); //shift y1
y1(1) = y(sizey(1));

dif = x.*y1-y.*x1; //green's theorem
A = 0.5*sum(dif)
-----------------------------------------------------------

Abraham Latimer Camba and Eduardo David Helped me in this activity.

Wednesday, June 11, 2008


For this activity I will give myself a score of 9. I was able to fit the reconstructed plot to the original image, however, both the x and y axes are off by around 1 - 3 divisions. this happened because the image i scanned is skewed, so the divisions in the image are not equally spaced.

to compute for the conversion formula, i obtained the pixel positions of the x and y axes and looked for an appropriate conversion factor. Afterwards I subtracted the position of the origin. I was able to obtain the following formula;

x axis: (xpixel-65)/43.4375
y-axis: (ypixel-529)/-42.9167
my origin is at (65,529)

Jorge Michael Presto and Eduardo David helped me in this activity