# What is the math behind white-balance, and how to choose it?

Given a bad balanced RGB image (in terms of white balance), how can I choose the proper color balance that needs to be applyed (as a RGB vector where 128,128,128 is the neutral)?

Same question for a YUV image.

• If there is anything white or known to be gray in the picture, you can measure the white balance outright and correct for it. Digital sensors are quite linear over the dark/light range, but if this is from film then multiple gray references over the light/dark range may be needed. If none of these exist, you fiddle until it looks right. Oct 11, 2013 at 12:12
• what do you mean "measure the white balance outright". Measure the RGB value of the known white pixel? and then what. What if this "white pixel" is (250, 245, 255) ?
– Jav
Oct 11, 2013 at 12:20
• If there is a known gray target, then it can be used as a reference point. Lightroom/Aperture/ACR/Photoshop all have eyedropper tools to color correct using a known gray. Is your question how do these programs work (what is the math behind color correction)? Oct 11, 2013 at 12:33
• yes, it is more about what is the math behind (I don't use photoshop : I have a specific interface for my industrial camera that needs a RGB vector to ajust the color balance).
– Jav
Oct 11, 2013 at 12:36
• possible duplicate of Does Auto White-Balance Really Work? How? Oct 11, 2013 at 13:26

If you have a known white or grey point, you want to calculate the ratio between the colors so that each is given a factor to multiply by. For example, in your 250,245,255 reference it would be 1.02, 1.0625, 1 since multiplying the sampled values for white would produce an actual white. (255,255,255). Since the sensor should be linearly responsive, this same ratio should be consistent for the entire light range.

The exact math may vary a bit though depending on what type of entry your camera takes, but the principal is generally the same that you are trying to adjust the color channel's values by a factor to reach the corrected value.

Given a bad balanced RGB image (in terms of white balance), how can I choose the proper color balance that needs to be applyed (as a RGB vector where 128,128,128 is the neutral)?

The answer is, you can't, at least not without human judgement.

If you have an RGB image with an incorrect white balance, there is no way to detemine what the correct white balance would have been computationally, with the RGB data alone. You would need a human brain that can interpret what's in the image and what areas of it should be white or neutral, from their own experience knowing what colours things should be.

So you can never do this without some level of human guesswork or common sense for each image.

Often the easiest way to do this is to identify an area of the image that should be white or neutral grey, and adjusting the white balance such that that area is pushed towards neutral grey. Image editing programs typically have a tool for doing this. However, if there is nothing that really is neutral grey in that picture, then you will simply have to adjust it by eye based on the un-scientific approach of "what looks right".

If you were present when the picture was taken you could have held a grey card in front of the camera, lit by the same light source as the image you took and with the same white balance setting on-camera. But if you had the ability to do that, you probably also have had the ability to get the white balance right on the day.

• Cameras and software can determine correct white balance for most normal situations. I'm not sure what algorithms they use to determine an image's white balance, as I've never looked into it. Oct 17, 2013 at 20:42
• Cameras and software can't determine correct white balance, they can only take a guess. To determine correct white balance you need to have measured a white or grey reference under the same light source. Oct 18, 2013 at 0:39