Wednesday, July 9, 2008

Activity 7: Enhancement in the Frequency Domain

For the 1st part of this activity, we are to observe some properties of the fourier transform. First, we observed the effect of changing the frequency of the sinusoidal image. The following images shows the result for increasing frequency. Left images are the original 2D sinusoidal images while right ones are the fourier transform of the left images.

As expected, the FT of a sinusoidal image is a dot since from the previous activity the FT of a sinusoidal function is a delta function. As we increase the frequency, the spacing between the sinusoidal image increase, this has the effect of increasing the spacing of the delta function in fourier space. Hence, we observe the dots move apart as we increase frequency.

The next part of the activity is we tried to rotate the sinusoid by a factor of theta using the following formula:

The following results were obtained, again left images are the sinusoid images while the right are its corresponding fourier transform:
Rotating the sinusoid in has the effect of rotating its fourier transform in the clockwise direction.

Multiplication of two sinusoids produces a checkerboard image while its fourier transform are 4 dots along the edges:

The next objective is to enhance the appearance of the ridges of a fingerprint to enhance its appearance. The image I used is from the activity 7 manual, the original image, enhanced image the FT of the original image and the filter is shown below:

I chose to design the filter to mimic the fourier transform of the original image, I saved the FT of the original image then edited its contrast/brightness using GIMP. Convolving this filter with the original image would result in the removal of unwanted frequencies of the image. The resulted in an improved image of the fingerprint whose ridges are more clear than the original image. Also I noticed that discontinuities in the original image was removed after applying the filter.
The code that I used is given below:
-------------------------------------------------------------------------------------
I = imread("C:\Documents and Settings\semicon\Desktop\activity7\final\sample.bmp");
I2 = im2gray(I);
C = imread("C:\Documents and Settings\semicon\Desktop\activity7\final\ft.bmp");
C = im2gray(C);
Cshift = fftshift(C);

ftI = fft2(I2);
conv = Cshift.*ftI;
iconv = fft2(conv);
scf(0);imshow(I2,[]);
scf(1);imshow((abs(fft2(fft2(iconv)))),[]);
scf(2);imshow(log(fftshift(abs(ftI))), []);
xset("colormap",hotcolormap(256))
//imwrite(log(fftshift(abs(ftI)))/max(log(fftshift(abs(ftI)))), "C:\Documents and Settings\semicon\Desktop\activity7\final\ft.bmp");
//imwrite(abs(fft2(fft2(iconv)))/max(abs(fft2(fft2(iconv)))), "C:\Users\RAFAEL JACULBIA\Documents\subjects\186\activity7\final\hiRes.bmp");
----------------------------------------------------------------------------------

The next part of the activity aims to remove the vertical lines of an image of the moon's surface. For faster calculations, I resized the image to 270x203 pixels keeping the aspect ratio constant.
The original image, the improved image, the FT of the original image and the Filter used is shown below:


Saving the FT of the original image and adjusting it seems to be not working in this particular image so I tried to just mimic the FT instead of really editing it. It can be seen that the vertical lines of the original image is not visible anymore.


I will give myself a grade of 10 for this activity because the primary objectives were met. So far this activity seems to be the hardest for me, maybe because I am not really sure if I understand the method to be used.

Thanks to the following for helping me in this activity
Eduardo David
Elizabeth Ann Prieto
Jorge Michael Presto

Monday, July 7, 2008

Activity 6: Fourier Transform model of Image Formation

For this activity we performed fourier transform on images using the FFT2 function of scilab. For the first part we obtained the FFT of circles. The following image shows the FFT of a small circle.The leftmost image is the original image. The second to the left is the fourier transform (FT) of the original image, it can be seen that it is not consistent with the analytical FT of a circle. Shifting all the zero frequencies in the image produces the 3rd image, which is now consistent with the analytical FT of a circle which airy disks. The last image is the FT of the FT of the original image, it can be seen that the image returns to original which proves that FT is reversible. I also performed FT on a larger circle shown below.

It can be seen here that FFT becomes smaller as the image becomes larger, which is consistent with the theoretical expectations. FT was also performed in the letter A shown below:The important result for this part is that it confirms property 4 of the lecture. Which states that "The inverse FFT is just the same as the forward FFT with the image inverted"


For part B of the activity we obtained the convolution of 2 images. One image serves as the "aperture" and the other serves as the "object." Taking the convolution of this two objects is similar to obtaining the "image" of the "object" using the "aperture" as a "lens." The following results are obtained for a small aperture
For a small aperture, fewer frequencies pass through. Hence we observe a bad reconstruction of the original image.


For a larger aperture, the results show that we are able to reconstruct a better image. However, we are still not able to obtain perfect reconstruction as seen in the image because the color of the image is not similar to the original.

For part C of the activity, we are to obtain the correlation of the a letter in a long sentence. We are to find the correlation of the letter "A" in the phrase "THE RAIN IN SPAIN STAYS MAINLY IN THE PLAIN." I used Arial font size 16, boldface for this part, the following is the code that I used:
-------------------------------------------------------
S = imread("C:\Documents and Settings\Semicon.POSITRON\Desktop\activity6\6C\sentence3.bmp");
A = imread("C:\Documents and Settings\Semicon.POSITRON\Desktop\activity6\6C\A3.bmp");
grayS = im2gray(S);
grayA = im2gray(A);
fftS = fft2(grayS);
fftA = fft2(grayA);
mul = fftA.*conj(fftS);
inve = fft2(mul);
scf(1);imshow(grayS);
scf(2);imshow(fftshift(abs((inve))),[]);
-------------------------------------------------------
I obtained the following result for the same sized A,

In this image we can see that the parts of the sentence where the letter "A" appears is somewhat highlighted. This is because high correlation is obtained for the parts where the letter A is present. I also tried to find the correlation of the same letter but with smaller font size, I obtained the following result:
Here we see that the phrase is more clear however, parts where the letter A appears is not highlighted.

For the last part of the activity, we performed edge detection on the word VIP using different patterns. I used the patterns suggested by Dr. Soriano, which are horizontal, vertical diagonal and spot.

The following code was used to implement the image detection using scilab:
-------------------------------------------------------------------
VIP = imread("C:\Documents and Settings\Semicon.POSITRON\Desktop\activity6\6d\T.bmp");
VIP = im2gray(VIP);
pattern1 = [-1 -1 -1;2 2 2; -1 -1 -1];
pattern2 = [-1 2 -1; -1 2 -1; -1 2 -1];
pattern3 = [2 -1 -1; -1 2 -1; -1 -1 2];
pattern4 = [-1 -1 -1; -1 8 -1; -1 -1 -1];

corre1 = imcorrcoef(VIP,pattern1);
corre2 = imcorrcoef(VIP,pattern2);
corre3 = imcorrcoef(VIP,pattern3);
corre4 = imcorrcoef(VIP,pattern4);

imwrite(corre1,"C:\Documents and Settings\Semicon.POSITRON\Desktop\activity6\6d\hori.bmp");
imwrite(corre2,"C:\Documents and Settings\Semicon.POSITRON\Desktop\activity6\6d\vert.bmp");
imwrite(corre3,"C:\Documents and Settings\Semicon.POSITRON\Desktop\activity6\6d\diag.bmp");
imwrite(corre4,"C:\Documents and Settings\Semicon.POSITRON\Desktop\activity6\6d\spot.bmp");

scf(0);imshow(corre1, []);
scf(1);imshow(corre2, []);
scf(2);imshow(corre3, []);
scf(3);imshow(corre4, []);
-----------------------------------------------------------------------
The following results were obtained for "VIP":

The arrangement is original, horizontal, vertical, diagonal and spot. For the horizontal, most of the vertical components are missing, for the vertical, the horizontal components are missing, for the diagonal some components from the vertical and horizontal are missing while for the spot most components are present. I tried using the algorithm in the letter "T" which highlighted the results further specially for the diagonal since, T almost has no diagonal components.

Obviously, components corresponding to the diagonal is almost absent and horizontal and vertical components for their corresponding filters are clear.

I will give myself a grade of 10 because I believe I was able to do all the required task and also I was able to completely explain the results.


Abraham Latimer Camba Helped me in this activity

Wednesday, July 2, 2008

Activity 5: Physical Measurements from Discrete fourier transform

For 1 dimension, the discrete fourier transform (DFT) of a set of N numbers xn is given by (taken from http://en.wikipedia.org/wiki/Discrete_Fourier_transform)

This function transforms the set of N Nnumbers xn usually in the time domain into a set of N numbers Xk in the frequency domain. The DFT of a set of numbers can be obtained using the fft(x) function of scilab.

Two dimensional DFT (i.e. for images) can be obtained using the following formula:
This function transforms a set of NxM numbers xn,m in the spatial domain to a set of numbers Xk,j in the frequency domain. Using scilab the function fft2(image) performs fourier transforms along the row and performs fft along the columns of the result.

Answers to questions:

a. The maximum sampling interval is give by
so if Fmax = 120Hz the maximum sampling interval will be 240Hz.

b. Increasing the number of samples would also result in a bigger axis since your minimum and maximum values will both increase this would mean that it can now detect more frequencies.
This is obvious in the following plots for N =100, 200 and 500 respectively,

c. Decreasing the sampling interval would result in an increase in Fmax as given by Nyquist Theorem this in turn would result in worse resolution than that for higher sampling interval since this would also increase df.
For the following plots, I changed T from 3, 1, and 0.5 respectively.


d. Changing N while keeping T constant would have the same result as b.


For this activity I will give myself a grade of 10 because I believe I have answered the questions adequately and I was able to verify my answers using Scilab.

Collaborators:
Eduardo David
Abraham Latimer Camba

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