Monday, August 1, 2011

Fourier Transform Model of Image Formation

Familiarization with the Fast Fourier Transform (FFT)

We'll be playing around with the FFT routine in Scilab. Let's see what we can do with images using FFT. Starting with the image of a circle:


Applying fft2() on the image produces this:
FFT of circle
The FFT routine by Cooley and Tukey used in most applications produces the Fourier transform with the quadrants interchanged. Applying fftshift() puts the quadrants in their correct position:
fftshift() performed on FFT of circle
From here on, when I say that I used FFT to perform Fourier transform on an image, that means I used both fft2() and fftshift().

Let's try taking the Fourier transform of other images! Here's one, a square and its Fourier Transform:


And another, a very small square and its Fourier Transform:


And more! The image of a letter A and its Fourier Transform:


A curious thing however, is that when we use the technique described above to retrieve 'A' from its Fourier transform, we get an inverted image:


Why's that? That's because we can only use the Fourier Transform twice, fft2(fft2()), instead of a forward Fourier transform then an inverse Fourier Transform. Which is actually the same thing except for the negative sign:

forward Fourier transform

inverse Fourier transform
So we'll also have to flip the images (twice, for both x and y!) to get them right. ;)


Convolution


The convolution theorem
H=FG

where H, F, and G denotes the Fourier transforms of hf, and g respectively, combines two functions f and g together such that the resulting function h has qualities of both functions. In optical systems, the image produced by passing through an optical device like a lens or an aperture is just the convolution of the object f and the transfer function of the optical device g. If we take the object to be this image,

object
and the aperture to be a circular hole, 
aperture
then apply the convolution theorem to get the image of the object as seen through the aperture, we get
image of object as seen through the circular aperture
As you can see, the image is quite blurred. Let's see the effects of using circular apertures of different sizes:
(the aperture and resulting image using the same 'VIP' object)






Smaller apertures have blurry images while larger apertures give finer images. Makes sense right? If you look at an object through a small hole, you'll find it harder to make out the image than through a larger hole. But remember that we're working with light, and diffraction effects occur. So for different apertures, expect a different effect.

How about we try different apertures to see what happens? :D
An annulus, grating, and roof aperture:





Nice. :D
I kinda like the annulus effect.



Teehee :P

Correlation

Correlation uses the Fourier Transform to measure the degree of similarity between two functions. A high correlation value (p) corresponds to a high degree of similarity.

P=F*G

P, F, and G stands for the Fourier transforms of p, f and g respectively, and * denotes complex conjugation.

Let's see what will happen if we use correlation on this image:


Let's try to correlate the image with the image of the letter A and the letter B:


And here are the results:
correlation with 'A'
correlation with 'B'
Did you see this?
correlation with 'A'
There are intense white dots in the part of the image where there is the letter A. That means those parts of the image matched highly with the image of 'A'. We don't see this in the correlation image with 'B' since there's no B in the image. This technique can be used to find an object within an image.


Edge Detection

The correlation technique can also be used to detect edges. Instead of looking for letters or objects in an image, we can look for a particular set of pixels (which we'll call the template) to detect the edges around an object. And instead of coding everything ourselves, we can just use Scilab's imcorrcoef(). The advantage is that it gives us a better idea of which parts of the image matches the template. The following shows the template used and the output of imcorrcoef(). 






Parts of the image are highlighted depending on the template used. Notice that the horizontal lines in 'VIP' are more prominent for the horizontal template, the vertical lines for the vertical template and so on. And of course to detect the edges you need to use a good template, and the cross and spot seem to be good choices. 



__________________________________________________________________________
Thanks to Aiko for drawing the nice picture. :)
I'm giving myself a 10 for this activity.