2
$\begingroup$

I have written a function that implements a gaussian filter. I use fft2 to transform my image and my filter to 2d fourier transform. Then I multiply them and then use ifft2. The resulting image is called (I).

I use fspecial in order to make a gaussian filter and use imfilter to get what resulted in (I). However, They are different. I checked my gaussian filter in both cases. They are the same. But in (I) the border of image is not smooth and dark and in the second case (which seems to be the correct case) the borders are smooth and darker than before.

I want to know what I am missing that results in an incorrect result? Thanks.

$\endgroup$
1

1 Answer 1

3
$\begingroup$

You have to remember that the FFT operation treats your data as if it's a periodic function. This means that, if you think of your image being defined over the range $x=0..(X-1)$ and $y=0..(Y-1)$ (so the image size is $X\times Y$) then the image should be though of as though it's just one rectangular $X\times Y$ sample taken from an infinite array of images that repeat every $X$ samples in the $x$ direction, and every $Y$ samples in the $y$ direction.

Your filtering operation will 'spread' each pixel form the input image over a larger areas in the output image, and this spreading operation will also span across these periodic boundaries. So, the pixel at $(7,X-1)$ will spread in all directions, and in particular, the 'spreading' to the right will affect pixel $(7,X)$, which is really $(7,0)$ (because the image 'repeats' every $X$ pixels).

If you want to use the FFT2 function to do your filtering, and you want to avoid this periodic 'wrap-around' effect, you need to enlarge your image first. If your Gaussian filter kernel is of size $2*N+1$ (it's usually odd), then you need to add a border of $N$ pixels on the left, right, top and bottom of the image. You also have to choose what pixel values to put in these border pixels. A good choice is to replicate the left-most pixel of each row in the image across the entire row in the left border (and do a similar replication operation into the other borders).

I hope this answers your question

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.