Skip to content

Gray-world correction

Fact Value
Also called Gray world white balance, Gray world assumption
Status Implemented and tested
Family Enhancement, illumination and color
Engine Browser
Type Linear, color, mean-based channel gains
Output Image
Operator shown as color-transform (exact)
Support radius global
Code web/src/core/filters/enhancement/white-balance.ts, grayWorldFilter
Operator code web/src/core/filters/enhancement/white-balance.ts, grayWorldGains

Overview

Gray-world correction balances colors with no input. It assumes that the scene averages to gray, so it scales the channels until their means are equal. It works well on scenes with many colors and fails on scenes dominated by one color.

Two panels: the color chart with a strong orange cast, and the chart after gray-world correction with the cast mostly removed. Two panels: the color chart with a strong orange cast, and the chart after gray-world correction with the cast mostly removed.
The chart with a tungsten cast, made by adapting it from D65 to illuminant A, and after gray-world correction.

How it works

  1. Compute the mean of each channel over the whole image.
  2. Compute the mean of the three means, the gray they should share.
  3. Scale each channel by that gray over its own mean.

Mathematical foundation

\[ g_C = \frac{\bar m}{m_C}, \quad \bar m = \frac{m_R + m_G + m_B}{3}, \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

  • \(m_C\): The mean of channel C over the image
  • \(\bar m\): The mean of the three channel means
  • \(g_C\): The gain of channel C

m_C is the mean of channel C over the whole image. Buchsbaum's gray-world assumption says that the average color of a scene is gray. So any difference between the channel means is taken to come from the light, and the gains remove it. After the gains, the three means are equal. 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 with mean 0 gets gain 1.

Buchsbaum proposed that the average of the surfaces in a scene is gray. Under that assumption, the average color of the image is the color of the light, and dividing by it removes the light. The assumption is about the scene, so a green forest breaks it: the filter takes the green for a cast.

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. Compute the mean of each channel over the whole image, in linear light if it is on.
  2. Compute the gains from the mean of the three means.
  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
Linear light linearLight on On or off Scale the linear light values instead of the encoded sRGB values.

Implementation

grayWorldFilter in web/src/core/filters/enhancement/white-balance.ts measures the means with grayWorldGains. It is a global color filter, so it always sees the whole image.

Worked example

Image Mean red Mean green Mean blue
Warm cast 147.55 101.02 49.80
Gray world, encoded values 99.47 99.47 98.87

With Linear light off, the gains act on the encoded values, so the channel means become equal, up to rounding and clipping. With Linear light on, the default, the means of the linear values become equal instead.

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 on scenes that are mostly one color, such as a forest or a sunset. It turns them gray.

Artifacts

  • A cast in the opposite color on scenes dominated by one color.

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 both settings of Linear light.

References

  • Buchsbaum, G. A spatial processor model for object colour perception. Journal of the Franklin Institute 310(1), 1 to 26, 1980. DOI 10.1016/0016-0032(80)90058-7.