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?".
stupid
Posts: 51
Joined: 2010-02-12T07:30:34-07:00
Authentication code: 8675308

White Balance Correction Using a Grey-card Reference

Post by stupid »

Hi. Thanks for the forum.

There is a well-known way of correcting the white balance of any photograph using a second photograph, with neutral grey target in shot, taken under the same lighting conditions. The question is simply how to do this using IM. I thought of doing this by averaging the colour of a crop of the grey target (-scale 1x1 ); negating the colour; then compositing a full-sized canvas of the resulting colour onto the original image using the Soft_Light method. This does work using an average of the whole main image rather than the reference image; but not very well. I'd appreciate a few pointers. Thanks.

Jo
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 »

can you post links to an image that needs correcting and the image of the graycard?
stupid
Posts: 51
Joined: 2010-02-12T07:30:34-07:00
Authentication code: 8675308

Re: White Balance Correction Using a Grey-card Reference

Post by stupid »

Hi to you.

I should have explained myself better. It's standard practice with some photographers to take a shot of a reference grey-card at the beginning of a sequence of images. This reference shot becomes the guide to the colour cast in all the images until the light is different and requires a new reference shot. In Photoshop you click on the grey-card colour (cast) in the reference shot using the dropper tool ; then (using levels) click on the linked images to correct them by the colour cast of the grey-card in the reference. This is a general question not a specific one so there are no particular images to show. I can't correct image by image because there would be too many images in a given sequence. The fix must be like the PS one - accurate every time.
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 »

I cannot help without an example to see how it works. I don't have a sequence of images nor an example graycard to even try it on PS. Not sure my PS CS is current enough anyway. Without more knowledge about what algorithm PS uses, it will be hard to develop without examples.

Also just for my own edification, does not the graycard image have variations in exposure, so that one eyedropper click may not be representative over the whole image? Or is this not important for the solution you want.

I can try using PS (if mine is not too old) with some mid gray shade and see how it affects some images. Perhaps I can see what is going on.

But real images may be more helpful.

P.S. I have a whitebalance script already, but it was used to correct white. However, it might be useable or adaptable to gray if one knows the exact value of the true gray that the graycard is supposed to represent. I am not much of a photographer and so don't really know this. But I can see what I can find via a google search. Any way feel free to try my script (if on unix/linux or windows cygwin). see link below
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: White Balance Correction Using a Grey-card Reference

Post by el_supremo »

I have a whitebalance script already, but it was used to correct white. However, it might be useable or adaptable to gray if one knows the exact value of the true gray that the graycard is supposed to represent
Hi Fred,
You don't need to know the true gray values of the card. The essential fact is that a gray card should have equal Red, Green and Blue components. In a photo which hasn't been properly balanced, the gray will have unequal RGB values and all you have to do is shift them so that they are equal. If you come up with a gray value that isn't exactly the same as the card all that means is that the overall image will be a bit darker or brighter, but that is easily fixed if required.
Also just for my own edification, does not the graycard image have variations in exposure, so that one eyedropper click may not be representative over the whole image?
Yes, it can do, particularly if it was shot in low light. I started writing a program to do colour balancing by being given the coordinates of a rectangular area of the gray card. It was then going to average that area and use the result as the gray balance for other photos. I didn't get very far with it though.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
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 »

el_supremo wrote:You don't need to know the true gray values of the card. The essential fact is that a gray card should have equal Red, Green and Blue components. In a photo which hasn't been properly balanced, the gray will have unequal RGB values and all you have to do is shift them so that they are equal. If you come up with a gray value that isn't exactly the same as the card all that means is that the overall image will be a bit darker or brighter, but that is easily fixed if required.
OK, but I was trying to be specific to "true" graycard gray. Nevertheless, my whitebalance script use -recolor to do its work. That would be where I would start. It needs a color transformation matrix, which is just the diagonal parts the rest are zero, if I recall. Then you just need to adjust the ratios of the redin/redout, greenin/greenout and bluein/blueout (may have that upside down until I check again) so that the out values of red, green and blue are equal and the in values are your near gray values for red, green and blue.

I was out all day today, but may look into this further over the weekend or next week.
stupid
Posts: 51
Joined: 2010-02-12T07:30:34-07:00
Authentication code: 8675308

Re: White Balance Correction Using a Grey-card Reference

Post by stupid »

Hello again. I've solved the problem. It goes like this...


convert MAIN.JPG ^
( +clone ( REFERENCE.JPG -gravity Center -crop "128x128+0+0" -scale "1x1" -negate ) +dither -interpolate Integer -clut ) ^
-compose Overlay -composite ^
FIXED.JPG



The white reference target will be discolored by the prevailing light. All you need to do is get the inverse colour and overlay it on the main image. My stupid mistake was to use a grey target - der!

Example:

REFRENCE IMAGE CONTAINING TARGET
Image
(The square in the centre of the image is white - honest!)

IMAGE TO FIX
Image

FIXED IMAGE
Image

If you want try this; use a white target (such as the white square of a QPCard) and set the camera to the preset that is nearest to the prevailing light conditions. Don't use AUTO! Take a snap with the white target in it, then shoot at that setting until the light has varied and you need a new reference shot. When you're done, run IM on the main sequence of images using each relevant reference image to fix the white balance in the images that follow until the next reference shot - and so on.


Thanks for taking an interest.


Jo
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: White Balance Correction Using a Grey-card Reference

Post by el_supremo »

I found one webpage that says
In theory, a shot of an 18% gray card in sRGB color space (gamma=1.8 ) should be RGB 117:117:117
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
stupid
Posts: 51
Joined: 2010-02-12T07:30:34-07:00
Authentication code: 8675308

Re: White Balance Correction Using a Grey-card Reference

Post by stupid »

That's true for a grey-card that's intended for exposure control. The theory says that an average scene is 18% grey (tonally). In truth only Ansel Adams wanted 18% - for his own reasons. In the real world an average scene is about 13% grey and all digital cameras conform to that percentage for metering.

The grey-cards I was attempting to read the colour-cast from are light grey. They're used as white-cards but are toned down to lessen the chance of glare which would give a false reading.

I don't know why I suddenly went off on a mad excursion but the solution - once I swapped to a white-card - works well I think; though if it won't apply routinely I'm sure someone will tell me.


Jo
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 »

I tried your solution and it is very nice. In looking at the -compose overlay, I see it is mostly for adjusting midtones, if I am not mistaken. My whitebalance script works relative to true white or whatever color you want as the reference and uses -recolor to adjust the whole image in proportion. On your image and your supposed white card, it over did the adjustment. But you say in you last message that your white card is not really white? Is that correct? I was assuming it was pure white, so that could explain some of it. Do you know what actual shade of gray it is?

But I found that your subsection 128x128 about the middle covered more than your "white" card. I tested it again using 50x60 about the middle to get just the card and got a slightly different but similar result.

I found that I got the same result by using:


convert mainky.jpg \
\( reference.jpg -gravity center -crop "50x60+0+0" \
-scale 1x1! -negate -scale 400x300! \) \
-compose overlay -composite \
main_fixed3.jpg


which is simpler but needs to know the size of the input image.


Any way, it is a nice solution and I have learned something new on this. Thanks.
stupid
Posts: 51
Joined: 2010-02-12T07:30:34-07:00
Authentication code: 8675308

Re: White Balance Correction Using a Grey-card Reference

Post by stupid »

Hi, Fred?

You're right about the 128x128; that was for the full size images. I also see what you mean about the overlay compositing method but I can't see a way to do without needing shifts in levels, which I was trying to avoid because of the possibility of clipping - I might've misunderstood the nature of level though. I had thought of the above as fine-tuning rather than a full-on change of colour and tone.

The white/light-grey/dark-grey card I'm using is here http://www.qpcard.se/BizPart.aspx?tabId ... =1&catId=1 (with some technical information).

My usual way of setting the white-balance was to read it directly into the camera from the white square on a QPcard. I thought I'd try something else that was possibly faster/easier and because the reference shots can also function as separators between lighting conditions combined with location, which is sometimes useful.

Another problem with my method is that in cases where no alteration is required it still recolors the working image, darkening it too. I think it needs some sort of bail-out condition added.

I saw your whitebalance script. It works very well; but I'm thinking that it was intended for bigger colour-shifts than I'm working with?

Maybe a compositing method similar to overlay that works pan-tonally could be considered? Perhaps I'm thinking of blend.


Jo
stupid
Posts: 51
Joined: 2010-02-12T07:30:34-07:00
Authentication code: 8675308

Re: White Balance Correction Using a Grey-card Reference

Post by stupid »

No, I'm not thinking of blend; it's just a 'tracing-paper' method really.
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 »

Yes, your method seems to be more of a fine tune. that was what I meant by mid-tone only adjustments. My technique tries to recolor the image from a color matrix and applies it to every pixel, not just mid-tones. So it makes a stronger change and with your image and assuming the card was really white, tends to overcorrect the image and make it too white.
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,

I find this white balancing methods very useful and would like to use them .
I tested the two methods decribed here (IM Version 6.8.0-Q16 under windows7) on the files given above mainky.jpg and reference.jpg and found out that I have to give a complete path for the reference file, otherwise I get the following error:

convert.exe: unable to open image `(reference.jpg': No such file or directory @ error/blob.c/OpenBlob/2638.

I tried the method given by 'stupid':

G:\tmp\test>convert mainky.jpg (+clone (G:\tmp\test\reference.jpg -gravity Center -crop "128x128+0+0" -scale "1x1" -negate ) +dither -interpolate Integer -clut ) -compose Overlay -composite fixed1.jpg

and got the error:

convert.exe: unable to open image `(+clone': No such file or directory @ error/blob.c/OpenBlob/2638.

I then tried the method given by 'fmw42':

convert mainky.jpg (G:\tmp\test\reference.jpg -gravity center -crop "50x60+0+0" -scale 1x1! -negate -scale 400x300!) -compose overlay -composite fixed2.jpg

and got a result in fixed2.jpg, but it is a uniform grey image.

I would be glad if someone could check this out and give me some clues? Ideally the method should not be dependant on file size. Thank you
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 »

convert.exe: unable to open image `(+clone': No such file or directory @ error/blob.c/OpenBlob/2638.
Try changing the (+clone to ( +clone I think you should always have a space between the code and any ( or )

I tend to use simple batch files and you can drop the image over the batch file icon which is simpler than using the command prompt
Post Reply