White Balance Correction Using a Grey-card Reference

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: White Balance Correction Using a Grey-card Reference

Post by fmw42 »

snibgo wrote:If white is taken as 100% and black as 1/60th of this, 100%/60 (=1.6667%), the geometric mean is sqrt (100 * 100/60) = 12.9099%.

Thanks. That makes more sense and show where the 13% value originates. I did not realize black was not 0. But as I said, I know very little about photography. I appreciate this clarification. So I learned several things new today.
rcolba
Posts: 6
Joined: 2012-11-22T01:47:43-07:00
Authentication code: 6789

Re: White Balance Correction Using a Grey-card Reference

Post by rcolba »

@fmw42:
The wbref.jpg image is intended to be an picture of a grey or white reference chart taken under the same illumination conditions, most importantly the same color temperature. It shouldn't matter whether it is white or grey, as long as it is pure, meaning that R, G and B values are the same. These charts are common photography accessories.
White balance should be set to manual on your camera in order to prevent it from doing the correction similar to what we are doing with the script. (There would be a double correction otherwise.)
In order to try this processing, you can use the pictures provided in the beginning of this thread by user stupid.
Robert
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: White Balance Correction Using a Grey-card Reference

Post by fmw42 »

rcolba wrote:@fmw42:
The wbref.jpg image is intended to be an picture of a grey or white reference chart taken under the same illumination conditions, most importantly the same color temperature. It shouldn't matter whether it is white or grey, as long as it is pure, meaning that R, G and B values are the same. These charts are common photography accessories.
White balance should be set to manual on your camera in order to prevent it from doing the correction similar to what we are doing with the script. (There would be a double correction otherwise.)
In order to try this processing, you can use the pictures provided in the beginning of this thread by user stupid.
Robert

Thanks for the clarification.

Fred
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: White Balance Correction Using a Grey-card Reference

Post by anthony »

This is a nice solution. But is this the right solution?

Overlay composition is used to add highlights and shadow textures, it isn't really meant for this use.

I would have thought a 'divide' would have been the right solution to make the white card white.
http://www.imagemagick.org/Usage/compose/#divide

Though you probably want the white card slightly grey in the final image not truely white.
Perhaps that is why they use a 'grey card'?

The true test is to apply the 'fix' to the original reference image.

Code: Select all

convert reference.jpg \
        \( +clone -gravity center -crop "50x60+0+0" \
           -scale 1x1! -negate -scale 400x300! \) \
        -compose overlay -composite \
        test.jpg
Is the 'white card' in the test image now the right grayscale shade?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: White Balance Correction Using a Grey-card Reference

Post by snibgo »

The "traditional" method of correcting white balance is by multiplying the red by a certain factor, and the blue channel by another factor, in linear space (not sRGB).

The "divide" operator will do this, although in sRGB space. The other issue is that if the image of the gray card isn't mid gray, it will shift the tones in the image. (It doesn't matter whether the card is actually white or gray; IM just does arithmetic. If the pixel values are above 50%, "divide" will darken the image.)

In practise, I find that multiplying two channels by factors works well for consistent lighting conditions -- when all the objects in the photograph receive the same light. This is often not the case, especially when clouds are the lightest part of the scene. Do I balance for the clouds or foreground? Balancing the foreground with the "traditional" method will often create a horrible cast in the sky. In these conditions, "overlay" works better as it shifts the balance in the mid-tones but the effect fades away to zero at black and white pixels.

For very critical work, I use a script to extract grey patches at different tone levels (eg bright cloud, white card in sun, white card in shadow, grey card, etc) and software that generates a pair of channel curves that ensures all the gray patches become gray in the output image.
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: White Balance Correction Using a Grey-card Reference

Post by anthony »

Thank you for the clarification. It makes sense.

Note that you can convert images from sRGB to linear RGB for the composition then convert back again, after the re-balancing.

With scenes involving sky, sunlight and shadow, do you divide the image into different areas for separate adjustment?
Or just generate a single curve from all the reference cards?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: White Balance Correction Using a Grey-card Reference

Post by snibgo »

I just use a single curve for one channel, and another for a second channel, that covers all areas. The curves are derived from all the patches.

True, this assumes that the required balance is determined by the local tone in the photograph, when in fact it's determined by what light falls on the subject. I could divide the image as appropriate but, in practise, I find that the assumption is sufficiently accurate.
snibgo's IM pages: im.snibgo.com
Post Reply