Saturday, September 18, 2010

Activity 6 - Fourier Transform Model of Image Formation

To become familiarized with discrete FFT, a 128 x 128 image of a white centered-circle with black background was created (see Figure 1.a.). This image was converted to grayscale and then the fourier transform of this 2D image was taken. The intensity values were calculated and are plotted as shown in Figure 1.b. The fftshift() function was used to shift the peak onto the center of the image as shown in Figure 1.c Lastly, the fft2() was applied to the image in Figure 1.c and obtained an image as shown in Figure 1.d, which is similar to the original image. This method was done the same with a 128 x 128 image of a letter "A" as shown Figure 2. Observe that the image shown in Figure 2.d is an inverted image of Figure 2.a.

stacksize(100000000);

PATH = 'C:\Users\Micent\Documents\Applied Physics\acad 2010-2011 1st sem\App Phy 186 - Instrumentation Physics II\Activity 6\';

//centered circle

x = linspace(-64,64,128);

[X,Y] = meshgrid(x);

r = sqrt(X.^2 + Y.^2);

circle = zeros(128,128);

circle = zeros(size(X,1),size(X,1));

circle(find(r<=16)) = 1.0;

//imshow(circle,[]);

imwrite(circle,strcat([PATH,'circle_16.bmp']));


(a)
(b)
(c)
(d)

Figure 1. Results from the Simulation of an imaging devive for a circle.



//image

filename = 'circle'

//filename = 'letterA'

Igray = gray_imread(strcat([PATH,filename,'.bmp']));

//fast fourier transform for 2D

FIgray = fft2(Igray);
//scf(1); imshow(abs(FIgray),[]);
//xs2bmp(1,strcat([PATH,filename,'_FIgray','.bmp']));
Im = abs(FIgray);
Im = (Im - min(Im))/(max(Im)-min(Im));
imwrite(Im,strcat([PATH,filename,'_FIgray','.bmp']));


//shift the FT

shiftFIgray = fftshift(abs(FIgray));
//scf(2); imshow(shiftFIgray), []);
//xs2bmp(2,strcat([PATH,filename,'_FIgray_shift','.bmp']));
Im = shiftFIgray;
Im = (Im - min(Im))/(max(Im)-min(Im));
imwrite(Im,strcat([PATH,filename,'_FIgray_shift','.bmp']));

//fast fourier transform

FIgray2 = fft2(FIgray);
//scf(3); imshow(abs(FIgray2),[]);
//xs2bmp(3,strcat([PATH,filename,'_FIgray2','.bmp']));
Im = abs(FIgray2);
Im = (Im - min(Im))/(max(Im)-min(Im));
imwrite(Im,strcat([PATH,filename,'_FIgray2','.bmp']));

(a)
(b)
(c)
(d)

Figure 2. Results from the Simulation of an imaging devive for a letter A.




In the second part of the activity, convolution was used for the simulation of an imaging device. Given two 2D functions f and g, the convolution between this functions is


Equation 1


Equation 1 can be similarly written in Equation 2 with "*" as the shorthand notation for convolution.


Equation 2


Following the convolution theorem, f and g can be recasted by linear transformations, such as Laplace or Fourier transform, and with H, F, and G as the transforms of h, f and g, respectively, Equation 3 can be obtained


Equation 3



Figure 3. Image of the letters "VIP" in a 128 x 128 black background.



Figure 3 shows a 128 x 128 image of the letters "VIP", which served as an object for the simulation. A 128 x 128 image of a white centered-circle was created over a black background. This circle represented the aperture of a circular lens. Both image were converted to grayscale. The 2D fourier transform of the object (VIP image) was taken and fftshift() was only applied to the aperture since it is already in the Fourier Plane. The product of their FFT is calculated and then the inverse of the product was determined to obtain the convolved image.

The process was repeated for different radii of the white circle such as 16 px, 32 px, 48 px and 64 px. Figure 4 shows the different outputs as the radius of the circular lens was varied. The radius of the aperture of lens shows that its size affects the quality of the image formed. The smallest radius produced a low quality image because the lens can only gather a limited number of rays.

filename1 = 'circle_16'

filename2 = 'VIP';

I = gray_imread(strcat([PATH,filename1,'.bmp'])); //circle

VIP = gray_imread(strcat([PATH,filename2,'.bmp'])); //vip

FI = fftshift(I);

FVIP = fft2(VIP);


FRVIP = FI.*FVIP;

IRVIP = fft2(FRVIP); //inverse FFT

VIPimage = abs(IRVIP);

Im = VIPimage;

Im = (Im - min(Im))/(max(Im)-min(Im));

imwrite(Im,strcat([PATH,filename2,'_JImage_16','.bmp']));



(a)
(b)
(c)
(d)

Figure 4. The convolution of the circular apertures with the VIP image

resulted to the images on the right column.




For the third part this actvity, correlation function was used in pattern recognition technique called template matching in which identical patterns in a scene are being looked for. The correlation between two 2D functions f and g si given by


Equation 4


or in shorthand notation


Equation 5


Equation 6 shows how Equation 5 is related to the convolution integral, where the superscript asterisk at function f means complex conjugation.


Equation 6


Equation 7 was derived from the correlation theorem which holds for the linear transforms of functions f and g. P, F, and G ar the Fourier transforms of p, f, and g, respectively, and the the superscript asterisk stands for complex conjugation.


Equation 7


The correlation measures the degree of similarity betwee two functions. That is, the more identical they are at certain positions (x,y), the higher correlation value.



Figure 5. Image of a sentence over a white background.

Figure 6. Pattern need to be matched in the image in Figure 5.




filename1 = 'sentence';

filename2 = 'A'


S = gray_imread(strcat([PATH,filename1,'.bmp'])); //sentence

A = gray_imread(strcat([PATH,filename2,'.bmp'])); //letter A


FA = fft2(A); //G

FS = fft2(S); //F

R = FA.*conj(FS); //F*G

output = fftshift(fft2(R));

Im = abs(output);

Im = (Im - min(Im))/(max(Im)-min(Im));

imwrite(Im,strcat([PATH,filename1,'_output','.bmp']));


FA = fftshift(fft2(A));

FS = fftshift(fft2(S));

R = FA.*conj(FS);

output = (fft2(R));

Im = abs(output);

Im = (Im - min(Im))/(max(Im)-min(Im));

imwrite(Im,strcat([PATH,filename1,'_output2','.bmp']));

Figure 5 shows a 128 x 128 image with white background was created with the text "THE RAIN IN SPAIN STAYS MAINLY IN THE PLAIN." Another image of size 128 x 128 was created with letter "A" of the same font and size in the text in Figure 6. Both these images were imported in Scilab and converted to grayscale. Their Fourier transforms were determined. To calculate the correlation of the two transforms, element-per-element multiplication of the FT of "A" and the conjugate of the FT of the text image was performed. Lastly, the inverse FFT of the correlation was determined and the output is diplayed in Figure 7.



Figure 7. Result of the correlation of image in Figure 5 and 6.


Comparing Figure 5 and Figure 7, it can be observed that the peaks locations show where the letter "A" can be found.

In the last part of the activity, convolution integral was applied in the template matching of an edge pattern with an image. A 3x3 matrix pattern of an edge was created such that the total sum of the elements is zero. The image formed was convolved with the VIP image using the imcorrcoef() function of Scilab. Figure 8 shows the result of using different patterns and their corresponding convolved image.

filename2 = 'VIP';

VIP = gray_imread(strcat([PATH,filename2,'.bmp'])); //vip


pattern1 = [-1 -1 -1; 2 2 2; -1 -1 -1];

pattern2 = [-1 2 -1; -1 2 -1; -1 2 -1];

pattern3 = [-1 -1 -1; -1 8 -1; -1 -1 -1];

output = imcorrcoef(VIP,pattern3);

Im = abs(output);

Im = (Im - min(Im))/(max(Im)-min(Im));

imwrite(Im,strcat([PATH,'convolve_output3','.bmp']));



(a)
(b)
(c)
Figure 8. Edge detected using three differe nt matrix patterns.


For Figure 8.a., matrix pattern [-1 -1 -1; 2 2 2; -1 -1 -1] was used that is why horizontal edges brighten up. In Figure 8.b, the convolution of matrix pattern [-1 2 -1; -1 2 -1; -1 2 -1] with the VIP image resulted to the higlighting of the vertical edges. The use of matrix patter [-1 -1 -1; -1 8 -1; -1 -1 -1] resulted to the detection of the entire edges of the VIP image.


Reference:

[1] M.N. Soriano, "Applied Physics 186 - Fourier Transform Model of Image Formation",2010.


Credits:
Self Evaluation:

No comments:

Post a Comment