Gaussian high-pass filter¶
| Fact | Value |
|---|---|
| Also called | Gaussian high pass filter, Gaussian HPF |
| 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, gaussianHighpassFilter |
| Operator code | web/src/core/filters/frequency/passband.ts, passResponse |
| Channels | Brightness only, RGB |
Overview¶
The Gaussian high-pass filter removes the low frequencies of the image and keeps the high ones. Flat areas become 0, and edges and fine detail remain. The Gaussian filter changes smoothly along a Gaussian curve, so it gives no ringing or very little.
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 0 near the center of the spectrum and rises to 1 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. The high-pass filter is 1 minus the Gaussian low-pass filter. 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 high-pass response is 1 minus the Gaussian low-pass response, so the two results add up to the image. The inverse transform of a Gaussian is a Gaussian, so the low-pass filter equals a spatial Gaussian blur with sigma = 1 / (2 pi D_0) pixels. The tests check this against scipy.ndimage.gaussian_filter.
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. |
Output mapping outputMapping |
Offset by 128 | Absolute value, Offset by 128, Stretch to full range, Clip to 0 to 255 | How signed results, such as gradients, become 8-bit pixel values. |
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 |
Brightness only | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
gaussianHighpassFilter 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 signed, so the output mapping turns it into pixel values. The default offsets 0 to gray 128.
Worked example¶
| D (cycles per pixel) | 0 | 0.05 | 0.1 | 0.15 | 0.2 | 0.25 | 0.3 | 0.5 |
|---|---|---|---|---|---|---|---|---|
| H | 0 | 0.1175 | 0.3935 | 0.6753 | 0.8647 | 0.9561 | 0.9889 | 1 |
The response at a few distances from the center of the spectrum, for D_0 = 0.1. The Gaussian filter changes smoothly and never jumps. The high-pass response is 1 minus the low-pass response. 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 change from pass to stop is gradual, so frequencies near the cutoff are only partly kept.
- Amplifies noise, which lives at high frequencies.
Artifacts
- None from the cutoff. Very low cutoffs can show the border extension near the image edge.
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.