# Border effects / artifacts for spectral filtering of images

I am working on a spectral filtering problem as follows:

I'm given an image with a periodic distortion pattern (see below), and I want to remove the distortion pattern while preserving the main content of the image. I can achieve this by spectral filtering. I compute the Fourier transform of the image, apply a filter mask (here: circular patches organized on a bigger circle, with Gaussian-like transition) to the Fourier transform, and compute the inverse Fourier transform to obtain the filtered image.

Update: I provide the entire example implemented using Python/OpenCV in this Gist.

This works pretty well in my case, however, I don't understand the border artifacts that appear in the filtered image (see below). The problem occurs for different filters (step, linear, Gaussian). The effect persists, if I replicate the image along its borders (I used cv.copyMakeBorder().

Questions: Where do these artifacts come from? I have some guesses (spectral leakage, periodicity assumption of the DFT), but I cannot explain it in detail. How to avoid these artifacts? Using a Hanning window (image = win*image) on the input image? While this will silence the artifacts, this will introduce vignetting to the image...

Update: Using the inputs from this answer, I was able to improve the result as seen below. The artifacts are still present, but less visible. Concretely, I added a border of 1 pixel, which improved the result as shown. Why a border of 1 pixel? My input image has size 478x478 pixel. cv.getOptimalDFTSize(478) yields 480.

If I replicate the image at the borders for larger border widths, the result does not improve any further. The reason for this is that the periodic pattern is not extended cleanly into the mirrored areas, creating seams that will have their own traces in the frequency domain.

I could completely eliminate the border artifacts if I extended the original, undistorted image by mirroring it and only then applied the periodic pattern. After cropping the reconstructed image to its original size, the border effects will have disappeared.

Summary: The described border effects are naturally present due to the periodicity restriction imposed by the DFT. However, they were additionally amplified by the unfavorable dimensions of my input image: Keep in mind the zero-padding in the FFT!

• Instead of image_rec = np.abs(scipy.fft.ifft2(ft_new)), take the real component of the inverse transform. If the filtering is correct, then the imaginary part should be essentially zero (not exactly zero because of rounding errors). If you take the absolute values, then you're flipping negative values in the result, which could cause strange artifacts. Commented Jun 5 at 15:47
• @CrisLuengo Thank you for the hint. In this case, there is no visible difference. Commented Jun 5 at 20:08

A convolution through the FFT imposes a periodic boundary condition. Imagine filling the space by tiling the image, and then applying the filter. Near the top image edge, where one of your arrows point, the filter will see that image patch as well as a bit of the image near the bottom edge. Because the top is black and the bottom is lighter, you will have a strong intensity jump there. The filter will respond to this jump. You’ll get some of the brightness at the bottom of the image show up at the top.

To avoid this, extend the image around all edges before applying the FFT. Fill in the new pixels by e.g. mirroring the image at the edge. After filtering, crop the image to its original extent. The width of the border should be at least equal to the width of the artifacts you see in your current result. You can choose the size of the border so that the resulting intermediate image has a nice size for the FFT (which handles sizes that are a product of small integers the best). OpenCV has getOptimalDFTSize() to help you find a good image size for applying the FFT.

It is indeed difficult to properly continue the strong, regular, artificial pattern added to the image. With common boundary extensions like mirroring you thus still will see an edge effect. However, the boundary extension should remove the edge effect caused by the strong transition in intensity. The overall edge effect should thus be less strong.

I don't think there's a simple way to avoid edge effects in this case, other than simply cropping them way. The complex way, of course, is to get image generator to hallucinate new data where the artifacts are.

• Thanks to your comments, I was able to resolve my problem. Thank you! See my updated post above. Commented Jun 5 at 10:16

There are vertical and horizontal lines in your amplitude spectrum that you have not removed. Those may be causing your edge effects. You have not shown us your code, also. Perhaps there is an issue.

• I've provided the entire example. See the updated question. Commented Jun 5 at 9:09