A day with Fourier: Fourier Transform Model of Image Formation

ACTIVITY 5: Fourier Transform Model of Image Formation

—Jessica Nasayao

First, I want to know what the Fourier transform of an image looks like. Starting with a circle, I proceeded to apply the FFT2 function (fft2) in Scilab to this image. However, FFT2 interchanges the quadrants along the diagonals, so I still need to interchange the quadrants back using fftshift. After doing this, I tried applying FFT2 twice to see if I could get the original image. Sure enough, doing so takes me back to the white circle, as shown in the image below.

Image of the circle after applying fft2, fftshift and fft2 again, respectively, from left to right.

Image of the circle after applying fft2, fftshift and fft2 again, respectively, from left to right.

So now we know how the Fourier transform works. But, the question now is, is the effect the same for images with no radial symmetry like the circle? To answer this question, we do the same thing to an image of the letter A. The results are shown below.

Image of the letter A after applying fft2, fftshift and fft2 again, respectively, from left to right.

Image of the letter A after applying fft2, fftshift and fft2 again, respectively, from left to right.

Turns out, if you try to apply FFT2 to an image twice, it will give you the vertically flipped version of the original image.

Applying the Fourier transform to different synthetic images, we get the following results:

corrugated

grating

doubleslit

Here’s the snippet of code I used:

i=imread(“/home/jnasayao/Desktop/AP_186/Activity5/circle_act5.bmp”);
i= im2double(i);
shape = .299*i(:,:,1) + .587*i(:,:,2) + .114*i(:,:,3);
Fshape = fft2(shape);
ff_t = (mat2gray(abs(Fshape)));
ft_circle = mat2gray(fftshift(abs(Fshape)));
fft_circle = abs(fft2(Fshape));
img= [ff_t ft_circle fft_circle];
imwrite(img, “/home/jnasayao/Desktop/AP_186/Activity5/circle.png”);

The next question is: can we use the concept of Fourier Transform to simulate an imaging device? We know by the Convolution theorem that, if you get the linear transformation of two functions and convolve them, the result will be a function similar to its “parent” functions. Now, how do this concept apply to imaging? We can think of the two functions as the object and the camera lens, and the resulting convolution of their linear transformations as the image reproduced by the camera. We now try to simulate this by taking the Fourier transformation of an image and a white circle, the latter serving as the camera’s aperture, and convolving these two images. We get the following result:

aperture3 aperture2 aperture1 aperture0                                     Aperture size and resulting convolved image.

The reproduced image becomes clearer as the aperture size gets larger. Shown below is the comparison between the original image and the result of convolution with the largest synthetic aperture.

Original image vs. Clearest convolved image.

Original image vs. Clearest convolved image.

Here’s the code I used for this part:

v = imread(“/home/jnasayao/Desktop/AP_186/Activity5/VIP_act5.bmp”);
a = imread(“/home/jnasayao/Desktop/AP_186/Activity5/aperture3_act5.bmp”);
vgray = double(rgb2gray(v));agray = double(rgb2gray(a));
Fv = fft2(vgray);
Fa = fftshift(abs(agray));
FAV = Fv.*(Fa);
IAV = fft2(FAV);
FImage = mat2gray(abs(IAV));
img = [vgray FImage];
imwrite(img, “/home/jnasayao/Desktop/AP_186/Activity5/comparison.png”);

Turns out, Fourier transform can be used for template matching, too! For example, if we want to find the position of occurrences of a letter in a sentence, we can get the Fourier transform of both the letter and the sentence images and multiply them element by element. In this case, I picked the letter A and generate a white image of it on a black background. Next, a sentence with the same font was constructed such that it contains the letter A . The FFT2 of the conjugate of the product of their fourier transforms gives us the following image:

Template matching.Bright dot s in the right picture suggests the position of the letter A.

Template matching.Bright dot s in the right picture suggests the position of the letter A.

The bright dots indicate the position of the letter A in the sentence image. Sure enough, these positions are where the A can be found.

The code I used for this part is shown below.

sp = imread(“/home/jnasayao/Desktop/AP_186/Activity5/RAIN_act5.bmp”);
a = imread(“/home/jnasayao/Desktop/AP_186/Activity5/ARAIN_act5.bmp”);
spgray = double(rgb2gray(sp));
agray = double(rgb2gray(a));
Fa = double(fft2(agray));
Fsp = double(fft2(spgray));
Fspa = conj(Fsp).*(Fa);
Ispa = fft2(Fspa);
Fimage = mat2gray(abs(fftshift(Ispa)));
img = [mat2gray(spgray) Fimage];
imwrite(img, “/home/jnasayao/Desktop/AP_186/Activity5/spain.png”)

Another application of Fourier Transform is edge detection. Apparently, if you convolve an image with a matrix whose sum of elements is zero, you get to see interesting images which vary according to the pattern of the matrix. Trying this on the previously generated VIP image, I was able to get the following resulting images:

Images generated when image is convoluted with a centered horizontal, centered vertical, diagonals and a spot pattern, from left to right respectively.

Images generated when image is convoluted with a centered horizontal, centered vertical, diagonals and a spot pattern, from left to right respectively.

Here’s the snippet of code I used:

v = imread(“/home/jnasayao/Desktop/AP_186/Activity5/VIP_act5.bmp”);
vgray = double(rgb2gray(v));
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 -2; 1 -2 1; -2 1 1];
pattern5 = [1 1 1; 1 -8 1; 1 1 1];
edge1 = conv2(vgray, pattern1);
edge2 = conv2(vgray, pattern2);
edge3 = conv2(vgray, pattern3);
edge4 = conv2(vgray, pattern4);
edge5 = conv2(vgray, pattern5);
img = [edge1 edge2 edge3 edge4 edge5];
imwrite(img, “/home/jnasayao/Desktop/AP_186/Activity5/edges.png”);


All in all, I would give myself a 9/10 on this one, since I think I’ve done quite good, being new to Scilab and all. 🙂

Leave a comment