Freq Fil Expt 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

EXPERIMENT: 4

Aim: To implement De-noising and De-blurring algorithm in frequency domain


Software: MATLAB R2020a
Theory:
Filtering in the frequency domain consists of modifying the Fourier transform
of an image, then computing the inverse transform to obtain the spatial domain
representation of the processed result. Thus, given (a padded) digital image, f (x,
y), of size P × Q pixels, the basic filtering equation in which we are interested
has the form
� �, � = ���� ℑ−1 [ � �, � �(�, �) ]
Where, ℑ−1 is the IDFT, F (u, v) is the DFT of the input image, f (x,y), H(u ,v)
is a filter transfer function, and g (x,y) is the filtered (output) image.

Steps for filtering in frequency domain


The process of filtering in the frequency domain can be summarized as follows:
1. Given an input image f (x, y) of size M × N, obtain the padding sizes P and
Q i.e. P = 2M, Q= 2N.
2. Form a padded image fp (x, y) of size P × Q using zero-, mirror-, or replicate
padding
3. Multiply fp (x, y) by ( -1) x +y to centre the Fourier transform on the P × Q
frequency rectangle
4. Compute the DFT, F(u, v), of the image from Step 3
5. Construct a real, symmetric filter transfer function, H(u ,v), of size P × Q
with centre at ( P/2, Q/2 ).
6. Form the product G (u, v) = H(u, v) F(u, v) using elementwise multiplication
7. Obtain the filtered image (of size P × Q) by computing the IDFT of G (u, v):
�� �, � = ���� ℑ−1 �(�, �) (-1) x +y
8. Obtain the final filtered result, g (x, y), of the same size as the input image,
by extracting the M × N region from the top, left quadrant of gp(x , y).
In theory, the IDFT in Step 7 should be real because f (x, y) is real and H (u, v)
is real and symmetric. However, parasitic complex terms in the IDFT resulting
from computational inaccuracies that are not uncommon. Taking the real part of
the result takes care of that. Multiplication by (-1) x +y cancels out the
multiplication by this factor in Step 3.

Ideal Low Pass Filter


A 2-D low-pass filter that passes without attenuation all frequencies within a
circle of radius from the origin, and cuts off all frequencies outside this, circle is
called an ideal low-pass filter (ILPF); it is specified by the transfer function,

41
1 �� � �, � ≤ �0
� �, � =
0 �� � �, � > �0
Where, D0 is a positive constant, and D(u ,v) is the distance between a point (u,
v) in the frequency domain and the centre of the P × Q frequency rectangle;
that is
1
2 2 2
� �
� �, � = �− + �−
2 2

Gaussian Low Pass Filter


Gaussian low-pass filter (GLPF) transfer functions have the form,
2 2
� �, � = �− � (�,�)/2�0
where D0 is the cut-off frequency.

Butterworth Low Pass Filter (BLPF)


The transfer function of a Butterworth low pass filter (BLPF) of order n, with
cutoff frequency at a distance D0 from the centre of the frequency rectangle, is
defined as,
1
� �, � = 2�
� �, �
1+
�0
BLPF function can be controlled to approach the characteristics of the ILPF
using higher values of n, and the GLPF for lower values of n, while providing a
smooth transition in from low to high frequencies. Thus, we can use a BLPF to
approach the sharpness of an ILPF function with considerably less ringing.

Image Sharpening using High Pass Filters


Edges and other abrupt changes in intensities are associated with high-
frequency components; image sharpening can be achieved in the frequency
domain by high pass filtering, which attenuates low frequencies components
without disturbing high frequencies in the Fourier transform.
Ideal High Pass Filter
Subtracting a low pass filter transfer function from 1 yields the corresponding
high pass filter transfer function in the frequency domain,
��� �, � = 1 − ��� (�, �)
Where, ��� (�, �) is the transfer function of ILPF, thus
0 �� � �, � ≤ �0
� �, � =
1 �� � �, � > �0
Where, D0 is a positive constant, and D (u, v) is the distance between a point (u,
v) in the frequency domain and the centre of the P × Q frequency rectangle.

Gaussian High Pass Filter


42
Gaussian High-pass filter (GHPF) transfer functions have the form,
2 2
� �, � = 1 − �− � (�,�)/2�0
Where, D0 is the cut-off frequency.

Butterworth High Pass Filter (BHPF)


The transfer function of a Butterworth High pass filter (BHPF) of order n, with
cutoff frequency at a distance D0 from the centre of the frequency rectangle, is
defined as,
1
� �, � =
�0 2�
1+
� �, �
GHPF transfer functions in the frequency domain tend to have a broader “skirt”
than Butterworth functions of comparable size and cutoff frequency. Thus, we
would expect Butterworth spatial kernels to be “broader” than comparable
Gaussian kernels.

Qualitative analysis
The mean-square error (MSE) and the peak signal-to-noise ratio (PSNR) are
used to compare image compression quality.
1. Mean Square error (MSE)
The MSE represents the cumulative squared error between the compressed and
the original image, whereas PSNR represents a measure of the peak error. The
lower the value of MSE, the lower the error.
2
�,�
�1 �, � − �2 �, �
��� =
�∗ �

Where, M and N are the number of rows and columns in the input images

2. Peak Signal to Noise Ratio (PSNR)


The PSNR computes the peak signal-to-noise ratio, in decibels, between two
images. This ratio is used as a quality measurement between the original and
estimated image. The higher the PSNR, the better the quality of the compressed
or reconstructed image.
�2
���� = 10 ���10
���
Where, R is the maximum fluctuation in the input image data type.

43
RESULT:

44
45
MSE and PSNR for De-noising of Gaussian noise using different filters
Filter Type Cut- Order of filter Mean Square Peak Signal to Noise
off Error Ratio
freq. (MSE) (PSNR)
Ideal LPF 60 2 6.66 39.89
120 2 2.11 44.89
180 2 0.46 51.5
Ideal HPF 60 2 224.11 24.63
120 2 223.67 24.63
180 2 223.87 24.63
Butterworth LPF 60 2 6.1 40.28
60 4 6.49 40.01
60 6 6.56 39.96
Butterworth HPF 60 2 224.98 24.61
60 4 224.31 24.62
60 6 224.29 24.62
Gaussian LPF 60 2 5.41 40.8
120 2 0.85 48.81
180 2 0.16 56.09
Gaussian HPF 60 2 225.39 24.6
120 2 225.23 24.6
180 2 226.92 24.57

DISCUSSION:
 Ideal LPF: Given a cut-off frequency the LPF smoothens the output
image and its level depends on the cutoff frequency. When cutoff
frequency increases the filter in frequency domain tends to contain more
high frequency (as it’s distance from the center becomes more and the
more it’s away from the center more the higher frequency components
gets exposed) components thus retaining the noise which is of high
frequency. We can see it quantitatively via MSE and PSNR.
 Ideal HPF: Sharpens the output image and acts quite similar to LPF in
the sense that the less the cutoff frequency the more the operation is
effective (here, sharpening) as it’s frequency domain representation can
be seen as a negative of Ideal LPF so when it’s cutoff frequency is less it
can extract majority of high frequency components and the greater it
becomes the some high frequency components may get ignored. This is
visibly seen from output.
 Butterworth LPF: The output kind of resembles the theoretical results as
the lesser the order the denoising effect is prominent as it should be for
Gaussian LPF and as the order increases the it resembles the ideal LPF
which show effect of ringing while doing it’s operation. This is also seen
quantitatively as the value of MSE increases with order.

46
 Butterworth HPF: The HPF frequency domain representation turns out
to be a negative of LPF so the properties of LPF and HPF remain same
expect for their core operations i.e. sharpening the image.The image is
crisp with its edges being very prominent when the order is
low(approximating a Gaussian filter) and effectiveness decreases with
increase in order.
 Both the above filters provide flexibility and act as a bridge between
the Ideal ones and the Gaussian filters.
 Gaussian LPF: Like Ideal LPF this filter depends on the cutoff
frequency and it turns out it effectively carries out smoothing when cutoff
frequency is low i.e., it’s variance is less which means a narrow kernel in
spatial domain and here in frequency domain the spread stays limited a
tall neck in the center thus having no higher frequency components being
exposed.
 Gaussian HPF: brings a change in the formula of Gaussian LPF so that
the operation of HPF is performed other than which effects remain the
same as LPF in regard to effect of cutoff frequency on effectiveness of
operation.

CONCLUSION:
In this experiment we’ve successfully demonstrated Ideal, Butterworth and
Gaussian filter using frequency domain filtering techniques used for denoising
an image.

CODE:
clc
clear variables
close all

%Get image
A=imread("cameraman.tif");
[rows,cols]=size(A);
A_zero_pad=zeros(rows*2,cols*2);
A_zero_pad(rows-(rows/2):rows+(rows/2)-1,cols-(cols/2):cols+(cols/2)-
1)=A(:,:);
% A_zero_pad(128:383,128:383)=A(:,:);
[rows_new,cols_new]=size(A_zero_pad);
% figure
% imshow(uint8(A_zero_pad));
A_gauss=imnoise(A,'gaussian',0.2);
A_gauss_zero_pad=zeros(rows*2,cols*2);
A_gauss_zero_pad(rows-(rows/2):rows+(rows/2)-1,cols-(cols/2):cols+(cols/2)-
1)=A_gauss;
% A_gauss_zero_pad(128:383,128:383)=A_gauss;

new_A_gauss=zeros(rows_new,cols_new);

47
for i=1:rows_new
for j=1:cols_new
new_A_gauss(i,j)=A_gauss_zero_pad(i,j)*(-1)^(i+j);
end
end

fre_A=fft2(new_A_gauss);

%defining D(u,v)
D=zeros(rows_new,cols_new);
for i=1:rows_new
for j=1:cols_new
D(i,j)=(((i-(rows_new/2))^2+(j-(cols_new/2))^2)^(0.5));
end
end

ideal_low_pass=zeros(rows_new,cols_new);
prompt_='Enter the cutoff freq:';
cutoff_fre=input(prompt_);
for i=1:rows_new
for j=1:cols_new
if(D(i,j)<=cutoff_fre)
ideal_low_pass(i,j)=1;
end
end
end

ideal_high_pass=ones(rows_new,cols_new)-ideal_low_pass;

fre_out_low_pass=fre_A.*ideal_low_pass;
space_out_low_pass=real(ifft2(fre_out_low_pass));

for i=1:rows_new
for j=1:cols_new
space_out_low_pass(i,j)=(space_out_low_pass(i,j)*(-1)^(i+j));
end
end
space_out_low_pass=uint8(space_out_low_pass);

fre_out_high_pass=fre_A.*ideal_high_pass;
space_out_high_pass=real(ifft2(fre_out_high_pass));

for i=1:rows_new
for j=1:cols_new
space_out_high_pass(i,j)=(space_out_high_pass(i,j)*(-1)^(i+j));

end
end
space_out_high_pass=uint8(space_out_high_pass);

%ButterWorth
butter_high=zeros(rows_new,cols_new);
prompt_2='Enter the order of the filter:';
order=input(prompt_2);
for i=1:rows_new
for j=1:cols_new
butter_high(i,j)=((D(i,j)/cutoff_fre)^(-2*order)+1)^(-1);
end
end

48
fre_o_butter_high=butter_high.*fre_A;
space_o_butter_high=real(ifft2(fre_o_butter_high));

for i=1:rows_new
for j=1:cols_new
space_o_butter_high(i,j)=(space_o_butter_high(i,j)*(-1)^(i+j));
end
end
space_o_butter_high=uint8(space_o_butter_high);

butter_low=zeros(rows_new,cols_new);

for i=1:rows_new
for j=1:cols_new
butter_low(i,j)=((D(i,j)/cutoff_fre)^(2*order)+1)^(-1);
end
end

fre_o_butter_low=butter_low.*fre_A;
space_o_butter_low=real(ifft2(fre_o_butter_low));
for i=1:rows_new
for j=1:cols_new
space_o_butter_low(i,j)=(space_o_butter_low(i,j)*(-1)^(i+j));
end
end
space_o_butter_low=uint8(space_o_butter_low);

%All output cropped img


I_1_Id_LPF=imcrop(space_out_low_pass,[128,128,255,255]);
I_2_Id_HPF=imcrop(space_out_high_pass,[128,128,255,255]);
I_3_bu_LPF=imcrop(space_o_butter_low,[128,128,255,255]);
I_4_bu_HPF=imcrop(space_o_butter_high,[128,128,255,255]);
% GLPF
g_low=zeros(rows_new,cols_new);
for i=1:rows_new
for j=1:cols_new
g_low(i,j)=exp((-1)*(D(i,j)^2)/(2*cutoff_fre^2));
end
end

fre_o_g_low=g_low.*fre_A;
space_o_g_low=real(ifft2(fre_o_g_low));

for i=1:rows_new
for j=1:cols_new
space_o_g_low(i,j)=(space_o_g_low(i,j)*(-1)^(i+j));
end
end
space_o_g_low=uint8(space_o_g_low);
%GHPF
g_high=zeros(rows_new,cols_new);
for i=1:rows_new
for j=1:cols_new
g_high(i,j)=1-exp((-1)*(D(i,j)^2)/(2*cutoff_fre^2));
end
end

fre_o_g_high=g_high.*fre_A;

49
space_o_g_high=real(ifft2(fre_o_g_high));

for i=1:rows_new
for j=1:cols_new
space_o_g_high(i,j)=(space_o_g_high(i,j)*(-1)^(i+j));
end
end
space_o_g_high=uint8(space_o_g_high);

I_5_g_LPF=imcrop(space_o_g_low,[128,128,255,255]);
I_6_g_HPF=imcrop(space_o_g_high,[128,128,255,255]);

idlpf_mse=MSE_res(A,I_1_Id_LPF,rows,cols);
idlpf_psnr=PSNR_res(idlpf_mse);
idhpf_mse=MSE_res(A,I_2_Id_HPF,rows,cols);
idhpf_psnr=PSNR_res(idhpf_mse);
blpf_mse=MSE_res(A,I_3_bu_LPF,rows,cols);
blpf_psnr=PSNR_res(blpf_mse);
bhpf_mse=MSE_res(A,I_4_bu_HPF,rows,cols);
bhpf_psnr=PSNR_res(bhpf_mse);
glpf_mse=MSE_res(A,I_5_g_LPF,rows,cols);
glpf_psnr=PSNR_res(glpf_mse);
ghpf_mse=MSE_res(A,I_6_g_HPF,rows,cols);
ghpf_psnr=PSNR_res(ghpf_mse);
figure
subplot(2,6,3);
imshow(A);
title("Original Image","FontSize",13);
subplot(2,6,4);
imshow(A_gauss);
title("Gaussian noise added image","FontSize",13);
subplot(2,6,7)
imshow(I_1_Id_LPF);
title("Ideal LPF","FontSize",13);
xlabel(sprintf("MSE:%0.2f PSNR:%0.2f\n Cutoff-
freq:%0.1f",idlpf_mse,idlpf_psnr,cutoff_fre),"FontSize",13);
subplot(268)
imshow(I_2_Id_HPF);
title("Ideal HPF output","FontSize",13);
xlabel(sprintf("MSE:%0.2f PSNR:%0.2f\n Cut-off
freq:%0.1f",idhpf_mse,idhpf_psnr,cutoff_fre),"FontSize",13);
subplot(2,6,9)
imshow(I_3_bu_LPF);
title("Butterworth LPF output","FontSize",13);
xlabel(sprintf("MSE:%0.2f PSNR:%0.2f\n
Order:%0.1f",blpf_mse,blpf_psnr,order),"FontSize",13);
subplot(2,6,10)
imshow(I_4_bu_HPF);
title("Butterworth HPF output","FontSize",13);
xlabel(sprintf("MSE:%0.2f PSNR:%0.2f\n
Order:%0.1f",bhpf_mse,bhpf_psnr,order),"FontSize",13);
subplot(2,6,11)
imshow(I_5_g_LPF);
title("Gaussian LPF output","FontSize",13);
xlabel(sprintf("MSE:%0.2f PSNR:%0.2f \nCut-off
freq:%0.1f",glpf_mse,glpf_psnr,cutoff_fre),"FontSize",13);
subplot(2,6,12);
imshow(I_6_g_HPF);
title("Gaussian HPF output","FontSize",13);
xlabel(sprintf("MSE:%0.2f PSNR:%0.2f \nCut-off
freq:%0.1f",ghpf_mse,ghpf_psnr,cutoff_fre),"FontSize",13);

50

You might also like