Gaussian Low Pass Filter

74 次查看(过去 30 天)
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.

采纳的回答

Image Analyst
Image Analyst 2012-6-17
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 个评论
Steve
Steve 2012-6-17
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 2012-6-17
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.

请先登录,再进行评论。

更多回答(1 个)

Habtamu Fanta
Habtamu Fanta 2021-11-1
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 个评论
Image Analyst
Image Analyst 2021-11-1
@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.

请先登录,再进行评论。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by