Homomorphic filter¶
| Fact | Value |
|---|---|
| Also called | Homomorphic filtering, Illumination correction |
| Status | Implemented and tested |
| Family | Frequency-domain filters (also Enhancement, illumination and color) |
| Engine | Browser |
| Type | Nonlinear, frequency, frequency response on the log image |
| Output | Image |
| Operator shown as | frequency-response (exact) |
| Support radius | global |
| Code | web/src/core/filters/frequency/shaping.ts, homomorphicFilter |
| Operator code | web/src/core/filters/frequency/shaping.ts, homomorphicResponse |
| Channels | Brightness only, RGB |
Overview¶
The homomorphic filter evens out uneven lighting and raises local contrast. It treats the image as lighting times reflectance. A logarithm turns the product into a sum, so a frequency filter can turn the slow lighting down and the detail up. The filter is listed under enhancement too.
How it works¶
- Take ln(1 + f) of every value.
- Transform it with the FFT and multiply by H, which is gamma_L near the center and gamma_H far from it.
- Transform back and take exp of the result minus 1.
- Stretch the result to the full range, the default output mapping.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(f, g\): The input and output values
- \(\gamma_L, \gamma_H\): The low and high frequency gains
- \(c\): The sharpness
- \(D, D_0\): The distance from the center of the spectrum and the cutoff in cycles per pixel
An image is lighting times reflectance. The logarithm turns the product into a sum, so a linear filter can treat the two parts apart. Lighting changes slowly and sits at low frequencies, where H is gamma_L. Reflectance holds the detail at high frequencies, where H is gamma_H. With gamma_L below 1 and gamma_H above 1, the filter flattens the lighting and raises the detail. The response and the steps follow Gonzalez and Woods. The filter uses ln(1 + f) so that black pixels have a logarithm, and returns exp(s) - 1. D is the distance from the center of the spectrum in cycles per pixel.
With f = i r, lighting i times reflectance r, the logarithm gives ln f = ln i + ln r. Lighting changes slowly, so ln i sits at low frequencies, and ln r holds the edges and texture at high frequencies. A high-emphasis response with gamma_L below 1 and gamma_H above 1 turns ln i down and ln r up. The exponential then turns the sum back into a product. The response and the steps follow Gonzalez and Woods, with D in cycles per pixel. The filter uses ln(1 + f), so black pixels have a logarithm.
Kernel or operator¶
The operator is the response H, which the Inspector draws over the spectrum. It acts on the spectrum of the log image.
Algorithm¶
- Take ln(1 + f) of every value.
- Extend the result with the border mode by half its size, at most 256 pixels, on each side.
- Take the 2D FFT and multiply each frequency by H.
- Take the inverse FFT and keep the real part.
- Crop the extension.
- Return exp of the result minus 1, stretched to the full range by default.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Low gain gammaLow |
0.5 | 0 to 1 | The gain gamma_L for the lowest frequencies, which carry the lighting. |
High gain gammaHigh |
2 | 1 to 4 | The gain gamma_H for the high frequencies, which carry the reflectance and detail. |
Sharpness sharpness |
1 | 0.1 to 5 | The constant c that sets how fast the gain rises from gamma_L to gamma_H. |
Cutoff cutoff |
0.05 | 0.005 to 0.5 cycles/px | The frequency D_0 around which the gain rises, in cycles per pixel. |
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¶
homomorphicFilter in web/src/core/filters/frequency/shaping.ts takes the logarithm, filters with homomorphicResponse and takes the exponential. 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¶
| D (cycles per pixel) | 0 | 0.015 | 0.03 | 0.06 | 0.12 | 0.5 |
|---|---|---|---|---|---|---|
| H | 0.4000 | 0.6654 | 1.1585 | 1.5780 | 1.6000 | 1.6000 |
The response for gamma_L = 0.4, gamma_H = 1.6, c = 1 and D_0 = 0.03, as in the figure. At frequency 0 it is gamma_L, so the mean of the log image is scaled by 0.4. Far from the center it reaches gamma_H. At D = D_0 it is gamma_L + (gamma_H - gamma_L)(1 - e^-1).
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 result has a new range of values, so it needs the output mapping.
- Lighting with sharp edges, such as hard shadows, is not slow and is not removed.
Artifacts
- Halos along strong edges between bright and dark areas.
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 equal gains of 1 keep the image.
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.
- Oppenheim, A. V., Schafer, R. W. and Stockham, T. G. Nonlinear filtering of multiplied and convolved signals. Proceedings of the IEEE 56(8), 1264 to 1291, 1968. DOI 10.1109/PROC.1968.6570.
- Stockham, T. G. Image processing in the context of a visual model. Proceedings of the IEEE 60(7), 828 to 842, 1972. DOI 10.1109/PROC.1972.8782.