Filtering in Frequency Domain
Filtering in Frequency Domain
1 Objective
This project aims to familiarize ourselves with the concepts of filtering images in the
frequency domain using MATLAB.
2.2 Create a black image, the size of the "pout.tif" image, that
has a white rectangular inside (choose an appropriate size for
the white rectangu- lar).
I_black = zeros ( size ( I ) ) ;
rows = length ( I (: ,1) ) ;
colomns = length ( I (1 ,:) ) ;
x_idx = round ( rows /2 - rows /4) : round ( rows /2 + rows /4) ;
y_idx = round ( colomns /2 - colomns /16) : round ( colomns /2 + colomns
/16) ;
I_black ( x_idx , y_idx ) = 255;
This code creates a black image with a white rectangular at the center of the same size
as "pout.tif".
2.3 Use ff2 to show the magnitude and phase of both images.
F_I = fftshift ( fft2 ( I ) ) ;
F_I_black = fftshift ( fft2 ( I_black ) ) ;
figure ;
subplot (2 ,2 ,[1 ,3])
imshow ( I )
title ( ’" pout . tif " ␣ Image ’)
subplot (2 ,2 ,2)
imagesc ( log10 ( abs ( F_I ) ) )
title ( ’ Magnitude ␣ ( Log ␣ Scale ) ’)
subplot (2 ,2 ,4)
imagesc ( angle ( F_I ) )
title ( ’ Phase ’)
colormap gray
figure ;
subplot (2 ,2 ,[1 ,3])
imshow ( uint8 ( I_black ) )
title ( ’ White ␣ rectangular ␣ image ’)
subplot (2 ,2 ,2)
imagesc ( log10 ( abs ( F_I_black ) ) )
title ( ’ Magnitude ␣ ( Log ␣ Scale ) ’)
1
2 The importance of Magnitude, and Phase of an image
subplot (2 ,2 ,4)
imagesc ( angle ( F_I_black ) )
title ( ’ Phase ’)
colormap gray
Both the "pout.tif" image and the white rectangular image have different magnitude
and phase patterns in the frequency domain. In Fig.1, the center of the magnitude plot
represents the intensity of the "pout.tif" image, while the phase looks chaotic, but contains
2
2 The importance of Magnitude, and Phase of an image
important details about the image. In Fig.2, since the rectangle is tall and narrow, its
magnitude response is the opposite, appearing wide horizontally and narrow vertically.
2.4 Construct a third image that has the magnitude of the white
rectangular image and the phase of the "pout.tif" image.
F_I_d = abs ( F_I_black ) .* exp (1 i * angle ( F_I ) ) ;
I_d = ifft2 ( ifftshift ( F_I_d ) ) ;
We just used the magnitude from the white rectangular image, which represents the image
intensity, and combined it with the phase from the "pout.tif" image, which holds most of
the important details about the image.
Figure 3: The magnitude of white rectangular image and the phase of "pout.tif" image.
3
3 Frequency Filtering
By applying the inverse Fourier transform, we reconstruct the image and notice that
the phase information from "pout.tif" controls the overall structure, even though the
magnitude comes from the white rectangular image. This confirms what we mentioned
in both sections. 2.4, and 2.3.
3 Frequency Filtering
3.1 Take the fft2 of the Gaussian filter in HW 2.
I = imread ( ’ pout . tif ’) ;
rows = length ( I (: ,1) ) ;
colomns = length ( I (1 ,:) ) ;
% a : From Hw2
N = 51;
sigma = 10;
K =1;
G_Kernel = [];
for i =1: N
for j =1: N
G_Kernel (i , j ) = K * exp ( -(( -( N +1) /2+ i ) ^2+( -( N +1) /2+ j ) ^2) /(2*
sigma ) ) ;
end
end
G_Kernel1 = G_Kernel ./ sum ( sum ( G_Kernel ) ) ;
I_G = imfilter (I , G_Kernel1 , ’ same ’) ; % spatial filtering
F_G_krnl = fftshift ( fft2 ( G_Kernel1 , rows , colomns ) ) ; % fft2 do zero
padding
The Gaussian kernel is created and transformed to the frequency domain using the ’fft2()’
function. The function ’fftshift()’ shifts the zero elements to zero.
3.3 Multiply (.*) both FFTs with each other. Then take the ifft2.
F_I_c = F_G_krnl .* F_I ; % frequency fitering ( N =11)
I_c = ifft2 ( ifftshift ( F_I_c ) ) ;
By multiplying the frequency responses of both images "pot.tif", and the Gaussian kernel,
we perform frequency domain filtering. The filtered image is then generated by applying
the inverse FFT using ‘ifft2()‘, but before that, we use ‘ifftshift()‘ to cancel the effect of
‘fftshift()‘.
4
3 Frequency Filtering
The filtered image appears smoother because of the reduction of high-frequency compo-
nents, indicating that a low-pass filtering process has been done. Noticing that since the
kernel size is N = 51, the filtered image exhibits aliasing effects, which can be seen in the
left corner and at the top.
5
3 Frequency Filtering
Figure 5: (a) "pout.tif" image filtered in frequency domain (using the procedure in
section.3.3), (b) "pout.tif" image filtered using the spatial filtering techniques.
A comparison is made between filtering in the spatial domain and filtering in the frequency
domain. The frequency domain exhibits aliasing, while the spatial domain shows the
effects of zero padding during convolution.
6
3 Frequency Filtering
The filtering process in the frequency domain is illustrated in Fig.6. The Fourier transform
of the Gaussian kernel results in another Gaussian in the frequency domain, as shown in
Fig.6(b), with the highest magnitude at the center. The original spectrum, shown in
Fig.6(a), is then multiplied by this Gaussian filter (since they are convolved in time),
resulting in the filtered image whose spectrum is displayed in Fig.6(c). For this reason,
the filtered images in the frequency domain do not exhibit the effects of zero padding, as
the convolution in time will average some of those zeros.