Thursday, September 23, 2010

Activity 8 - Enhancement in the Frequency Domain

In this activity, convolution theorem was applied in order to mask frequencies in the Fourier domain of an image with unwanted repetitive patterns. Filter masks were created to block out frequencies that produce unwanted patterns in an image. Recall that:


(1) The Fourier transform of a convolution of two functions in space is the product of the two functions' Fourier transform, that is, FT[f*g] = FG.

(2) The convolution theorem of a dirac delta and a function f(t) in a replication of f(t) in the location of the dirac delta, that is,


Equation 1

In the first part of the activity, the fourier transform of the binary images of two dots, two circles, two squares and two gaussians were taken and their corresponding modulus were observed (see Figure 1-4).



(a)

(b)

Figure 1. (a) Binary image of two dots along the x-axis and symmetric about the center.

(b) Modulus of the Fourier Transform




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

Figure 2. Binary image of two circles along the x-axis symmetric about the center

and modulus of the Fourier transform.

(a) radius = 2, (b) radius = 4, (c) radius = 8, (d) radius = 16, (e) radius = 32




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

Figure 3. Binary image of two squares along the x-axis symmetric about the center

and modulus of the Fourier transform.

(a) width = 2, (b) width = 4, (c) width = 8, (d)width = 16, (e) width = 32




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

(e)

Figure 4. Binary image of two Gaussians along the x-axis symmetric about the center

and modulus of the Fourier transform.

(a) variance = 2, (b) variance = 4, (c) variance = 8, (d)variance = 16, (e) variance = 32



Figure 5 shows the convolution of two matrices, 200x200 matrix A with 10 1's randomly located and 3x3 matrix d with a certain pattern. The 3x3 matrix used is defined as d = [1 0 1; 0 1 0; 1 0 1]. The convolution was taken by using the imconv() function of Scilab.



Figure 5. Convolution of two matrices.

200x200 matrix A with 10 1's randomly located and 3x3 matrix d with a certain pattern




(a)
(b)
(c)

Figure 6. Convolution of two matrices.

200x200 matrix A with 1's equally spaced and 3x3 matrix d with a certain pattern.

(a) 2 px apart, (b) 4 px apart, (c) 20 px apart


Figure 6 shows the Fourier transform and modulus of a 200x200 matrix with equally spaced 1's along x and y axis. Also, the results when the spacings were varied are also shown.



As mentioned, convolution theorem may be applied in order to remove unwanted repetitive patterns by enhancing the frequency domain of the image of interest. In this part of the activity, three images were used to illustrate how convolution theorem may be used.


Figure 7.a (left) shows an image of a fingerprint (downloaded from the internet). Figure 7.a (right) shows the an image of the same fingerprint with enhanced appearance of the ridges and absence of blothes. The image was enhanced by masking certain frequencies in the domain using gaussian function (see Figure 7.b) .



(a)

(b)

Figure 7. (a) Original Fingerprint (left) and Enhance Fingerprint (right).

(b) Filter Mask and the Frequency Domain

(taken from: http://www.papillon.ru/files/image/content/2008/pic/L_scan/smaz_paper_small.png)



Figure 8.a shows an image of the surface of the moon taken in 1967 prior to the Apollo missions. This image was further improved when the vertical lines were removed by filtering in the Fourier domain. Figure 8.b shows the matrix used to mask the unwanted frequency (horizontal line in the domain). Using the convoluton theorem, we obtained the new frequency in Figure 8.b.



(a)

(b)
Figure 8. " The two groups of irregularly shaped craters north and west of the landing site are secondaries from Sabine Crater. This view was obtained by the unmanned Lunar Orbiter V spacecraft in 1967 prior to the Apollo missions to the Moon. The black and white film was automatically developed onboard the spacecraft and subsequently digitized for transmission to Earth. The regularly spaced vertical lines are the result of combining individually digitized 'framelets' to make a composite photograph and the irregularly-shaped bright and dark spots are due to nonuniform film development. [NASA Lunar Orbiter photograph]".



Lastly, canvas weave patterns in a subimage of an oil painting from the UP Vargas Museum Collection (see Figure 9.a were removed by using the filter mask shown in Figure 9.b. Figure 10 shows an image when the inverse fourier transform the inverted filter mask was determined. The modulus was also determined and the image shown is seemed to be similar with the observed canvas weave patterns.



(a)
(b)

Figure 9. (a) canvas weave pattern removed show the brush strokes.


Figure 10. Canvas Weave Pattern


Self-Evaluation:

Credits:


Appendix: Source Code

/Activity 8.B Fingerprints : Ridge Enhancement

filename = 'fingerprintsmall';

//http://www.papillon.ru/files/image/content/2008/pic/L_scan/smaz_paper_small.png

fingerprint = gray_imread(strcat([PATH,filename,'.png']));


FT_fingerprint = abs(fftshift(fft2(fingerprint)));

Im = FT_fingerprint;

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

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


FT_fingerprint2 = log(abs(fftshift(fft2(fingerprint))));

Im = FT_fingerprint2;

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

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


//filter mask

[m,n] = size(fingerprint);

x = linspace(-m/2,m/2,m); //defines the range

y = linspace(-n/2,n/2,n);

[X,Y] = ndgrid(x,y); //creates two 2-D arrays of x and y coordinates

u = 0; //peak location

v = 0;

s1 = 84; //variance

FM1 = exp((-((X-u).^2)/s1^2) + (-((Y-v).^2)/s1^2));

s2 = 48;

FM2 = exp((-((X-u).^2)/s2^2) + (-((Y-v).^2)/s2^2));

s3 = 2;

FM3 = exp((-((X-u).^2)/s3^2) + (-((Y-v).^2)/s3^2));

filter_mask = FM1 - FM2 + FM3;

filter_mask(m/2,n/2) = 1;

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


//filtering

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

filtered_FFT = filter_mask.*FT_fingerprint2;

//filtered_FFT = imconv(fingerprint,filter_mask);

Im = (filtered_FFT);

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

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


//enhancement

enhanced = abs(ifft(fftshift(filter_mask).*fft2(fingerprint)));

Im = (enhanced);

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

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



//difference of original to processed

original = gray_imread(strcat([PATH,filename,'.png']));

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

Imdiff = original - processed;

imwrite(Imdiff, strcat([PATH,'difference_',filename,'.bmp']));







//Activity 8.C Lunar Landing Scanned Pictures : Line removal

filename = 'lunar';

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

imwrite(Lunar, strcat([PATH,filename,'_grayscale','.bmp']));

[m,n] = size(Lunar);


//Fourier transform

FT_lunar = abs(fftshift(fft2(Lunar)));

Im = FT_lunar;

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

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


FT_lunar2 = log(abs(fftshift(fft2(Lunar))));

Im = FT_lunar2;

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

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


//vertical lines filter

filter_mask = zeros(m,n);

kx = 2;

ky = 16; //affectst the contrast

filter_mask(m/2-kx:m/2+kx,1:n/2-ky) = 1;

filter_mask(m/2-kx:m/2+kx,n/2+ky:n) = 1;

//filter_mask(1:m/2-ky,n/2-kx:n/2+kx) = 1;

//filter_mask(m/2+ky:m,n/2-kx:n/2+kx) = 1;

filter_mask = 1 - filter_mask;

imwrite(filter_mask, strcat([PATH,filename,'_filter_mask','.bmp']));


//filtering

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

filtered_FFT = filter_mask.*FT_lunar2;

//filtered_FFT = imconv(Lunar,filter_mask);

Im = (filtered_FFT);

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

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


//enhancement

enhanced = abs(ifft(fftshift(filter_mask).*fft2(Lunar)));

Im = (enhanced);

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

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


//difference of original to processed

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

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

Imdiff = original - processed;

imwrite(Imdiff, strcat([PATH,filename,'_difference','.bmp']));




//Activity 8.D Canvas Weave Modeling and Removal

filename = 'canvasweave';

canvasweave = gray_imread(strcat([PATH,filename,'.jpg']));

imwrite(canvasweave, strcat([PATH,filename,'_grayscale','.bmp']));


//Fourier transform

FT_canvasweave = abs(fftshift(fft2(canvasweave)));

Im = FT_canvasweave;

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

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


FT_canvasweave2 = log(abs(fftshift(fft2(canvasweave))));

Im = FT_canvasweave2;

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

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


//circular filter

[m,n] = size(canvasweave);

x = linspace(1,m,m); //defines the range

y = linspace(1,n,n);

[X,Y] = ndgrid(x,y); //creates two 2-D arrays of x and y coordinates

filter_mask = ones(m,n);

R = 6;

v = [290 290 290 290 244 335 244 336 198 381]; //peak location

u = [163 115 256 302 186 185 233 231 210 208];

for i = 1:10

r = sqrt((X-u(1,i)).^2 + (Y-v(1,i)).^2);

filter_mask(find(r

end

imwrite(filter_mask, strcat([PATH,filename,'_filter_mask','.bmp']));


filter_mask = 1 - filter_mask;

inv_filter_mask = log(abs(fftshift(fft2(filter_mask))));

Im = (inv_filter_mask);

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

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


//filtering

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

filtered_FFT = filter_mask.*FT_canvasweave2;

//filtered_FFT = imconv(canvasweave,filter_mask);

Im = (filtered_FFT);

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

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


//enhancement

enhanced = abs(ifft(fftshift(filter_mask).*fft2(canvasweave)));

Im = (enhanced);

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

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


//difference of original to processed

original = gray_imread(strcat([PATH,filename,'.jpg']));

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

Imdiff = original - processed;

imwrite(Imdiff, strcat([PATH,filename,'_difference','.bmp']));


No comments:

Post a Comment