# Why we need distances in Ideal LPF?

Here I have a MATLAB code for Ideal Low pass filtering an Image. In the below code they have used D=sqrt(U.^2+V.^2); which finds some distances. Then this distance matrix D is compared with the cut-off frequency to create a filter function H ( H=double(D<=P); ) that has be multiplied with the fourier transformed image F. i.e, G=H.*F; to get the output image.

My questions: What is this distance ? Why we need this distance matrix D ? What information this gives to us? How it helps in Low Pass Filter an Image? (I understood that it helps to create the filter function H) but Why we need the distances to create the filter function? I need to it understand clearly. This may be a simple question but could anyone please explain me this with little patience.

Thank you for reading my question.

Reference: MATHWORKS:IDEAL LOW PASS FILTER

%IDEAL LOW-PASS FILTER

    function idealfilter(X,P) % X is the input image and P is the cut-off freq
[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

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);   % finding out the distance

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


In the Fourier domain, the frequency increases with the distance from origo. So an ideal low-pass filter in the Fourier domain is shaped like a disk centered at the origo.

Here they work with only one quadrant of the Fourier domain. Therefore, they create a pie-shaped mask H for the quadrant by computing the Euclidean distance from the quadrant corner and store the distance values in D. The mask H is then chosen as all elements in D with values less than the parameter P (which is the radius of the disk).

So the answer to your question(s) is that the distance computation helps the method compute the filter shape. The key is that a constant Euclidean distance from a point defines the shape of a circle, and all elements inside that circle form the disk that is used as filter mask.

So, naturally, after filtering, only the low-frequency components of the image are left in the Fourier domain. Then the low-pass filtered image is retrieved by inverse transforming the filtered image back from the Fourier domain to the spatial domain.

EDIT

Here is an illustrated example to clarify the answer.

A typical way of illustrating filtering in the Fourier domain is to show the input image. Then we show the Fourier transform of the image. Here the origo of the Fourier space is in the center. Since the frequencies increase with the distance from origo, the ideal filter is a disk with its center at the origo. Here we see the disk. The disk radius is the input parameter P. We use the disk to filter the image by multiplying the filter with the transformed image. We then get the filtered transform. To get the filtered image we take the inverse transform of the filtered result. So far so good. As I said, this is usually how low-pass filtering in the Fourier domain is explained.

Now what I was trying to explain when talking about quadrants of the Fourier domain, which got a bit confusing, is that the result from the Fourier transform in the MATLAB code provided does not have origo centered as in the second image in the example above (the image showing the unfiltered Fourier transform).

Instead the result from the Fourier transform F=fft2(double(f)) looks like this (we refer to it as the transform image). This means that since the filter is a disk with its center in origo, the disk must be placed in the corner of the transform image. Since the Fourier transform is cyclic, the filter should wrap around to cover all four corners of the transform image. Here is where the distance matrix D comes in. It is used to compute the filter mask above. D is computed by taking the Euclidean distance from each corner (using a neat trick with meshgrid). So Dis the Euclidean distance map looking like this, where blue values represent low distances and red values represent high.

% Top left corner of D
0.00000   1.00000   2.00000   3.00000   4.00000   5.00000
1.00000   1.41421   2.23607   3.16228   4.12311   5.09902
2.00000   2.23607   2.82843   3.60555   4.47214   5.38516
3.00000   3.16228   3.60555   4.24264   5.00000   5.83095
4.00000   4.12311   4.47214   5.00000   5.65685   6.40312
5.00000   5.09902   5.38516   5.83095   6.40312   7.07107


The filter mask can therefore simply be acquired by selecting the elements in D that have a distance value less than or equal to P.

The rest of the code works as in the example described above. That is, the filter mask is multiplied by the transform image, and the result is inverse transformed to get the low-pass filtered image.

I hope this clarifies what role the distance computation has for creating the filter.

• Thank you very much. What do you mean by "Here they work with only one quadrant of the Fourier domain"?? – Premnath D Nov 12 '13 at 15:37
• The Fourier transform is usually presented as a 2D, symmetric, result with origo in the center. From the code I could only tell that the mask was produced with a center in the top left corner which made me presume that it only operated on one quadrant. But now I see that the filter is translated so it will cover all four quadrants. That is, the matrix H will not have disk in the center, but on pie-shape in each corner that together make up a disk, if I'm not reading the code completely wrong. Can someone please correct me if I'm wrong? – mags Nov 12 '13 at 16:29
• I thought that I understood the concept but again confusing.. – Premnath D Nov 12 '13 at 17:02
• @PremnathD I edited the answer to further clarify the role of the distance calculation. I hope this clears things up for you. – mags Nov 12 '13 at 23:33
• Thank you very much... Great explanation.. This kind of explanation is needed for a starter like me to understand the in and out of any concepts. Thank you again for spending your time to explain the concept with this much details. – Premnath D Nov 13 '13 at 6:01