Auto WB correction

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: Auto WB correction

Post by fmw42 »

Where did you get the warm and cold images? Do you have links to them or are they your photographs?
mikmach
Posts: 42
Joined: 2015-02-06T07:50:43-07:00
Authentication code: 6789

Re: Auto WB correction

Post by mikmach »

First of all - thank you very much, mask is working.
Second - sorry i was copying links to images and mislabeled one of them.
Third - these are not my photos, just among first examples when you put 'bad white balance' in Google Images
Fourth - 'my' method is variation on use of Photo filter adjustment layer in PS (second part of page here: https://helpx.adobe.com/photoshop/using ... tment.html ) and on what photographers were doing for decades when using filters like this:
https://www.bhphotovideo.com/c/buy/filt ... ilters/N/0

There are other techniques to make maybe more accurate corrections but they need more manual input and don't know how to replicate them in IM, maybe one of Grand Masters ;-) will be eager to play with them:

https://fstoppers.com/news/secret-white ... shop-79939
http://photoblogstop.com/photoshop/accu ... -photoshop
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Auto WB correction

Post by fmw42 »

Some time ago, I implemented an IM equivalent of the Photoshop color filter. See my script, colorfilter, at my link below.

The following is the equivalent and is the same as what I suggested above except for two difference. First, rather than using a mask, I adjust the -colorize percent (not -compose colorize). Second I have put in an option to do it in linear colorspace. Here are the sRGB and linear RGB codes. The second is a bit warmer, but you can change the percent (to say 55%) and get a similar result.

Input:
Image

Code: Select all

WxH=`convert warm.jpg -format "%wx%h" info:`
color=`convert warm.jpg -resize 1x1! -modulate 100,100,0 -format "%[pixel:u.p{0,0}]" info:`
convert warm.jpg -colorspace sRGB \
\( -clone 0 -fill "$color" -colorize 50% \) \
-compose colorize -composite -colorspace sRGB \
tmp2.jpg
Image

Code: Select all

WxH=`convert warm.jpg -format "%wx%h" info:`
color=`convert warm.jpg -resize 1x1! -modulate 100,100,0 -format "%[pixel:u.p{0,0}]" info:`
convert warm.jpg -colorspace RGB \
\( -clone 0 -fill "$color" -colorize 50% \) \
-compose colorize -composite -colorspace sRGB \
tmp3.jpg
Image


In my colorfilter script, I swap the two images and use -compose luminize. But this is equivalent to the above using -colorize and no swap. Also in my colorfilter script, the default percent is 25, which was chosen to match results from PS.

I have other scripts that approach whitebalancing by finding some percent of white in the image and changing the image so that it is perfectly white. Similarly, I do that with some percent gray and making that color perfectly gray. See my autotoning, autowhite and whitebalance scripts.

An advantage of the mask is that you can change it so that it affects the results differently in different parts of the image.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Auto WB correction

Post by fmw42 »

Here are two more variations. I believe tmp2 above and tmp5 are the same, but tmp6 is slightly cooler.

Code: Select all

WxH=`convert warm.jpg -format "%wx%h" info:`
convert warm.jpg \
\( -clone 0 -colorspace sRGB -resize 1x1! -resize $WxH\! -modulate 100,100,0 \) \
\( -clone 0 -fill "gray(50%)" -colorize 100 \) \
-compose colorize -composite -colorspace sRGB \
tmp5.jpg
Image

Code: Select all

WxH=`convert warm.jpg -format "%wx%h" info:`
convert warm.jpg \
\( -clone 0 -colorspace RGB -resize 1x1! -resize $WxH\! -modulate 100,100,0 \) \
\( -clone 0 -fill "gray(50%)" -colorize 100 \) \
-compose colorize -composite -colorspace sRGB \
tmp6.jpg
Image
mikmach
Posts: 42
Joined: 2015-02-06T07:50:43-07:00
Authentication code: 6789

Re: Auto WB correction

Post by mikmach »

I've found your scripts but instead study them dismissed them because they didn't have auto option :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Auto WB correction

Post by fmw42 »

What auto option do you need added? It is easy to add -auto-level if that is what you want.

All of the above code with an option for -auto-level, brightness, contrast and saturation adjustment is in my script removecolorcast at my link below.

If you want auto processing, then see my scripts autotone and autotone2 or whitebalance or autowhite.

But no method is automatic and perfect. Each method simply hides the default values for certain arguments and provides no other options to change those values.

Even in your method, you have a variable that sets the transparency or mask value to 50%. That is just a default, which can be adjusted. If you set it to 50% and make no provision for the user to change that for fine tuning, you have an "automatic" tool.

Some of my other scripts search for some percentage of the most white pixels or nearest to gray, but the default is simply set to 1%. If I were to hard code that, then it would be automatic. But I leave it as a default so that the user has the option to change it if desired.

If I misunderstand what you mean by "auto", then please clarify.
Post Reply