Monday, July 6, 2009

ACTIVITY 5 - Fourier Transform Model of Image Formation

Activity 5.A - Familiarization with Discrete FFT

The function fft2() was demonstrated by utilizing a 128x128 images. This FFT was used for an image of a 'circle' which was created with the given code:

x = [-1:0.01:1];
[X,Y] = meshgrid(x);
r = sqrt(X.^2 + Y.^2);
circle = zeros(size(X,1), size(X,2));
circle(find (r <=0.5)) = 1.0; imshow(circle,[]);


Because of being complex, the abs() function was used for the output of the FFT to be able to view the intensity values. The first image shows the output of fft2() which has interchanged quadrants along the diagonal.

image = imread('imagefilename');
gray = im2gray(image);
FT = fft2(gray);
a = abs(FT);


To compensate, the function fftshift() was used to bring the interchanged quadrants back which is show in the second figure. With a closer look, this image is consistent to the analytical fourier transform of a circle which is an airy disk pattern.

b = fftshift(abs(FT));

Then, fft2() was again applied and the intensity of the image was displayed.

c = abs(fft2(FT)
This procedure was also done for an image of letter 'A'.

The inversion of the image after FFT being applied was clearly seen from this sample than the image of a circle. Thus, applying FFT twice will make the image inverted.



Activity 6.B - Simulation of an Imaging Device

A 128X128 image of the letters "VIP" which represents an object and an image of a white circle against black background which represents the "aperture" of a circular lens were created using MS Paint. The images were opened as grayscale in Scilab, taking the 2D FFT of the "VIP" image using fft2() function and using fftshift() function for the aperture image (the aperture is already in Fourier space).

object = gray_imread('vip.jpg');
aperture = gray_imread('circle.jpg');
offt = fft2(object);
afftshift = fftshift(aperture);


The product their FFT was inversed to get the convolved image.

FOA = offt.*(afftshift);
IOA = fft2(FOA);
The images above show the results for different radii of white circles. From left to right, the radius of white circle decreases. Thus, for the largest circle resulted for the sharpest image and for the smallest circle resulted for the blurriest image. This shows that the system acts just like a microscope.



Activity 6.C Template Matching Using Correlation

A 128X128 image with the text "THE RAIN IN SPAIN STAYS MAINLY IN THE PLAIN" on it was created in MS Paint. Then another 128X128 image with letter "A" at the center was also created with the same type of font as the first image. These images were opened as grayscale in Scilab and then got their 2D FFT.

text = gray_imread('text.bmp');
A = gray_imread('atext.bmp');
TFFT = fft2(text);
TA = fft2(A);


Then the Fourier transform of the image with letter "A" was multiplied element per element to the conjugate of the Fourier transform of the text image. Lastly, the inverse FFT of the product was computed.

P = TA.*(conj(TFFT));
R = fftshift((fft2(P)));

The picture above shows the output image besides the original image. It noticeable that the output image shows hazy counterpart of each letter on the original image. There is a significant peaks (white dots) on the letter "A" counterpart. This template matching technique recognizes the pattern (letter "A") from the template (text image). However, this can be problematic when the pattern looks like any part of each letter on the text. Take for example, when we use letter "I" as our pattern the output image will shows peaks on parts of each letters with straight vertical line just like the pattern.



Activity 6.D Edge Detection Using the Convolution Integral

Using Scilab, a 3X3 matrix pattern of an edge which has a total sum of zero was created. Three variants of this pattern were created.

horizontal:
template = [-1 -1 -1; 2 2 2; -1 -1 -1];

vertical:
template = [-1 2 -1; -1 2 -1; -1 2 -1];

spot:
template = [-1 -1 -1; -1 8 -1; -1 -1 -1];

This pattern was convolve with a "VIP" image using the function imcorrcoef().

C = imcorrcoef(vip, template);

The image above shows the results. We can see there are sharp edges on the output image depending on the pattern used. The pattern was detected along the edge of the template.


It felt good that I finished this activity during class hours. However, I was not able to finish the blog right away because of lack of time. I will give myself 10/10 for this activity because I was able to produce expected output although I have published the blog late. Thanks again for the help of Gilbert and Raffy in discussing the activity.

No comments:

Post a Comment