How can I change an image’s white balance/color temperature programmatically?

Asked 5/20/2011

5 views

2 answers

0

I want to adjust an image’s white balance in code rather than in an editor. For example, if I assume an image is currently balanced to about 6500K, how would I algorithmically make it warmer or cooler to a chosen color temperature?

I’m especially interested in the difference between working from RAW data versus a processed file like JPEG. Is this basically a color-space/white-point conversion, or is it more like scaling color channels? And if RAW is preferred, is that because the sensor data remains linear and camera metadata helps make the conversion more reliable?

Originally by Photography Stack Exchange contributor. Source · Licensed CC BY-SA 4.0

Photography Stack Exchange contributor

15y ago

2 Answers

7

You would need to do a color space white point conversion in L*a*b* space. Most color models that we normally work with, primarily RGB but often CMYK, are designed to support the limitations and requirements of physical hardware. Those models usually don't represent the "perceptual" space of color very well, though, and white point is definitely a perceptual aspect of color.

Whenever you need to apply perceptual adjustments to an image, such as color temperature or tint shifts, it is best done by converting from RGB to XYZ, and during the conversion, you can apply "reference white points" for the source and destination color space. Conversion from XYZ to Lab then gets you into a color space where you can perceptually remap the white point and all the colors such that they maintain continuity.

Color space conversions, chromatic adaptation, white point adjustments, etc. are rather complex mathematically. The more perceptually accurate you intend to be, the more complex the math usually ends up. An excellent source of information for color space conversions can be found at Bruce Lindbloom's web site. Some additional useful information can be found on Wikipedia. You might also find this information on CIE's D-Series Illuminants useful, as it contains white point specific information, calculations, and constants.

Originally by user124. Source · Licensed CC BY-SA 4.0

user124

15y ago

0

AI Answer

Generated from our catalog & community — verify before relying on it.

For white balance, the practical method is usually to work in linear RGB and apply per-channel gains, typically to red and blue:

Rout = Rin × Rgain
Gout = Gin
Bout = Bin × Bgain

This is why RAW is preferred: RAW data is closer to linear scene data, so channel scaling behaves predictably. JPEG is less suitable because it usually has a tone curve/gamma and other processing applied, which breaks that linear relationship. If needed, you’d first have to invert that nonlinearity as much as possible.

Conceptually, white balance can also be described as a white-point conversion through color spaces such as RGB → XYZ → Lab, where white point is handled more perceptually. That’s a more color-science-oriented framing, but for image processing, white balance is commonly implemented as channel gains in linear RGB.

So: if you want reliable temperature shifts, start from RAW or other linearized data, compute the red/blue gains for the desired source and target white points, apply them, then continue normal rendering. For mixed lighting, a single global temperature adjustment may still not fully fix the image.

UniqueBot

AI

15y ago

Your Answer