White-patch correction¶
| Fact | Value |
|---|---|
| Also called | Max-RGB, White patch white balance, Perfect reflector |
| Status | Implemented and tested |
| Family | Enhancement, illumination and color |
| Engine | Browser |
| Type | Linear, color, percentile-based channel gains |
| Output | Image |
| Operator shown as | color-transform (exact) |
| Support radius | global |
| Code | web/src/core/filters/enhancement/white-balance.ts, whitePatchFilter |
| Operator code | web/src/core/filters/enhancement/white-balance.ts, whitePatchGains |
Overview¶
White-patch correction balances colors by assuming that the brightest part of each channel is white. It scales each channel so that this value reaches white. With percentile 100 it is the max-RGB method. A lower percentile ignores the very brightest pixels, such as small reflections and noise.
How it works¶
- Find the chosen percentile of each channel over the whole image.
- Take its inverse as the gain of that channel.
- Multiply every pixel by the gains, so the bright values reach white.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(P_p(C)\): The p-th percentile of channel C, as a fraction of white
- \(g_C\): The gain of channel C
P_p(C) is the p-th percentile of channel C, as a fraction of white, in linear light if it is on. It uses the linear rule of numpy.percentile on the levels 0 to 255 of the whole image. The white-patch assumption, from the retinex work of Land and McCann, is that the brightest surface in a scene is white, so its color is the color of the light. The gains bring that color to white. Percentile 100 is the max-RGB method, and lower percentiles keep a few bright or noisy pixels from setting the gains. With Linear light on, the default, the gains act on linear light values: decoded from sRGB, scaled, and encoded again. A change in the color of the light scales linear values, so this is the physical model. With it off, the gains scale the encoded values. A channel whose percentile is 0 gets gain 1.
The retinex work of Land and McCann assumes that the brightest surface in view is white, so its color is the color of the light. Dividing by it removes the light. The method breaks when a channel is already clipped at 255, since the true value there is lost. In the figure, the cast pushed red past 255, where it clipped, so the highlights stay pinkish after the correction.
Kernel or operator¶
The gains depend on the image, so the Inspector shows how they are computed and how they scale the channels.
Algorithm¶
- Find the chosen percentile of each channel over the whole image.
- Decode it to linear light, if Linear light is on, and take the gains as its inverse.
- Scale each channel by its gain, in linear light or on the encoded values.
- Encode back to sRGB and clip to 0 to 255.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Percentile percentile |
99 | 90 to 100 % | The percentile of each channel that becomes white. 100 uses the largest value, the max-RGB method. |
Linear light linearLight |
on | On or off | Scale the linear light values instead of the encoded sRGB values. |
Implementation¶
whitePatchFilter in web/src/core/filters/enhancement/white-balance.ts finds the percentiles with whitePatchGains, using the histogram percentile of the point operations. It is a global color filter, so it always sees the whole image.
Worked example¶
| Channel | Red | Green | Blue |
|---|---|---|---|
| Largest value in the warm chart | 255 | 237 | 149 |
| Gain with percentile 100, encoded values | 1 | 1.076 | 1.711 |
In the warm chart, red already reaches 255 while blue stops at 149. So red keeps gain 1 and blue gets the largest gain, which removes most of the yellow cast.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(1)\)
N is the number of pixels. One pass measures the channels, a second applies the gains.
Artifacts and limitations¶
Limitations
- Fails when the brightest area is colored, such as a sunset sky.
- Clips the pixels above the percentile.
Artifacts
- Clipped highlights. A cast when the brightest area is not neutral.
Tests¶
The reference computes the same steps with NumPy on a small RGB test image with saturated, gray, black and white pixels, and compares all three channels. The cases cover percentiles 90, 99 and 100, and both settings of Linear light.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/enhancement/enhancement.test.ts
References¶
- Land, E. H. and McCann, J. J. Lightness and retinex theory. Journal of the Optical Society of America 61(1), 1 to 11, 1971. DOI 10.1364/JOSA.61.000001.