Wolfram Blog
Giulio Alessandrini

New in the Wolfram Language: ColorBalance

September 2, 2015 — Giulio Alessandrini, Mathematica Algorithm R&D

I’ve taken pictures numerous times, either with a camera or with my phone, only to find out that the colors were completely off—they had bluish, reddish, or even greenish tints. Before I started working on image and color processing, this was quite mysterious to me. Moreover, I’d always noticed on my cameras a white balance setting that, when played with, produced results very much like my skewed-color photographs. Could it be these two were related?

That camera setting is indeed the key to correcting a color cast, and it has been added to the Wolfram Language with the ColorBalance function.

Here is a simple example of how it works:

Applying ColorBalance to photos

Notice how the green colors of the original picture have been corrected and the overall color distribution is now more balanced. This is exactly what is happening behind the scenes: the pixels in the image have been rescaled so that the average hue is a neutral color (i.e., a shade of gray).

Here is what ColorBalance produces when applied to images that I found searching for color cast on Google:

Applying ColorBalance to images on Google

Blue churchGreen treesYellow lab

Images after ColorBalance is applied

Without any other input, ColorBalance will try to achieve this neutral result, but in some situations you may know additional information that can be used to achieve a more accurate color balance. It’s very easy to pass that information to the function.

For instance, you can balance the apple image by specifying exactly which color you want to be considered neutral:

Balancing color in image by specific color

Or, use a mask to indicate the pixels that should be used as a reference:

Using a mask to use specific pixels as reference

You can even ask to map the reference pixels to the chromaticity of a specific color in order to add an intentional color cast to your image:

Adding color cast to your image

The option to designate a neutral area is really important. Take for example this underwater picture with a strong blue cast:

Underwater picture with strong blue cast

This is what happens when you balance it using brighter and brighter parts of the image:

Using brighter parts to balance the image

Because the image does not contain a uniform distribution of colors, we get a good balance only when we consider the brightest pixels. In this case, a better approach is to disregard the rest of the image and build the transformation by rescaling these pixels to their neutral values, that is, white, which can be done easily by changing the default method of the function:

Rescaling pixels to their neutral values

There are many more situations where you may want to slightly adjust the tones of you pictures—or even modify them entirely to create a unique effect, for example, mapping white in this image to a warmer color temperature of 3,200 kelvins:

Mapping white to a color temperature of 3,200 kelvins

Correcting colors and illumination can be as simple as multiplying your image by a number, or it can require complex color space transformations and color temperature estimation. ColorBalance is built to handle all these cases and to offer enough flexibility to implement your custom balancing procedures.

ColorBalance is part of our effort to enhance the computational photography capabilities of the Wolfram Language. ColorBalance is supported in Version 10.2 of the Wolfram Language and Mathematica, and is rolling out soon in all other Wolfram products.

Download this post as a Computable Document Format (CDF) file.

Leave a Comment


Scott Crittenden

It’s entirely possible that the yellow image is accurate. They’re all in bunny suits in what could be a clean room. Often such rooms have yellow lighting because white light causes undesired photoreactions with a common class of photoresists.

Posted by Scott Crittenden    September 3, 2015 at 8:57 am

Leave a comment


Or continue as a guest (your comment will be held for moderation):