Please help me understand the following MATLAB code for Ideal Low pass filter. I am unable to understand the Part2 in the below code. Please explain me why we are doing like this.
I have read the Rafael C. Gonzalez's Digital Image Processing Using Matlab 2E which explains my question but I couldn't understand properly. It states that DFTUV Computes meshgrid frequency matrices. I don't understand this "meshgrid frequency matrices".It will be very much helpful if someone could explain me clearly.
%IDEAL LOW-PASS FILTER
%Part 1
function idealfilter(X,P) % X is the input image and P is the cut-off freq
f=imread(X); % reading an image X
[M,N]=size(f); % Saving the the rows of X in M and columns in N
F=fft2(double(f)); % Taking Fourier transform to the input image
%Part 2 % I don't understand this part
u=0:(M-1);
v=0:(N-1);
idx=find(u>M/2);
u(idx)=u(idx)-M;
idy=find(v>N/2);
v(idy)=v(idy)-N;
[V,U]=meshgrid(v,u);
D=sqrt(U.^2+V.^2);
%Part 3
H=double(D<=P); % Comparing with the cut-off frequency
G=H.*F; % Multiplying the Fourier transformed image with H
g=real(ifft2(double(G))); % Inverse Fourier transform
imshow(f),figure,imshow(g,[ ]); % Displaying input and output image
end
I tried to run each commands in Part2 individually for M= 8 and N=8. I get
u=0:(M-1); ==> u = 0 1 2 3 4 5 6 7
v=0:(N-1); ==> v = 0 1 2 3 4 5 6 7
idx=find(u>M/2); ==> idx = 6 7 8
u(idx)=u(idx)-M; ==> 0 1 2 3 4 -3 -2 -1
idy=find(v>N/2); ==> idy = 6 7 8
v(idy)=v(idy)-N; ==> 0 1 2 3 4 -3 -2 -1
[V,U]=meshgrid(v,u); ==>
V=
0 1 2 3 4 -3 -2 -1
0 1 2 3 4 -3 -2 -1
0 1 2 3 4 -3 -2 -1
0 1 2 3 4 -3 -2 -1
0 1 2 3 4 -3 -2 -1
0 1 2 3 4 -3 -2 -1
0 1 2 3 4 -3 -2 -1
0 1 2 3 4 -3 -2 -1
U =
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4
-3 -3 -3 -3 -3 -3 -3 -3
-2 -2 -2 -2 -2 -2 -2 -2
-1 -1 -1 -1 -1 -1 -1 -1
I am unsure why they are doing like this. How it affects the image.Please help me understand. The fftshift
is used to avoid the wraparound error. Is the above operation anyway related to fftshift
command? My understanding is that they are doing this to avoid using the fftshift
command. Thanks in advance. Help me learn.
Reference: Rafael C. Gonzalez's Digital Image Processing Using Matlab 2E