This activity is divided into 4 parts namely: (1) Familiarization with the discrete Fast Fourier Transform (FFT), (2) Simulation of an imaging device, (3) Template matching using correlation, and (4) Edge detection using the convolution integral.
(1) Familiarization with the discrete FFT
This part is straightforward in the sense that we try to understand the properties of the FFT and see what happens to an image after doing the operation. As a start, we generate a 128x128 image of a circle in MSPaint and apply the FFT.
I = imread('circle.bmp');
Igray = im2gray(I);
FIgray = fft2(Igray);
imshow(abs(FIgray),[]); //intensity image
//imshow(abs(fftshift(FIgray)),[]); //fft shifted image
//imshow(abs(fft2(FIgray))); //fft twice
xset("colormap",hotcolormap(32)); //add color to your images
CIRCLE: ORIGINAL image, FFT INTENSITY image, FFT SHIFTED image, DOING FFT TWICE




For the 1st FFT image (2nd from left), we observe spots at the corners of the image. These are actually the 4 quarters of a circle and we predict that once this image is FFT shifted, we will observe the correct analytical Fourier Transform of a circle, which is an Airy disk: a bright spot centered about concentric rings outside of it. Indeed, this is what we observe in our image (3rd from left). Upon applying the FFT twice on our original image, what we observe is that we return to our original image. However, this is actually an inverted image of the original and it is just not obvious. We can verify this by using an "A" as our image.
"A": ORIGINAL image, FFT INTENSITY image, FFT SHIFTED image, DOING FFT TWICE




"A": ORIGINAL image, FFT INTENSITY image, FFT SHIFTED image, DOING FFT TWICE




The "A" is flipped with respect to the horizontal and is now inverted with respect to the original image. Thus, doing FFT twice will result in an inverted image.
____________________________________________________________________(2) Simulation of an imaging device
Here we try to simulate what happens in a camera. We vary the camera lens radii and see what the FFT images look like.
c = gray_imread('circle.png'); //medium aperture
//c = gray_imread('bigcircle.png'); //big aperture
//c = gray_imread('smallcircle.png'); //small aperture
vip = gray_imread('vip.png');
Fc = fftshift(c); //aperture is already in the Fourier Plane and need not be FFT'ed
Fvip = fft2(vip);
Fcvip = Fc.*(Fvip);
Icvip = fft2(Fcvip); //inverse FFT
FImage = abs(Icvip);
imshow(FImage, []);
c = gray_imread('circle.png'); //medium aperture
//c = gray_imread('bigcircle.png'); //big aperture
//c = gray_imread('smallcircle.png'); //small aperture
vip = gray_imread('vip.png');
Fc = fftshift(c); //aperture is already in the Fourier Plane and need not be FFT'ed
Fvip = fft2(vip);
Fcvip = Fc.*(Fvip);
Icvip = fft2(Fcvip); //inverse FFT
FImage = abs(Icvip);
imshow(FImage, []);
ORIGINAL, SMALL APERTURE, MEDIUM APERTURE, BIG APERTURE




A finite lens radius in a camera means that the lens can only gather a limited number of rays reflected off an object, therefore reconstruction of the object is never perfect. A smaller lens aperture will gather fewer rays reflected off an object and this results in a blurred image. Comparing this with the bigger aperture, the image is sharper and most closely resembles the original.
____________________________________________________________________
(3) Template matching using correlation
____________________________________________________________________
(3) Template matching using correlation
Template Matching is a pattern recognition technique suitable for finding exactly identical patterns in a scene such as in the case of finding a certain word in a document. Here, we apply this technique in finding where the letter A's are in a sample text.
rain = gray_imread('rain.png'); //image of the text
a = gray_imread('a2.png'); //image of the letter to correlate with text, same font type and size
Frain = fft2(rain);
Fa = fft2(a);
p = Fa.*conj(Frain);
im = ifft(p); //inverse FFT
imshow(abs(fftshift(im)),[]);
ORIGINAL & RESULTING IMAGE


In the resulting image, we see that bright dots are located where there are letter A's in the text. These words are "rain", "Spain", "stays", "mainly", and "plain." There are 5 bright dots that correspond with 5 A's in the text. We have demonstrated here how the shape of the letter A was matched with the text using correlation.
(4) Edge detection using the convolution integral
rain = gray_imread('rain.png'); //image of the text
a = gray_imread('a2.png'); //image of the letter to correlate with text, same font type and size
Frain = fft2(rain);
Fa = fft2(a);
p = Fa.*conj(Frain);
im = ifft(p); //inverse FFT
imshow(abs(fftshift(im)),[]);
ORIGINAL & RESULTING IMAGE


In the resulting image, we see that bright dots are located where there are letter A's in the text. These words are "rain", "Spain", "stays", "mainly", and "plain." There are 5 bright dots that correspond with 5 A's in the text. We have demonstrated here how the shape of the letter A was matched with the text using correlation.
(4) Edge detection using the convolution integral
Edge detection can be seen as template matching of an edge pattern with an image. We can detect the edges of the previous "VIP" image, for example, by applying a pattern oriented in a particular detection. The code and resulting images are shown below.
hor = [-1 -1 -1; 2 2 2; -1 -1 -1]; //horizontal pattern
vert = [-1 2 -1; -1 2 -1; -1 2 -1]; //vertical pattern
spot = [-1 -1 -1; -1 8 -1; -1 -1 -1]; //spot pattern
pattern = hor;
vip = gray_imread('vip.png');
im = imcorrcoef(vip, pattern);
imshow(im);
ORIGINAL, HORIZONTAL EDGE, VERTICAL EDGE, SPOTS




As we can see, the edges are more prominent depending on what kind of pattern the image was convolved with. If a horizontal pattern was used, only the horizontal portions of the "VIP" image is shown (2nd to the left). The same is true for the vertical pattern. The spot pattern only shows portions containing 1 white pixel and the other pixels are merged with the background. That is why there are some holes in the letter V and P in particular upon close inspection.
____________________________________________________________________
hor = [-1 -1 -1; 2 2 2; -1 -1 -1]; //horizontal pattern
vert = [-1 2 -1; -1 2 -1; -1 2 -1]; //vertical pattern
spot = [-1 -1 -1; -1 8 -1; -1 -1 -1]; //spot pattern
pattern = hor;
vip = gray_imread('vip.png');
im = imcorrcoef(vip, pattern);
imshow(im);
ORIGINAL, HORIZONTAL EDGE, VERTICAL EDGE, SPOTS




As we can see, the edges are more prominent depending on what kind of pattern the image was convolved with. If a horizontal pattern was used, only the horizontal portions of the "VIP" image is shown (2nd to the left). The same is true for the vertical pattern. The spot pattern only shows portions containing 1 white pixel and the other pixels are merged with the background. That is why there are some holes in the letter V and P in particular upon close inspection.
____________________________________________________________________
I give myself 10 points for this activity since the all of the 4 objectives were met. I found this activity to be interesting and the techniques discussed here useful either for research and even for leisure. Thanks to Jeric Tugaff for help in the code for parts 3 and 4.
0 comments:
Post a Comment