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?".
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 »

As Bonzo says, you need spaces. The clue is in the message:

Code: Select all

convert.exe: unable to open image `(reference.jpg': No such file or directory @ error/blob.c/OpenBlob/2638.
convert is looking for a filename that starts with an open bracket.
snibgo's IM pages: im.snibgo.com
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 »

OK, I got it running. Thank you for your input.
My final solution is the following in a batch file:

@echo off
if [%1]==[] goto :eof
:loop
convert %1 ( +clone ( wbref.jpg -gravity Center -crop "50x50+0+0" -scale "1x1" -negate ) +dither -interpolate Integer -clut ) -compose Overlay -composite %~n1_wb.jpg
shift
if not [%1]==[] goto loop


on which you can drop multiple images. The convert function will correct the white balance of each of them relative to a reference image for the white balance named wbref.jpg placed in the same folder and save them back with a _wb suffix to the filename.

Unfortunately it seems as if with this method, images also tend to get darker than if I manually corrected their white balance in Lightroom.

Robert
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: White Balance Correction Using a Grey-card Reference

Post by Bonzo »

If your original images are in RAW you may need to edit the delgates.xml file

I have problems with CR2 files and from memory I needed to change:

Code: Select all

<delegate command="dcraw.exe -6 -w -O "%u.ppm" "%i"" stealth="True" decode="dng:decode"/>

To

<delegate command="dcraw.exe -4 -w -O "%u.ppm" "%i"" stealth="True" decode="dng:decode"/>
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 might point out that the above methods work because the grey patch, as photographed, has a lightness of about 50%. If it was lighter or darker, it would adjust the lightness of the result, generally creating a colour shift.

This can be cured by ensuring the patch has a lightness of 50%. Windows script:

Code: Select all

convert mainky.jpg ^
  ( reference.jpg -gravity center -crop "50x60+0+0" ^
    -scale "1x1^!" -negate ^
    -set colorspace RGB ^
      -colorspace HSL -channel Lightness -evaluate set 50%% -colorspace RGB ^
    -set colorspace sRGB ^
    -scale "400x300^!" ^
  ) ^
  -compose overlay -composite ^
  main_fixed4.jpg
snibgo's IM pages: im.snibgo.com
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 »

Thank you for this important information,

Can I also get this running somehow without giving the file size 400x300 ? I would like to use it on files with different sizes.
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 »

Code: Select all

FOR /F "tokens=1,2" %%i IN ('%IM%identify -format "%%w %%h" %1') DO (
  set WIDTH=%%i
  set HEIGHT=%%j
)
Then use those environment variables in the script.
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: White Balance Correction Using a Grey-card Reference

Post by Bonzo »

That is a useful piece of code snibgo; I may have to put another example on my website examples using it :)
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 »

Hi snibgo,
Thank you for your fast feedback.
I'm not in expert in batch file/IM syntax. Could you explain where I have to put this code exactly in my script posted before?
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 »

rcolba wrote:Can I also get this running somehow without giving the file size 400x300 ?
This is the size (width and height) of the image. You can get these dimensions by inserting this earlier in the script:

Code: Select all

FOR /F "tokens=1,2" %%i IN ('%IM%identify -format "%%w %%h" %1') DO (
  set WIDTH=%%i
  set HEIGHT=%%j
)
and then using %WIDTH%x%HEIGHT% insead of 400x300.

If you don't need the dimensions, you don't need to do this.
snibgo's IM pages: im.snibgo.com
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 »

Hi,
Here is my solution for a batch file for white balance correction on multiple files using as a reference a file called wbref.jpg (placed in the same folder) of a white or grey card (using the 50 x 50 pixels center of it) . (Win7/ImageMagick-6.8.0-Q16)
Thanks for everybody's input,
Robert

Code: Select all

@echo off

if [%1]==[] goto :eof
:loop

FOR /F "tokens=1,2" %%i IN ('%IM%identify -format "%%w %%h" %1') DO (
  set WIDTH=%%i
  set HEIGHT=%%j
)
convert %1 ^
  ( wbref.jpg -gravity center -crop "50x50+0+0" ^
    -scale "1x1^!" -negate ^
    -set colorspace RGB ^
      -colorspace HSL -channel Lightness -evaluate set 50%% -colorspace RGB ^
    -set colorspace sRGB ^
    -scale "%WIDTH%x%HEIGHT%^!" ^
  ) ^
  -compose overlay -composite ^
  %~n1_wb.jpg

shift
if not [%1]==[] goto loop
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 »

What or where can one fine the file wbref.jpg?

Just curious how your approach compares to just using -color-matrix. See http://www.imagemagick.org/script/comma ... lor-matrix, where you compute the r,g,b ratios using the averages of those pixels closest to some desired gray or white and the reference gray or white.
I have done the latter from a combination of saturation and brightness from HSB.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: White Balance Correction Using a Grey-card Reference

Post by Bonzo »

I would say make an image of your own fmw42; it is 18% grey whatever that is. There are some links here: http://www.workwithcolor.com/gray-card-6475.htm
I assume downloading this and using on the PC would work but printing it would depend on the setup of the printer? Was the OP's image taken from his camera I wonder; it is also a jpg and is it still an 18% grey after compression?
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 »

It would be nice if rcolba would provide a link to an input and output example and his wbref.jpg so that we really understand what he is doing or trying to do.

P.S. According to http://en.wikipedia.org/wiki/Gray_card an 18% graycard as shown gives color values of #D6D6D6 or rgb(214,214,214) and seems to me to be much too bright (light). That is not mid gray. However, I am not that much of a photographer to know how to translate Percent graycards into hex or rgb values.

On the other hand, from http://en.wikipedia.org/wiki/Middle_gray, it seems that

"middle gray was defined as the geometric mean intensity between a white and a black intensity that are in a ratio of 60:1. That is equivalent to 12.9% of the white intensity."

where geometric mean is given by http://en.wikipedia.org/wiki/Geometric_mean

However, right now I don't see how you can do sqrt((60x255)x0) and get anything but 0? So I obviously do not understand this yet.



It also says:

"In the sRGB color space, middle gray is equivalent to 46.6% brightness (i.e 50%,0,0 for LAB) In 24-bit color, this is rounded to RGB value (119,119,119) or #777777." See http://www.brucelindbloom.com/index.htm ... lator.html

Which makes more sense, but I am not sure if it is for 18% or 13%?

This reference is interesting but does not clarify further http://books.google.com/books?id=1JL2jF ... 25&f=false

This is a bit more helpful with several values, but does not agree with Lindstrom's calculator, http://www.workwithcolor.com/gray-card-6475.htm

This one gives another value http://www.luminous-landscape.com/forum ... #msg189465

This seems close to Lindbloom's values http://www.pentaxforums.com/forums/pent ... -card.html

According to the following http://forums.dpreview.com/forums/thread/2938408 (with an approximation to sRGB by using gamma=1/2.2=.4545)

18% Gray card = (0.18^1/2.2) = 0.4587. Thus 0.4587*255 = 117

So

12% Gray card would be 255*(0.12^0.4545) => 97

and

13% Gray card would be 255*(0.13^0.4545) => 101
Last edited by fmw42 on 2012-11-26T20:52:45-07:00, edited 13 times in total.
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 »

Don't make the mistake of thinking that a grey card should give any particular pixel values. Ideally, it should give the same value for the R, G and B channels, but we don't know what that value should be.

To demonstrate this, photograph a building. Suppose the sun is shining and the building casts a shadow. Place one gray card in the sunlight and another in the shadow. Take the photo, including the building and both cards. The pixel values from the cards will be different, because they are illuminated differently. Neither is "incorrect".

Quite likely the images of the cards will also have different hues because the one in the shadow is lit mostly by a blue sky.

As a further complication, the photographer probably aims for the maximum exposure that doesn't create clipping. (This is termed "exposing to the right".) If the photo includes just the building, the exposure can be quite large and the cards will have high pixel values. But if the photo also includes something bright, such as sky, the exposure must be reduced, and the pixel values for the cards will also reduce.
snibgo's IM pages: im.snibgo.com
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 »

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%.
snibgo's IM pages: im.snibgo.com
Post Reply