# Low-pass filter parameters for image downsampling

I need to downscale an image in a factor of sx horizontally and sy vertically (sx, sy < 1).

I want to use a finite n x m low-pass filter before downsampling.

How should I determine the low-pass filter parameters (n, m and the Gaussian sigma) to get it as a function of sx and sy?

Specifically, I'm interesting in the case where hx=hy=1/sqrt(2).

-
question related to [the one asked here][1] [1]: stackoverflow.com/questions/3149279/… – isrish Nov 18 '12 at 13:27
Thanks, but this question is not about how to determine the filter parameters as a function of the downscaling factor. – Ben-Uri Nov 18 '12 at 13:36
How much do you want to filter? What's your goal? – Franco Callari Nov 19 '12 at 16:34
I want to use a low pass filter before down sampling to avoid aliasing. I want to preserve as much as possible information without aliasing. – Ben-Uri Nov 19 '12 at 20:21
You need to look at the Fourier transform of the filter to know how well it will cut the aliasing frequencies while keeping the frequencies below the Nyquist limit of the sampled result. A gaussian's transform is another gaussian, meaning there's no sharp cutoff. There's almost always a better choice. – Mark Ransom Apr 10 at 23:05

## migrated from stackoverflow.comDec 20 '12 at 17:53

You have to think about the change in Nyquist frequency between both images. If the Nyquist frequency of the original image is N, the downsampled image will have a lower Nyquist frequency, xN, where x is related to the ratio of sizes between the final image and the initial one. You would need to remove those spatial frequencies which are higher than xN in the original image before downsampling it.

The power spectrum of a Gaussian in the image space, is also a Gaussian in the frequency space. If we ignore for a moment the second dimension, the Gaussian in the image space is defined as exp(-x^2/s^2), where x represents your pixels. This is mapped to the frequency space as exp(-w^2*s^2), where w is the frequency. The sigma parameter (s) shows that a broad Gaussian in the image space, corresponds to a narrow Gaussian in the frequency space.

You would like to choose a sigma parameter that yields a very low value in frequency space at the frequency that corresponds to the Nyquist frequency of the down sampled image.

-
 Right, but how do I translate this to a discrete convolution kernel? (this was the question) – Ben-Uri Apr 14 at 9:38 see the edit pls... – NoNameNo Apr 14 at 15:14 Thanks, but still is there a formula to find the sigma as a function of the maximum frequency that should be in the output result? – Ben-Uri Apr 14 at 19:19 i don't know any formulas. – NoNameNo Apr 14 at 19:50

If you were doing a large downsize (say 2x, 3x, 4x) you can do pixel averaging to achieve good anti-aliasing. That's actually why anti-aliasing uses a lot of additional CPU/GPU in order to make video games look crisper.

Since you're going from a 1000x1000 to a 707x707 image (just an example for the scale factor) you're correct that aliasing might be an issue.

Thankfully this is a problem that many people have run into already and done quite a bit of work to solve. In many cases a bicubic interpolation is the way to go. There are some examples of what the different interpolation methods look like here:

http://www.compuphase.com/graphic/scale.htm

OpenCV's resize has several of those methods built-in:

http://opencv.willowgarage.com/documentation/cpp/geometric_image_transformations.html#cv-resize

If you have already played around with several of those interpolation methods and they don't work well, please post some kind of a sample source image and sample result image showing the shortfall. We'll need this in order to diagnose the problem and try and come up with a good solution to it.

-
 remark that, while it is fast, pixel averaging is not the ideal solution for quality. pixel averaging effectively applies a rect filter. in the frequency space, this is equivalent to multiplying by a sinc function that oscillates with zero crossings at the harmonics of Nyquist. this has two problems: 1. it attenuates high frequency but not that much 2. it inverts phase at every other side lobe. Both (1) and (2) can contribute to aliasing. – thang Jan 20 at 8:19

I don't really have a good answer for you, but here are 2 options that you could try:

• in Computer Vision , this rescaling factor is usually handled by applying a Gaussian filter of width (in pixels) between 5 and 9. You can find the corresponding $\sigma$ of the Gaussian because the pixel width is classically equal to $3 \sigma$.
• if you are willing to do fine signal-sampling computations, then why not using Fourier transform ? Take the FFT of your image, keep only the subpart that corresponds to your target size, and invert the transform. This will apply an anti-aliasing filter on your spectrum. If there are too many artifacts (ripples, ringing) for you, then apply on your spectrum of Gaussian filter whose width corresponds to your target size .
-