white-balance
algorithm
image-processing
photography
computer-vision

White balance algorithm

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

A white balance algorithm tries to remove the color cast introduced by the scene illuminant so neutral objects look neutral again. In practical terms, the algorithm estimates whether the image is too warm, too cool, or tinted, then rescales the color channels to compensate. The hard part is that the camera sees only pixel values, not the true lighting conditions, so white balance is always an estimation problem.

The Core Idea: Estimate Illumination, Then Correct the Channels

Most white balance methods follow the same structure:

  1. estimate the scene illuminant,
  2. compute channel gains,
  3. multiply the image channels by those gains,
  4. clip or renormalize the result.

A simple implementation of the gray-world assumption looks like this.

python
1import numpy as np
2
3image = np.array(
4    [[[120, 100, 80], [140, 120, 90]],
5     [[100,  90, 70], [160, 140, 110]]],
6    dtype=np.float32
7)
8
9channel_means = image.mean(axis=(0, 1))
10gray_value = channel_means.mean()
11gains = gray_value / channel_means
12balanced = np.clip(image * gains, 0, 255).astype(np.uint8)
13
14print(channel_means)
15print(gains)
16print(balanced)

This method assumes the average color in the scene should be gray. If the blue channel average is too low relative to red and green, the algorithm boosts blue. It is simple and fast, which is why it is often the first white-balance algorithm people implement.

Common Classical Algorithms

The gray-world assumption is only one member of a larger family.

White-patch or max-RGB methods assume the brightest meaningful patch in the image should be close to white. They estimate channel gains from strong highlights.

Gray-edge methods extend gray world by looking at image derivatives instead of raw color averages. The intuition is that average edge color statistics may be more stable than average pixel colors in some scenes.

Learning-based methods train a model to predict the illuminant from image content. These methods can be more accurate across diverse scenes, but they need training data and are harder to reason about.

Each approach trades accuracy, robustness, and complexity differently. A camera pipeline may even combine multiple cues rather than trusting one rule.

Why Gray World Works and Why It Fails

Gray world works surprisingly well on mixed scenes because many natural images do contain a broad spread of colors. The average can drift toward neutral if the scene is varied enough.

It fails when one color dominates the frame. A forest scene full of green leaves or a concert photo lit almost entirely in red can fool the method into “correcting” a cast that is actually part of the subject.

That is the central lesson of white balance work: you are not measuring the illuminant directly. You are inferring it from scene statistics, and the scene itself can be biased.

Add Practical Safeguards

Real implementations usually add constraints to avoid wild corrections. Common safeguards include:

  • ignoring saturated pixels,
  • excluding very dark regions,
  • applying gain limits,
  • and performing correction in linear color space rather than already tone-mapped display space.

Even a basic white-balance step becomes more stable if you ignore clipped highlights and noise-heavy shadows. Those regions often distort the illuminant estimate.

A Slightly More Practical Example

The following version avoids using extreme pixels when computing gains.

python
1import numpy as np
2
3image = np.random.randint(0, 256, size=(100, 100, 3)).astype(np.float32)
4mask = (image > 10).all(axis=2) & (image < 245).all(axis=2)
5valid_pixels = image[mask]
6
7channel_means = valid_pixels.mean(axis=0)
8gray_value = channel_means.mean()
9gains = gray_value / channel_means
10balanced = np.clip(image * gains, 0, 255).astype(np.uint8)
11
12print(gains)

The algorithm is still simple, but it avoids letting clipped or nearly black pixels dominate the estimate.

White Balance Is Not the Same as Color Grading

It is important to separate technical correction from artistic choice. White balance aims to remove unwanted illumination bias. Color grading intentionally changes mood or style.

That matters because a “perfectly neutral” result is not always the desired final image. A sunset may be more appealing with some retained warmth. A product photo may need strict neutrality. The algorithmic target depends on the application.

Common Pitfalls

  • Assuming one global algorithm such as gray world works well on every scene.
  • Estimating channel gains from clipped highlights or noisy shadows.
  • Applying white balance after strong nonlinear tone mapping instead of earlier in the imaging pipeline.
  • Confusing white balance correction with creative color grading.
  • Evaluating results only by visual preference when the task actually needs color accuracy.

Summary

  • White balance algorithms estimate scene illumination and correct channel gains.
  • Gray world is simple and fast, but it depends on scene statistics that may be biased.
  • White-patch, gray-edge, and learned methods offer different tradeoffs.
  • Practical implementations usually ignore extreme pixels and constrain channel gains.
  • White balance is a technical correction step, not the same thing as artistic color styling.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.