User Tools

This is an old revision of the document!


Image processing

White Balance

What is white color temperature

A “white” color has different chromatic characteristics, depending on its “temperature”. That temperature is traditionally expressed in Kelvin and corresponds to light, emitted by an ideal black body radiator, when heated to that temperature. Think of it as the color of a burning stick of wood at a camp site or a “red-hot iron” that looks more yellow when it actually melts in a furnace.

When a scene is illuminated by “white” color there's usually color correction needed to bring the “true” colors out. The “true” in this sense is an approximation of how our brains perceive colors during daylight, which is around 6000°K.

For our purposes, we are using individual color channel gains to compensate for a given temperature. Lower temperature “white light” needs a lot of blue added to it and very little red and as the temperature climbs up, the amount of added red grows while the added blue goes down.

Color correction for white temperature

For the calibration purposes we have acquired a Philips “Hue White and Color Ambiance A19 LED Starter Kit” that allowed us to test various illumination scenarios for a range of color temperatures. For a given white color temperature setting we have dialed the red and blue gains to make the scene “white” (leaving the green gain at its constant value of 1024).

Corrections to the red channel were way more noticeable than those to the blue one so we approximated the blue gains' graph with a single line, described by the formula B = 4205.4 - T*0.4087.

Red channel gains are approximated by two line segments, bordering the 3500°K mark:

below 3500°K: R = 467.6 + T * 0.166
above 3500°K: R = 82 + T * 0.277

Experimental data and verification

Here is the table with the results of that testing and corresponding calculated values based on the above formulae:

Manually set Calculated
Color, K Red Blue Red Blue
2000 800 3388 800 3388
2296 840 3285 849 3267
2592 913 3196 898 3146
2888 959 3078 948 3025
3184 1016 3005 997 2904
3480 1046 2858 1046 2783
3776 1119 2710 1128 2662
4072 1195 2519 1210 2541
4368 1296 2460 1292 2420
4664 1370 2298 1374 2299
4961 1458 2121 1456 2178
5257 1561 2033 1538 2057
5553 1620 1895 1620 1936
5849 1750 1782 1702 1815
6145 1738 1694 1784 1694
6500 1738 1694 1882 1549

Practical example

What does all that mean in practice? Suppose you have set up your scene with “white” lamps that are marked as 4100°K. Based on the above information you'd know that to get the best “natural” colors out of that scene means setting your red channel gain to 1217 and blue gain to 2529 while leaving the green gain at its default 1024 value.

Automatic white balancing

Quite often end users don't have the patience or the expertise (or both) to properly set up the white balance of the captured scene and prefer to rely on camera “just knowing what to do”.

Many algorithms are available today to deal with color normalization, some very sophisticated (and computationally expensive) and others, way less computationally expensive, but so simple that they are easily thrown off by things like a huge green screen background or a bright super-yellow T-shirt.

Below is one of those “simplistic” approaches with a little twist, that makes it an excellent option for real-time image processing on even the most basic of the devices.

Overall idea

The general idea is based on an assumption that if one takes a look around and measures the color of every pixel, then the grand total sum of all those values should be close enough to a pure grey color. This idea has a name and it is the "grey world".

Unfortunately, the world around is only grey if we are in a more-or-less neutral environment. Such is not the case with large patches of high-contrast colors (like "green screens") or even a relatively small patches of “highly toxic” colors, like bright yellow. These conditions affect the scene's average color sufficiently enough to throw it off and produce horrible color artifacts instead of normalizing the colors to their perceived values.

To combat the problem of “toxic colors”, a simple twist is introduced: ignore all the pixels that are too saturated. Now, what exactly is “too saturated” is up for a debate and is quite subjective. In our experiments, it looked like the threshold value should be chosen in such a way as to end up considering around 25-35% of all the pixels for the grey world calculation. For regular room environments, that means the threshold should be between 20 and 30 but may need adjustment either way if the scene is poorly lit (adjust down), or has lots of highly saturated bright colors (adjust the threshold up).

Once we throw away all the pixels that normally contribute to the normalization going wrong, we proceed with the “usual” grey world color normalization.

Step-by-step algorithm

Below are a few blocks of steps that describe the process in a top-down approach (which is generally easier to comprehend than the bottom-up):

  1. Decide the action to take and based on that either:
    1. Increase the “tolerance level” and go back to top
    2. Decrease the “tolerance level” and go back to top
    3. Adjust the white balance - follow to the next step
  2. Figure out the adjustment and exit, if processing a single frame and the result is “close enough”
  3. Loop back to step one

Collect statistical data set

For each pixel:

  1. Perform the RGB->Y'UV conversion
  2. Calculate the pixel's “saturation” using the following formula:
    1. (|U1)| + |V2)|) / Y3) < threshold / 1004)
      1. Of course one should be careful about dividing by 0 so first check if the Y is not 0 ;-)
  3. If the Y value is not 0 and threshold comparison yielded true - add that pixel's U and V components to our running totals and increment the total number of “good” pixels by 1
    1. Optionally collect the same data for Y value

Decide the action to take

  1. If the number of pixels that matched our filter criteria is under 20% of the total pixels in the image - that means we need to increase the “tolerance level”
  2. If the number of pixels in our data set is over 40% - decrease the “tolerance level”
  3. Otherwise we have a good data set - let's analyze it further and make the corresponding color balance adjustment(s)

Calculate color properties

Now that we have our data set we can figure out what our picture looks like from the color balance point of view. For that we just calculate the average values of U and V. The simplest approach is to calculate the arithmetic mean value, but other formulae may provide more accurate results.

At the end of this step we have the following 3 sets of numbers:

  1. “Main results” are the averages of U and V - these tell us how far off the picture is from being considered to be the “grey world”
  2. Calculate the “average” R, G, and B values from U and V.
    1. Use the Y from previous step (if it was collected) or just go with the value of 100 which is hopefully a good representation of a well-adjusted overall brightness
  3. Calculate the two ratios that represent how far off the Red and Blue colors are from the Green5). Those ratios are R/G and B/G

Figure out the adjustment

Now that we have all the information we need to make a decision let's make one!

  1. If the absolute value of average U is above some arbitrary threshold (we use 0.1) then we need to adjust the Blue channel. In our case we just change the sensor's blue channel gain by multiplying it by the B/G ratio
    1. Be careful not to overflow beyond the sensor gain's cap
    2. If the gain is already at 0 and needs to be increased - make sure you use addition, as multiplication won't help you6) ;-)
  2. Same for the Red channel, only we use the V and R/G values

The Blue channel gets a bit over-saturated on the sensor we use so a tiny adjustment is introduced: the B/G ratio is offset by 2% right before it is applied to the new gain's value.

Exposure

FIXME

1) , 2)
in range -127..+127
3)
in range 0..255
4)
convert threshold from percent value to relative value, i.e. convert, say, 13% to 0.13
5)
we use a fixed value of Green and never adjust it for color normalization
6)
or just make sure you never adjust the color channel's gain below, say, 10

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also, you acknowledge that you have read and understand our Privacy Policy. If you do not agree, please leave the website.

More information