Butterworth band-reject filter¶
| Fact | Value |
|---|---|
| Also called | Butterworth band-stop filter, Butterworth band stop filter |
| 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, butterworthBandrejectFilter |
| Operator code | web/src/core/filters/frequency/passband.ts, passResponse |
| Channels | RGB, Brightness only |
Overview¶
The Butterworth band-reject filter removes the frequencies in a ring around the center of the spectrum and keeps the rest. It suppresses noise or a pattern of one size. The Butterworth filter changes smoothly from keeping to removing. Its order sets how sharp the change is: order 1 does not ring, and high orders ring almost like the ideal filter.
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 in a ring around the center of the spectrum and 1 elsewhere.
- 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 center of the band
- \(W\): The width of the band
- \(n\): The order
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. At D = D_0 the response is 0. 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 changes smoothly. Gonzalez and Woods note that order 1 has no ringing, order 2 has very little, and higher orders ring more as the response approaches the ideal 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 |
|---|---|---|---|
Band center center |
0.2 | 0.01 to 0.5 cycles/px | The frequency D_0 in the middle of the band, in cycles per pixel. |
Band width width |
0.1 | 0.005 to 0.5 cycles/px | The width W of the band, in cycles per pixel. |
Order order |
2 | 1 to 10 | The order n of the Butterworth filter. |
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¶
butterworthBandrejectFilter 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 | 0.9997 | 0.9878 | 0.6494 | 0 | 0.3962 | 0.8853 | 0.9968 |
The response at a few distances from the center of the spectrum, for D_0 = 0.2 and W = 0.1 and order 2. With order 2 the Butterworth filter changes smoothly, and the band-reject response is 0 at D_0. 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
- High orders ring almost as much as the ideal filter.
- A ring removes all directions. Use notch filters for patterns in one direction.
Artifacts
- Ringing next to strong edges at high orders.
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.