Ideal low-pass filter¶
| Fact | Value |
|---|---|
| Also called | Ideal low pass filter, Ideal LPF |
| Status | Implemented and tested |
| Family | Frequency-domain filters |
| Engine | Browser |
| Type | Linear, frequency, frequency response |
| Output | Image |
| Operator shown as | frequency-response (exact) |
| Support radius | global |
| Code | web/src/core/filters/frequency/passband.ts, idealLowpassFilter |
| Operator code | web/src/core/filters/frequency/passband.ts, passResponse |
| Channels | RGB, Brightness only |
Overview¶
The ideal low-pass filter keeps the low frequencies of the image, the slow changes in brightness, and removes the high ones. The result is a blurred image. The ideal filter keeps or removes each frequency completely, with a sharp cutoff. The sharp cutoff makes ripples, called ringing, next to strong edges.
How it works¶
- Extend the image with the border mode.
- Split it into waves of many frequencies with the 2D FFT.
- Multiply each frequency by H, which is 1 near the center of the spectrum and falls to 0 beyond the cutoff.
- Add the waves back together with the inverse FFT and crop the extension.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(D\): The distance from the center of the spectrum in cycles per pixel
- \(f_x, f_y\): The frequency along x and y in cycles per pixel
- \(D_0\): The cutoff frequency
D is the distance of a frequency from the center of the spectrum, in cycles per pixel, so the filter does the same thing on any image size. Gonzalez and Woods measure D in samples of the padded spectrum. Divide their value by the padded size to get the one here. Ringing near edges comes from the impulse response of H, which is wider the sharper the cutoff.
The filter works on the spectrum of the image. D is the distance of a frequency from the center of the spectrum, in cycles per pixel, so the same settings do the same thing on any image size. A frequency of 0.5 cycles per pixel, a stripe pattern with a period of 2 pixels, is the finest detail a pixel grid can hold. Gonzalez and Woods measure D in samples of the padded spectrum instead. The response jumps from 1 to 0, so its impulse response, the inverse transform of H, is a wide function with side lobes like a sinc. Those side lobes are the ringing next to edges.
Kernel or operator¶
The operator is the response H over the spectrum. The Inspector draws it as a gray image, with frequency 0 in the center, and as a profile along the f_x axis. The Frequency explorer in the Learn panel shows the spectrum of the image, H, their product and the result.
Algorithm¶
- Extend the image with the border mode by half its size, at most 256 pixels, on each side.
- Take the 2D FFT.
- Multiply each frequency by H.
- Take the inverse FFT and keep the real part.
- Crop the extension.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Cutoff cutoff |
0.1 | 0.005 to 0.5 cycles/px | The cutoff frequency D_0, in cycles per pixel. 0.5 is the finest detail a pixel grid can hold. |
Border mode border |
Reflect | Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap | How pixels outside the image are filled when the window reaches past the edge. |
Channels channelScope |
RGB | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
idealLowpassFilter in web/src/core/filters/frequency/passband.ts builds H with passResponse. frequencyFilter in web/src/core/filters/frequency/spectral.ts does the rest. The image is extended with the border mode by half its size, at most 256 pixels, on each side. It gets a little more to reach a length whose only prime factors are 2, 3 and 5. The FFT handles such lengths fast. This is the padding step of Gonzalez and Woods, with the border mode in place of zeros. Without it, the circular wrap of the FFT would mix opposite edges of the image. The filter needs the whole spectrum, so it always runs on the whole image, and a selection is cut out of the result. The result is clipped to 0 to 255.
Worked example¶
| D (cycles per pixel) | 0 | 0.05 | 0.1 | 0.15 | 0.2 | 0.25 | 0.3 | 0.5 |
|---|---|---|---|---|---|---|---|---|
| H | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 |
The response at a few distances from the center of the spectrum, for D_0 = 0.1. The ideal filter switches between 1 and 0 at once. In the zone plate of the figure, distance r from its center has frequency r / 160, so D = 0.1 lies 16 pixels from the center.
Complexity¶
- Time: \(O(N log N)\)
- Memory: \(O(N)\)
N is the number of pixels of the extended image, about 4 times the image for images up to 512 pixels on a side.
Artifacts and limitations¶
Limitations
- The sharp cutoff makes strong ringing next to edges.
- Blurs edges as much as noise.
Artifacts
- Ringing, ripples that run parallel to strong edges.
Tests¶
The reference extends the plane with numpy.pad, filters it with numpy.fft and crops it, with the formulas of Gonzalez and Woods. Before the cases are written, the script checks the Gaussian low-pass filter against scipy.ndimage.gaussian_filter on a plane whose extension is an exact mirror. It also checks that each low-pass and high-pass pair, and each band-reject and band-pass pair, adds up to the image. The unit tests check the values at the cutoff and that a flat image passes a low-pass filter unchanged.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/frequency/frequency.test.ts
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 4, filtering in the frequency domain.