Skip to content

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.

Three panels: the color chart with a strong orange cast, the chart after white-patch correction at percentile 100, and at percentile 99, both with less cast and pinkish highlights. Three panels: the color chart with a strong orange cast, the chart after white-patch correction at percentile 100, and at percentile 99, both with less cast and pinkish highlights.
The chart with a tungsten cast, made by adapting it from D65 to illuminant A, after white-patch correction at percentiles 100 and 99.

How it works

  1. Find the chosen percentile of each channel over the whole image.
  2. Take its inverse as the gain of that channel.
  3. Multiply every pixel by the gains, so the bright values reach white.

Mathematical foundation

\[ g_C = \frac{1}{P_{ 99 }(C)}, \quad C' = \begin{cases} \operatorname{srgb}\left(g_C \operatorname{lin}(C)\right) & \text{linear light} \\ g_C\, C & \text{otherwise} \end{cases} \]

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

  1. Find the chosen percentile of each channel over the whole image.
  2. Decode it to linear light, if Linear light is on, and take the gains as its inverse.
  3. Scale each channel by its gain, in linear light or on the encoded values.
  4. 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.

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.