How can I white-balance using white, gray, and black reference cards mathematically?
Asked 3/16/2016
2 views
2 answers
0
I want to correct white balance manually rather than using an auto tool. In one image I have white, gray, and black reference cards. For example, the white card measures about RGB (181, 176, 141) and the black card about (64, 61, 38). If I simply scale channels so the white card becomes neutral, the black card no longer maps to true black. What is the correct mathematical approach for using these references? Should white balance be based only on the white card, or should black and gray also be used? I’m aiming for a repeatable, rigorous workflow across multiple images.
Originally by Photography Stack Exchange contributor. Source · Licensed CC BY-SA 4.0
Photography Stack Exchange contributor
10y ago
2 Answers
4
Why are you trying to match 3 values with a linear relation?
Consider only the red channel. You have 3 reference values (taken from your original photo): Rw for the white color, Rg for the gray and Rb for the black.
You want to find a function mapping Rb to 0, Rg to 128 and Rw to 255. With 3 references, you can't use a linear function if you want a perfect match, you have to use a polynomial function with a degree at least of N-1, N being the number of references to match.
The grey reference (here 128) is related to an 18% grey card (more info: Why is 18% grey considered to be in the middle for photography?). It may be different for your grey.
The function ax² + bx + c = v (1) is of degree 2, exactly what you want for a perfect match ("fitting") with 3 values. Now, you want to find a, b and c such as (1) is verified for Rb, Rg and Rw. In other words, you have 3 equations:
- a (Rb)² + b (Rb) + c = 0
- a (Rg)² + b (Rg) + c = 128
- a (Rw)² + b (Rw) + c = 255
Solving this equation is easy (but not related to photography).
Once you have found a, b and c you can map any red value to a "calibrated" red. You may have negative values or values greater than 255, be careful.
An example maybe?
If Rb=10, Rg=100 and Rw=254, solving for (1) will yield a=-0.002449, b=1.6916 and c=-16.671 (need help ? Wolfram Alpha) so (1) <=> f(x)=-0.002449*x²+1.6916*x-16.671.
On the absciss (x-axis) lies your original values, on the ordinate (y-axis) the "calibrated" ones.
As you can see, all original values bellow 10 are mapped to negative values and should be set to 0... and values beyond 254 are mapped to values greater than 255. In fact, the [0,255] interval is mapped to [f(0),f(255)] = [-16.671,255.44].
If you don't want to clip your values (=losing information), you should use a new function to map [-16.671,255.44] to [0,255]. This function can be linear, such as ax+b.
You can replace (1) with any polynomial function of degree 2, you will get differents results.
If you really want to use a linear relation, say ax+b=v, you will need to find a and b minimizing errors between expected values (0, 128 and 255) and "calibrated" ones.
For more information on Polynomial Interpolation, take a look at https://math.stackexchange.com/questions/7125/polynomial-fitting-how-to-fit-and-what-is-polynomial-fitting or in Wikipedia (https://en.wikipedia.org/wiki/Polynomial_interpolation and https://en.wikipedia.org/wiki/Curve_fitting)
Originally by user26456. Source · Licensed CC BY-SA 4.0
user26456
10y ago
0
Generated from our catalog & community — verify before relying on it.
White balance itself is normally a per-channel gain: multiply R, G, and B so a neutral reference becomes neutral. In practice, use one neutral reference (white or gray) and do this in a linear space, ideally from RAW data.
Why your black card doesn’t go to (0,0,0): white balance is not a full tonal correction. If the black patch is lifted, that usually indicates a black offset, flare, ambient tint, or a tone curve/gamma already applied. Multiplying channels to fix white will not also force black to zero.
So the rigorous workflow is:
- Shoot RAW.
- Convert to a linear color space.
- Use a neutral white or gray card to compute channel multipliers for white balance.
- If needed, separately correct black level/offset before or apart from white balance.
Using white, gray, and black as three exact constraints requires a non-linear fit (for example, a quadratic per channel), but that is no longer just white balance; it becomes a tone/color mapping and can introduce false color if the underlying issue is not uniform.
In short: for accurate, repeatable white balance, use a single neutral reference in linear RAW data; treat black-level problems as a separate correction.
Recommended products
UniqueBot
AI10y ago
Your Answer
Related Questions
Why do some gray card sets include white and black cards too?
Are 18% gray cards mainly for exposure, and can they also be used for white balance?
How can I use a gray reference card to set white balance and brightness for scientific RAW color analysis?
Why do gray and white cards give different white balance readings in Camera Raw?
How can I use a gray card to judge darkroom print exposure?
