Frequency Domain Filtering by Convolution Theorem

8 views (last 30 days)
Matthew Worker on 15 Oct 2021
Commented: Rena Berman on 8 Dec 2021
So the program works but i get warning messages, i attached the imag as well
%% 4. Perform frequency domain filtering by Convolution Theorem
%a)perform frequency filtering using im,imz and lpfz according to the procedure given
%first figure: f and gp
% second figure: fp to the IDFT result
% Step1: Given an input image f(x,y) of size M x N, obtain the pading
% parameters P and Q. Typically, we select P = 2M and Q = 2N
im=imresize(imm,[226 187]);
% Converting the image class into "double"
b = im2double(im);
[m,n] = size(b);
% creating a null array of size 2m X 2n
c = zeros(2*m,2*n);
% reading the size of the null array
[p,q] = size(c);
% Step 2: appdending the original image with the null array to create a padding image
for i = 1:p
for j = 1:q
if i <= m && j<= n
c(i,j) = b(i,j);
else
c(i,j) = 0;
end
end
end
imshow(b);title('original image');
figure;
% Step 3
% creating a null array of size p X q
d = zeros(p,q);
% Multiplying the padded image with (-1)^(x+y)
for i = 1:p
for j = 1:q
d(i,j) = c(i,j).*(-1).^(i + j);
end
end
figure;
imshow(d);title('pre processed image for calculating DFT');
% Step 4
% Computing the 2D DFT using "fft2" matlab command
e = fft2(d);
figure;imshow(e);title('2D DFT of the pre processed image');
% Step 5
% Generating the Real, Symmetric Filter Function
% Here we will implement a "Low Pass Filter" using "freqspace" matlab command
[x,y] = freqspace(p,'meshgrid');
z = zeros(p,q);
for i = 1:p
for j = 1:q
z(i,j) = sqrt(x(i,j).^2 + y(i,j).^2);
end
end
% Choosing the Cut off Frequency and hence defining the low pass filter mask
H = zeros(p,q);
for i = 1:p
for j = 1:q
if z(i,j) <= 0.4 % here 0.4 is the cut-off frequency of the LPF
H(i,j) = 1;
else
H(i,j) = 0;
end
end
end
% Step 6:Form the product G(u,v) = H(u,v)F(u,v) using array multiplication
% Obtain the processed image from the previous program lines we know that,
% e : the 2D DFT output of pre processed image
% H : the mask for Low Pass Filter
% let out is the variable
h1 = e.*H;
figure;
imshow(h1);title('Low passed output');
% Step 7: gp(x,y) = {real{inverse DFT[G(u,v)]}(-1)^(x+y)
% calculation of inverse 2D DFT of the "out"
h2 = ifft2(h1);
figure;
imshow(h2);title('output image after inverse 2D DFT');
% post process operation
h3 = zeros(p,q);
for i = 1:p
for j = 1:q
h3(i,j) = h2(i,j).*((-1).^(i+j));
end
end
figure;
imshow(h3);title('Post Processed image');
% Step 8: Obtain the final processed result g(x,y) by extracting the M X N region from the top, left quadrant of gp(x,y)
% let the smoothed image (or low pass filtered image) be "out"
out = zeros(m,n);
for i = 1:m
for j = 1:n
out(i,j) = h3(i,j);
end
end
figure;
imshow([b out]);title('input image output image');
%b)Why do you multiply by (-1)^x+y ? If you omit both of these steps, explain how the final result will be affected.
%If we did not multiply by (-1)^x+y in order to better see the result.
%If we had not we would have had to apply the shift function to see the result of the fourier transform Fp
and this is the message i get, is it an error because i did something wrong or can i leave it as it is?
Warning: Displaying real part of complex input.
> In images.internal.imageDisplayValidateParams>validateCData (line 146)
In images.internal.imageDisplayValidateParams (line 30)
In images.internal.imageDisplayParseInputs (line 79)
In imshow (line 253)
In script (line 84)
Warning: Displaying real part of complex input.
> In images.internal.imageDisplayValidateParams>validateCData (line 146)
In images.internal.imageDisplayValidateParams (line 30)
In images.internal.imageDisplayParseInputs (line 79)
In imshow (line 253)
In script (line 116)
Warning: Displaying real part of complex input.
> In images.internal.imageDisplayValidateParams>validateCData (line 146)
In images.internal.imageDisplayValidateParams (line 30)
In images.internal.imageDisplayParseInputs (line 79)
In imshow (line 253)
In script (line 122)
Warning: Displaying real part of complex input.
> In images.internal.imageDisplayValidateParams>validateCData (line 146)
In images.internal.imageDisplayValidateParams (line 30)
In images.internal.imageDisplayParseInputs (line 79)
In imshow (line 253)
In script (line 131)
Warning: Displaying real part of complex input.
> In images.internal.imageDisplayValidateParams>validateCData (line 146)
In images.internal.imageDisplayValidateParams (line 30)
In images.internal.imageDisplayParseInputs (line 79)
In imshow (line 253)
In script (line 142)
Rena Berman on 8 Dec 2021

Matt J on 15 Oct 2021
They are warnings, not errors, but if you expect your FFT/IFFT to be real-valued due to symmetry, you can do
h2 = ifft2(h1,'symmetric');
or else just do
h2 = real(ifft2(h1));

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!