Professional Documents
Culture Documents
Freq Fil Expt 4
Freq Fil Expt 4
Freq Fil Expt 4
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
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
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);
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