Tuesday, July 8, 2008

A6-Fourier Tranform Model of Image Formation

A. Familiarization with Discrete FFT

In the first part of the activity, we performed Fourier Transform (FT) on an image of a circle .

Figure 1. circle.bmp (128x128)

I = imread('circle.bmp');

Igray = im2gray(I);
FIgray = fft2(Igray); // This produces the fourier transform of a 2D image
//This is a complex image so we use

imshow(abs(FIgray),[]); // to obtain the magnitude of the image

The result is below

Figure 2: FT of circle.bmp

Because the result of the FT is a matrix with the diagonal quadrants interchanged, we must put the quadrants back to their original position. So we used:

imshow(fftshift(abs(FIgray))), []);

And the result is

Figure 3: FT of circle.bmp

By taking the FT of the same image twice:

imshow(abs(fft2(FIgray)));

The obtained result is the same as the image in Figure 1. This is expected because if the dimensions of the original image is X, its FT has dimensions of 1/X. Hence the FT of the FT of the image has dimensions of X, which is the same as the original.



Now we replace the circle image with "A".

Figure 4: A.bmp

I = imread('A.bmp');

Igray = im2gray(I); FIgray = fft2(Igray);// This produces the fourier transform of a 2D image, this is complex
imshow(abs(FIgray),[]); // To obtain the magnitude of the image

Figure 5: FT of A.bmp
imshow(fftshift(abs(FIgray))), []);

Figure 6: FT of A.bmp

imshow(abs(fft2(FIgray)));

Figure 7: FT of figure 6

As it turns out, when the FT is applied twice the image becomes inverted. Because the Fourier transform of the image is complex, it has a real and imaginary part. We can also think of it as the magnitude and phase (Euler Formula). Since the above codes deal with the magnitude of the image only, the phase of the image was not preserved and hence the result is an inverted image. Figure 7 has the same magnitude as Figure 4 but their corresponding phases are different.

B. Simulation of an Imaging Device

For the next part of the activity, there are two images used. Convolution between the two functions were performed. One of the functions serves as the object and the other one serves as the transfer function of an imaging system. For this activity, we can think of this transfer function as the aperture of a circular lens.

Figure 8. VIP. bmp is the object

Figure 9. circle.bmp is the aperture

r=imread('circle.bmp');
a=imread('VIP.bmp');
rgray = im2gray(r);
agray = im2gray(a);
Fr = fftshift(rgray);

Fa = fft2(agray);
FRA = Fr.*(Fa);

IRA = fft2(FRA); //inverse
FFT
FImage = abs(IRA);
imshow(FImage, [ ]);


The result is below:
Figure 10: Convolved figure of a and r

When a smaller circle is used, the result will be:

Figure 11: Smaller Circle used

On the other hand, when a larger circle is used such that the one shown below,


the result will be:
Figure 12: Larger Circle used

The larger the aperture is, the finer the image becomes. If "circle.bmp" serves as an aperture, then it determines how much light reaches the image plane. This is also explains why Figure 12 has the brightest image and Figure 11 is blurred.

C. Template Matching Using Correlation

This process is used for pattern recognition. One of the images will be the pattern and the other one will be the image where a certain pattern is or is not found. The goal is to match the A in the template to the A's in the given image. Here are the images:

Figure 13: Template Image


Figure 14: Image with the pattern "A"

Note that in Figure 14, there are 5 A's in the sentence. The code for this activity is written below.

a=imread('A.bmp');

b=imread('sentence.bmp');

agray = im2gray(a);

bgray = im2gray(b);
Fa = fft2(agray);
Fb = fft2(bgray);

C=conj(Fb); // conjugate of bgray

ab = Fa.*C;
I = fft2(ab); //forward FT

FImage = abs(I); // Inverse FT
imshow(FImage, [ ]);

Figure 15. Convolved image

In Figure 15, bright spots can be observed. This bright spots can be thought of as a match confirmation between the images in figures 13 and 14. Although the resulting image is not readable, the spots are distinct in this final image. There are 5 spots in Figure 15 telling us that there must be 5 A's in Figure 14.

D. Edge Detection using Convolution Integral

For this part of the activity, several 3x3 matrices will be used to allow edge recognition of an image. The image that was used is the same image as Figure 8.

pattern = [0,-1,0;-1,5,-1;0,-1,0];
a=imread('VIP.bmp');

agray = im2gray(a);
C = imcorrcoef(a, pattern); // convolves the matrix with the VIP image

imshow(C, [ ]);


The result is an image with sharper edges.

Figure 16: [0,-1,0;-1,5,-1;0,-1,0] for sharper edges

By using different types of matrices we have:

Figure 17: [-2,-1,0;-1,1,1;0,1,2] for an embossed image

Figure 18: [-1,-1,-1;-1,8,-1;-1,-1,-1] for line detection

Figure 19: [-1,0,0;0,-1,0;0,0,0] for edge enhancement

apreture
References:
http://www.cra.org/Activities/craw/dmp/awards/2006/Bolan/DMP_Pages/filters.html
http://en.wikipedia.org/wiki

1 comment:

Jing said...

Great work! Unique choices of filters you've got there.