Thursday 23 June 2016

image processing - 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
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

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


Answer



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.


cam1


Then we show the Fourier transform of the image.



cam1_fourier


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.


filter_mask


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.


filtered_cam1_fourier


To get the filtered image we take the inverse transform of the filtered result.


filtered_cam1


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).



cam1_fourier_real


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.


filter_mask_real


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,


D


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.


No comments:

Post a Comment

readings - Appending 内 to a company name is read ない or うち?

For example, if I say マイクロソフト内のパートナーシップは強いです, is the 内 here read as うち or ない? Answer 「内」 in the form: 「Proper Noun + 内」 is always read 「ない...