Dip-Lab#11: Frequency Domain Filtering: Objective
Dip-Lab#11: Frequency Domain Filtering: Objective
OBJECTIVE:
The objective of this lab is to implement and understand frequency domain filtering and work
on Ideal, Gaussian and Butterworth filters to apply low pass filtering on images.
INTRODUCTION:
Frequency domain filtering of digital images involves conversion of digital images from
spatial domain to frequency domain. Frequency domain image filtering is the process of
image enhancement other than enhancement in spatial domain, which is an approach to
enhance images for specific application. In this paper we discussed about frequency domain
approach of image filtering. As normally images are in spatial domain, so it needed its
conversion in frequency domain. Fast Fourier transform is a specific tool for spatial domain
to frequency domain conversion. For image smoothing, we implement low pass filters while
for image sharpening we implement high pass filters. Both low pass and high pass filtering
implemented and analyze for Ideal filter, Butterworth filter and Gaussian filter. Filtering in
frequency domain is based on modifying the Fourier transform of image of interest to achieve
frequency domain filtering and then get back spatial domain image by taking inverse Fourier
transform of resultant filtered image.
TASK#01: Shrink the image man.png to 1/8 size. First without smoothing and then by
applying smoothing first and then shrinking. Now zoom/expand both the images back to
the original size. Which method give better result and why?
MATLAB CODE:
clc
clear all
close all
I=imread('cameraman.tif');
subplot(321)
imshow(I)
title('Original Image')
[r,c]=size(I);
I2(1:r/8, 1:c/8) = I(1:8:r, 1:8:c);
subplot(322)
imshow(I2)
title('Shrinked Image')
% Smooth the Original Image
gaussmask = fspecial('gaussian',3,2);
filt_img = imfilter(I,gaussmask);
subplot(323)
imshow(filt_img)
title('Original Filtered Image')
% Now shrink to the original Image
filtimg1(1:r/8, 1:c/8) = filt_img(1:8:r, 1:8:c);
subplot(324)
imshow(filtimg1)
title('Original Shrinked Image')
A=input('Enter the Zooming Factor')
h1=imresize(I2,A,'bilinear');
subplot(325)
imshow(h1)
title('Zoomed Original Image')
A=input('Enter the Zooming Factor')
h2=imresize(filtimg1,A,'bilinear');
subplot(326)
imshow(h2)
title('Zoomed Shrinked Image')
OUTPUT:
Enter the Zooming Factor 2; A = 2
Enter the Zooming Factor 3; A = 3
COMMENT: From the above outputs, it has been seen that by shrinking the image, no of
pixels in an image is reduced and image resolution is changed and by zooming the shrink
image, the resulting image quality gets degraded.
TASK#02:
Compute the Fourier transform of square/rectangle image using the MATLAB fft2
function. Display the magnitude and phase as an image.
MATLAB CODE:
clc
clear all
close all
A=ones([100,50]);
B=zeros([300,150]);
B(100:199,50:99)=A(1:100,1:50);
subplot(231)
imshow(B)
title('Original Image')
F=fft2(B);
subplot(232)
imshow(F)
title('Apply fft')
S=abs(F);
subplot(233)
imshow(S)
title('Magnitude Response')
S2=log(1+abs(F));
subplot(234)
imshow(S2)
title('Apply Log')
angle=angle(F);
subplot(235)
imshow(angle)
title('Phase Response')
OUTPUT:
TASK#03:
Apply different cutoff frequencies 10, 30, 60 etc, ILPF(Ideal low Pass Filter) filters on
the image test.tif. Comment on the output of different size filter. Plot the filter along
with the result too. Remember to apply different steps of frequency domain filtering i.e.
zero padding, centering, dft, generate frequency domain ILPF filter H(u,v),
multiplication of dft with frequency domain ILPF filter H(u,v), inverse dft, reversing the
centering, removing the padding.
MATLAB CODE:
clc
clear all
close all
I=double(imread('test.tif'));
% I1=double(imread('cameraman.tif'));
subplot(231)
imshow(uint8(I));
title('Original Image')
[M N]=size(I);
z=zeros(M,N);
I_padded=[I z;z z];
subplot(232);imshow(uint8(I_padded));title('Padded Image')
[r c]=size(I_padded);
for i=1:r
for j=1:c
centered_image(i,j)=(-1)^(i+j).*I_padded(i,j);
end
end
subplot(233)
imshow(uint8(centered_image))
title('Centered Image')
FT=fft2(centered_image);
COMMENT:
This output is the result of applying an ideal low pass filter to the noisy image with the cut-
off frequency being 10.Although we managed to reduce the high frequency noise, this image
is of no practical use. We lost too many of the fine-scale details and the image exhibits strong
ringing due to the shape of the ideal low pass filter.
When cutoff frequency is 30:
COMMENT:
This output is the result of applying the same filter with a cut-off frequency of being 30.
Since this filter keeps a greater number of frequencies, more details remain in the output
image. The image is less blurred, but also contains more noise. The ringing is less severe, but
still exists.
COMMENT:
This output is the result of applying the same filter with a cut-off frequency of being 60.
Since this filter keeps a greater number of frequencies, more details remain in the output
image. The image is no more blurred. There is no ringing effect.
TASK#04:
Apply different cutoff frequencies 10, 30, 60 etc and order 1, 2, 3 etc BLPF(Butterworth
Low Pass Filter) filters on the image test.tif. Compare with the outputs in question-3.
Plot the filter along with the result too.
MATLAB CODE:
clc
clear all
close all
I=double(imread('test.tif'));
% I1=double(imread('cameraman.tif'));
subplot(231)
imshow(uint8(I));
title('Original Image')
[M N]=size(I);
z=zeros(M,N);
I_padded=[I z;z z];
subplot(232);imshow(uint8(I_padded));title('Padded Image')
[r c]=size(I_padded);
for i=1:r
for j=1:c
centered_image(i,j)=(-1)^(i+j).*I_padded(i,j);
end
end
subplot(233)
imshow(uint8(centered_image))
title('Centered Image')
FT=fft2(centered_image);
% Butter-worth low pass filter
[m n]=size(FT);
filter=zeros(m,n);
for i=1:m
for j=1:n
D(i,j)=sqrt((i-m/2)^2+(j-n/2)^2);
OUTPUT:
When cutoff frequency is 10 and filter order is 1:
COMMENT:
This output is the result of applying the Butterworth filter with a cut-off frequency of being
10 and order 1. This image doesn't show any visible ringing and only little noise. However, it
also lost some image information, i.e. the edges are blurred and the image contains fewer
details than the original.
When cutoff frequency is 30 and filter order is 2:
COMMENT:
In order to retain more details, we increase the cut-off frequency to 30 and order is 2. This
image is less blurred, but also contains a reasonable amount of noise. In general, when using
a low pass filter to reduce the high frequency noise, we have to compromise some desirable
high frequency information if we want to smooth away significant amounts of noise.
COMMENT:
In order to retain more details in an image, we increase the cut-off frequency to 60 and order
is 3. Since this filter keeps a greater number of frequencies, more details remain in the output
image. The image is no more blurred. The ringing effect is severe by increasing the order of
filter.
TASK#05:
Apply different cutoff frequencies 10, 30, 60 etc GLPF(Gaussian Low Pass Filter) filters
on the image test.tif.
MATLAB CODE:
clc
clear all
close all
I=double(imread('test.tif'));
% I1=double(imread('cameraman.tif'));
subplot(231)
imshow(uint8(I));
title('Original Image')
[M N]=size(I);
z=zeros(M,N);
I_padded=[I z;z z];
subplot(232);imshow(uint8(I_padded));title('Padded Image')
[r c]=size(I_padded);
for i=1:r
for j=1:c
centered_image(i,j)=(-1)^(i+j).*I_padded(i,j);
end
end
subplot(233)
imshow(uint8(centered_image))
title('Centered Image')
FT=fft2(centered_image);
% Guassian Low Pass Filter
[m n]=size(FT);
filter=zeros(m,n);
for i=1:m
for j=1:n
D(i,j)=sqrt((i-m/2)^2+(j-n/2)^2);
COMMENT:
In order to retain more details in an image, we increase the cut-off frequency to 30 and order
is 2.The above image is less blurred from the above output.
TASK#06:
Compare with the outputs in question-3, question-4 and question-5. Plot the filter along
with the result too.
If we compare all the outputs which were done above, we conclude that by increasing the
cutoff frequencies and filter order in ideal low pass filter, Butterworth low pass filter and
Gaussian low pass filter the burred images get smooth and if we talk about ringing effect in
the filtered image which was plotted in all the results, was more in ideal low pass filter and
lesser effect in Butterworth filter and has no effect in Gaussian low pas filter.