Clear Filters
Clear Filters

Gaussian Low Pass Filter

74 views (last 30 days)
Steve
Steve on 16 Jun 2012
Commented: Image Analyst on 1 Nov 2021
Hello Dear Experts,
I need to build a function performing the low pass filter: Given a gray scale image (type double) I should perform the Gaussian low pass filter. The filter size is given by a ratio parameter r. The values of the r parameter are between 0 and 1 - 1 means we keep all the frequencies and 0 means no frequency is passed. The DC should always stay.
Here is what I did:
function [nImg,mask] = lowPassFilter(img,r)
F = fftshift(fft2(img));
mask = fspecial('gaussian',[3 3], r);
M = fft2(mask, size(F,1), size(F,2));
Filtered = M.*F;
nImg = real(ifft2(ifftshift(Filtered)));
end
P.S Please tell me what I did wrong, I have been advised by Anton Semechko: "The are two fundamental ways you can perform linear filtering of an image. One approach is to use convolution in the spatial domain. The second approach is to find the product of the filter's and image's Fourier transforms in the frequency domain and then take the inverse. Now what you are attempting to do is more like the send approach. But I see two major problems with your code. First, you don't center the image on the DC component (see fftshift) after taking the FT. Secondly, you don't zero pad the filter to match the dimensions of the image."
Please tell me if I did it right or where are my mistakes. Maybe you have an example image to test on and compare to real right result that should be.
Will appreciate if your answer will be informative and straight to the point.

Accepted Answer

Image Analyst
Image Analyst on 17 Jun 2012
A 3 by 3 filter is no where near large enough to filter out all frequencies. You just don't have the resolution. Think about it: Many Gaussians that can fit inside a 3x3 box will have their "tails" clipped off by the edges of the box. And super narrow Gaussians are so quantized with only three sample points that you can't get the frequency resolution that you need. And of course there is still the problem that Anton mentioned that you didn't fix, which is you performed fftshift() on the spectrum of img but not on the spectrum of mask.
  2 Comments
Steve
Steve on 17 Jun 2012
Tell me if I am right - I should perform J = fftshift(fft2(mask)) and
then to multiple it to the F above?
And my filter should be as the matrix size?
Thanks a lot in advance.
Image Analyst
Image Analyst on 17 Jun 2012
The fftshift()'s are not necessary at all for the multiplication. They are only needed for display, which you aren't even doing at all in your function. The arrays will multiply just fine with the origin at the corners instead of the center.

Sign in to comment.

More Answers (1)

Habtamu Fanta
Habtamu Fanta on 1 Nov 2021
function [nImg,mask] = lowPassFilter(img,r)
F = fftshift(fft2(img));
\mask = fspecial('gaussian',[3 3], r);
M = fft2(mask, size(F,1), size(F,2));
Filtered = M.*F;
nImg = real(ifft2(ifftshift(Filtered)));
end.
  1 Comment
Image Analyst
Image Analyst on 1 Nov 2021
@Habtamu Fanta, I'm not sure how this is an answer. You just introduced a couple of errors (bad characters) into the original poster's code - that's all. It doesn't fix or address either of the two items mentioned by Steve's advisor professor.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!