Spectral whitening¶
| Fact | Value |
|---|---|
| Also called | Whitening filter, Spectrum flattening |
| Status | Implemented and tested |
| Family | Frequency-domain filters |
| Engine | Browser |
| Type | Nonlinear, frequency, spectrum equalization |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | web/src/core/filters/frequency/shaping.ts, spectralWhiteningFilter |
| Operator code | web/src/core/filters/frequency/shaping.ts, whiteningValues |
| Channels | Brightness only, RGB |
Overview¶
Spectral whitening evens out the spectrum of an image. In natural images the strength of the spectrum falls with frequency, so coarse detail is much stronger than fine detail. Whitening scales each ring of the spectrum to the same mean strength. A strength setting blends from no change to a flat spectrum.
How it works¶
- Transform the image with the FFT.
- Average the magnitude over each ring of frequencies at the same distance from the center.
- Scale each frequency by the mean magnitude of the spectrum over its ring mean, raised to the strength.
- Transform back.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(F\): The spectrum of the extended image
- \(A(r)\): The mean magnitude on ring r
- \(M\): The mean magnitude of the whole spectrum
- \(\alpha\): The strength
The magnitude of the spectrum of a natural image falls roughly as 1 over the frequency, as Field showed. A(r) is the mean magnitude on each ring of frequencies at the same distance r from the center, and M the mean magnitude of the whole spectrum. Scaling each ring by M / A makes the rings equally strong, a flat or white spectrum. Frequency 0, the mean brightness, is kept. The strength alpha blends from no change at 0 to a flat spectrum at 1. Using the ring mean as the estimate of the magnitude is this filter's own choice. A ring holds the frequencies whose distance from the center, in steps of 1 over the larger side of the FFT grid, rounds to the same whole number.
Field showed that the magnitude of the spectrum of natural images falls roughly as 1 over the frequency. A spectrum with the same strength at every frequency is called white, after white light. The ring mean A(r) estimates the magnitude at each distance r, and the factor (M / A)^alpha scales it toward the mean M. With alpha = 1 every ring has mean M. Frequency 0, the mean brightness, is kept. Using the ring mean is this filter's own choice. It ignores direction, so an image with strong stripes in one direction is whitened as a whole ring.
Kernel or operator¶
The gain depends on the image, so the Inspector lists the stages. The Frequency explorer shows the gain on the patch.
Algorithm¶
- Extend the image with the border mode by half its size, at most 256 pixels, on each side.
- Take the 2D FFT.
- Average the magnitude over each ring of frequencies.
- Multiply each frequency by M over its ring mean, raised to the strength.
- Take the inverse FFT and keep the real part.
- Crop the extension.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Strength strength |
1 | 0 to 1 | The power alpha of the correction. 0 keeps the image, 1 makes every ring of the spectrum equally strong. |
Output mapping outputMapping |
Stretch to full range | 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¶
spectralWhiteningFilter in web/src/core/filters/frequency/shaping.ts computes the gains with whiteningValues. Ring means below 1e-9 times the largest magnitude count as 0. As for the other frequency filters, the image is extended with the border mode by half its size, at most 256 pixels, on each side. The filter needs the whole spectrum, so it always runs on the whole image, and a selection is cut out of the result.
Worked example¶
| Strength | PSNR against the input (dB) |
|---|---|
| 0 | infinite |
| 0.25 | 38.56 |
| 0.5 | 32.46 |
| 0.75 | 28.74 |
| 1 | 25.75 |
The zone plate filtered with growing strength, clipped to 0 to 255, compared with the zone plate itself. Strength 0 keeps the image, so the PSNR is infinite. Higher strengths change the image more.
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
- Raises noise along with fine detail.
- The ring mean ignores direction, so a strongly oriented image is whitened as if it were not.
Artifacts
- A grainy look at strength 1.
Tests¶
The reference extends the plane with numpy.pad, filters it with numpy.fft and crops it. Before the cases are written, the script checks that strength 0 keeps the image.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/frequency/frequency.test.ts
References¶
- Field, D. J. Relations between the statistics of natural images and the response properties of cortical cells. Journal of the Optical Society of America A 4(12), 2379 to 2394, 1987. DOI 10.1364/JOSAA.4.002379.