# FFT of image data: “mirroring” to avoid boundary effects

I load and display an image of some rice in Matlab:

g = imread('rice.png');
imshow(g); I take the FFT of this image and shift it:

G = fft2(g);
imshow(log(abs(fftshift(G)) + 1), []); If I place a x and y axis trough the center of the image; I find that the image is symmetric g(-x,-y)=g(x,y). For a 1D signal we have that the FFT of a real signal has a symmetrical real part and an asymmetrical imaginary part. I guess that is what we see here in 2 dimensions?

Since the original image is darker at the bottom than at the top, there is a strong horizontal discontinuity at the periodic boundary causing the vertical line in the FFT.

I want to get rid of this boundary effect. A common approach to this seems to be windowing.

However I want to solve this problem by a technique I found in a paper called "mirroring". The paper was not very specific so I need your help in figuring out this approach :-).

First I create a symmetric "tile" from the original image:

tile=[flipdim(g,2) g; flipdim(flipdim(g,1),2) flipdim(g,1)];
imshow(tile); Now I take the FFT of this "tile":

Tile=fft2(tile);
imshow(log(abs(fftshift(Tile)) + 1), []) The vertical line seems to be (almost) gone: good. However the mirroring seem to have introduced more symmetry.

What is the correct result: the FFT of the original image or the FFT of the "mirrored" image?

Is there a way I can "mirror" so that I both get rid of boundary effects and get a purely real FFT?

The FFT of the original image is correct. The artifacts you are seeing are typical for the DFT, because the DFT basis functions have difficulty representing non-periodic signals. Even though the DFT is of finite length, it actually represents a periodic signal that extends to negative and positive infinity. The DFT's basis functions are all sinusoids with periods that are integer divisors of the DFT size, so they all start and end at the same value, and it is hard for it to fit signals that are not periodic in this way. So, imagine tiling your image: there is indeed an abrupt discontinuity at the edges. The edges do not match up well, and that is the reason for the boundary effects.

In audio signal processing, where the FFT is used often, a very common approach is to use a window function, as you noted. This tapers the edges off towards 0, and reduces this effect.

In image processing, the DCT is usually used rather than the DFT, because it imposes different symmetry constraints that give better results. In other words, it's used to prevent exactly the issue you're seeing. In contrast to the DFT's basis functions (remember how they all start and end at the same value), the DCT's basis functions are integer divisors or half of integer divisors, so many of them start and end at different values. As a result, the implied boundary conditions are different: the signal is assumed to be symmetric about its edges, which is actually somewhat similar to the "mirroring" you were experimenting with. Here is another post by me with a little more information on the DCT: https://dsp.stackexchange.com/a/362/392

So, as a short summary, if you are absolutely set on using the FFT, you might want to try windowing. However, the best choice is probably to use the DCT, which implies boundary conditions similar to your "mirroring" idea, and as a result, handles images better.

• From what I could understand DFT implies periodic extension whereas DCT implies even extension. When doing image compression one is working on small blocks of the image. Since the periodic extension implied by DFT causes jumps at the border which in turn decreases the convergence rate; it is better to use DCT for image compression (faster convergence-> smaller files with same amount of visible information). However DCT seems to be mentioned mostly in this context. The context I am working in is that I want to filter an image by a Gabor filter. Can this be done using DCT? – Andy Jan 10 '12 at 14:21
• If you just want to filter the image with a Gabor filter, you should just use the FFT. The boundary conditions won't be an issue. Why are you worried about the boundary conditions, or having a purely real spectrum, for filtering with a Gabor filter? – schnarf Jan 10 '12 at 14:30
• Regarding having a purely real spectrum: I need to estimate the dominant frequencies: fu = Sum(u*G)/Sum(G) and fv, where G(u,v) is the FFT of my image g(x,y). I do not understand how this works if G is complex. I would get a complex fu? – Andy Jan 10 '12 at 16:33
• Rather than considering the complex values, you should consider their magnitudes. I.e. for each complex frequency bin z = a + bi, its magnitude is sqrt(a^2 + b^2). – schnarf Jan 10 '12 at 18:24
• Another thing to think about: It might seem confusing that you're taking real data and getting a complex spectrum. The complex spectrum is just a way of giving each sinusoid a particular phase. – schnarf Jan 10 '12 at 18:26