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?".
mikmach
Posts: 42
Joined: 2015-02-06T07:50:43-07:00
Authentication code: 6789

Auto WB correction

Post by mikmach »

Hello,

I tried to create auto color correction according to well known PS method: duplicate layer, blur to average, invert H value, set opacity to please, blend in color mode.

I hit to obstacles in ImageMagick:
1) best solution would be create automatically two separate layers with appropriate settings (blend mode, opacity) and later just review them, adjust sliders when necessary and save output; but I've found that IM doesn't really support multilayer TIF files
2) after many tests I've come to settings that best 'good enough' opacity of correcting layer is B*0.5. It isn't perfect but as I said 'good enough'. But looks like Colorize composition supports neither blending intensity options nor opacity in blended file. Any suggestions how to solve that?

Code: Select all

#!/bin/zsh

LC_ALL=C
overlay_size=$(identify -format "%wx%h" "$1")
hsb_line=$(/x/Roboczy/mikolaj/im690st/convert.exe "$1" -resize "1x1" -colorspace HSB txt:- | tail -1)
hsb_vals=$(expr match "$hsb_line" '.*hsb(\(.*\))' | tr -d '%')
h_val=$(echo $hsb_vals | cut -f1 -d,); h_val=$(printf "%.0f" $(($h_val+0.5)))
s_val=$(echo $hsb_vals | cut -f2 -d,); s_val=$(printf "%.0f" $(($s_val+0.5)))
b_val=$(echo $hsb_vals | cut -f3 -d,); b_val=$(printf "%.0f" $(($b_val+0.5)))
o_val=$(printf "%.0f" $((($b_val*45)/100)))

if [[ $h_val -gt 50 ]]; then
    new_h=$(($h_val-50))
elif [[ $h_val -lt 50 ]]; then
    new_h=$(($h_val+50))
else
    new_h=0
fi
/x/Roboczy/mikolaj/im690st/convert.exe -depth 8 -size $overlay_size xc:"hsba($new_h%,$s_val%,$b_val%,0.$o_val)" o.tif
/x/Roboczy/mikolaj/im690st/convert.exe  -depth 8 "$1" o.tif -compose Colorize -composite $1.o.tif

mikmach
Posts: 42
Joined: 2015-02-06T07:50:43-07:00
Authentication code: 6789

Re: Auto WB correction

Post by mikmach »

As usual forgot:
Version: ImageMagick 6.9.3-5 Q8 x64 2016-02-20 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib cairo freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib
mikmach
Posts: 42
Joined: 2015-02-06T07:50:43-07:00
Authentication code: 6789

Re: Auto WB correction

Post by mikmach »

Tested with (script with minor corrections due to changes in HSB values reporting):
Version: ImageMagick 7.0.4-5 Q16 x64 2017-01-21 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib cairo flif freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib

Still the same.
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 »

IM doesn't really support multilayer TIF files
In what way? Can you provide an multilayer TIF and explain what does not work? Have you seen the -defines for TIFF? See http://www.imagemagick.org/script/comma ... php#define

Do you have a reference for your white balance technique?
mikmach
Posts: 42
Joined: 2015-02-06T07:50:43-07:00
Authentication code: 6789

Re: Auto WB correction

Post by mikmach »

I cannot create in IM multi-layer TIFF files which would be readable by PS. Found post on forum by one of IM developers that they don't plan to introduce this feature (note that this post was few years old).

Cannot find on the web exact copy of this but here is slightly different version:
https://www.youtube.com/watch?v=UAQsOeCCrAY

Note that Youtube video recommends complete invert of overlaying layer which in my opinion gives in some cases bad effect, reverting just Hue value is safer. Description of my version step by step (in PS, in Gimp should be very similar):

1. Duplicate background layer and rename it to corr_layer
2. Switch to corr_layer and use Filter->Blur->Average
3. Use eyedropper tool to set foreground color to color of averaged corr_layer
4. Modify foreground color in Hue (as in HSB model) by adding 180 if base value is <180 or substract 180 if base value is >180
5. Use Bucket tool to fill corr_layer with new color
6. Set opacity of corr_layer to please, by tests I've found best fit is halved Brightness of corr_layer; in tutorials in similar methods they recommend to start from 50% opacity.
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 was the exact command you tried to use to create a multi-layer TIFF and if possible provide the input and output images.
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 »

try this to emulate the video where test.png was a screen capture of the image in the video.

input:
Image

Code: Select all

wxh=`convert test.png -format "%wx%h" info:`
convert test.png \( +clone -resize 1x1! -scale $wxh! -negate \) \
-define compose:args=50,50 -compose blend -composite -auto-level result.png
Image

Adjust the blend amounts as desired.
mikmach
Posts: 42
Joined: 2015-02-06T07:50:43-07:00
Authentication code: 6789

Re: Auto WB correction

Post by mikmach »

Thank you for your help but blend mode doesn't cut it (already tried). In this case it is working because example is in so bad state that any correction is improvement. But look here:

Original image:
Image

Your method:
Image

My method (in PS):
Image

I know that in several places blend is recommended as substitute of PS color mode but in reality colorize works as real substitute and I used it successfully several times but for the first time needed transparency and cannot find the way for it to work.
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 »

Yes, the blend method is for very badly color cast images. But I do not see anything in your outline about the use of colorize. Am I missing something. Here is what you wrote before:

Code: Select all

1. Duplicate background layer and rename it to corr_layer
2. Switch to corr_layer and use Filter->Blur->Average
3. Use eyedropper tool to set foreground color to color of averaged corr_layer
4. Modify foreground color in Hue (as in HSB model) by adding 180 if base value is <180 or substract 180 if base value is >180
5. Use Bucket tool to fill corr_layer with new color
6. Set opacity of corr_layer to please, by tests I've found best fit is halved Brightness of corr_layer; in tutorials in similar methods they recommend to start from 50% opacity.
I believe that changing the hue by 180 is the same as getting the complement color or negating the color.

If you can provide a better PS tutorial, I can try to duplicate it and then see about how to do it in IM.

Where to you need transparency in this procedure? You can always save the alpha channel, process the opaque image, then add the alpha channel back at the end. Does that not do what you want?
mikmach
Posts: 42
Joined: 2015-02-06T07:50:43-07:00
Authentication code: 6789

Re: Auto WB correction

Post by mikmach »

Ups sorry. Between 5 and 6 - use PS Color layer blending mode Color (in IM jargon: compositing mode - Colorize) to blend two layers.

In this particular case complete invert/negate is almost undistinguishable from invert of just Hue channel so posted only one example. But difference is there and in other cases it is more prominent.

Transparency is needed to limit effect of Colorize composition mode, not in main layer.
mikmach
Posts: 42
Joined: 2015-02-06T07:50:43-07:00
Authentication code: 6789

Re: Auto WB correction

Post by mikmach »

Here is another example, this time with cold image.

Original:
Image

Your method:
Image

Your method:
Image

As you see your method with Blend compositing overshoots. In case of warm images it makes them too cold, in case of cold images it makes them too warm.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Auto WB correction

Post by snibgo »

I think this is a weird method for WB correction.

If I take a photo of green grass, the photo will be green. But this method will find the average colour is "green", and the opposite is "magenta". If that colour is used to "-compose Colorize", the result will be a magenta image of grass. Or, if we use a 50% blend, we will get a monochrome result.

I can see the method might be used to remove an extreme colour bias, but I can't see how the method might give a correct result.
snibgo's IM pages: im.snibgo.com
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 »

Try this. In IM rather than using transparency, create a mask image for the blending during the -compose colorize.

See http://www.imagemagick.org/Usage/compose/#compose

Unix Syntax:

Code: Select all

WxH=`convert warm.jpg -format "%wx%h" info:`
convert warm.jpg \
\( -clone 0 -resize 1x1! -resize $WxH! -modulate 100,100,0 \) \
\( -clone 0 -fill "gray(50%)" -colorize 100 \) \
-compose colorize -composite result.jpg
The modulate 100,100,0 changes the hue by 180 degrees in HSL colorspace (unless you use a define to change the colorspace to HSB, HCL, etc).

You can adjust your effective tranparency by changing the gray(50%) to some other percent in the mask image.

It seems to work for me on your warm.jpg. Let me know.

Also in IM 7, you can do it all in one command line as

Code: Select all

magick warm.jpg -set option:WxH "%wx%h" \
\( -clone 0 -resize 1x1! -resize "%[WxH]"! -modulate 100,100,0 \) \
\( -clone 0 -fill "gray(50%)" -colorize 100 \) \
-compose colorize -composite result2.jpg
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 »

snibgo wrote: I can't see how the method might give a correct result.
That was my first reaction, also. But it seems to work because one is only using 50% colorize compositing. I do not understand it, but it does work. In PS, when I was checking this, before I specified 50% transparency, I got the opposite color cast. But when I moved the transparency slider to 50%, it looked good.
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 »

mikmach wrote: 2017-01-28T15:59:33-07:00 Here is another example, this time with cold image.

Original:
Image

Your method:
Image

Your method:
Image

As you see your method with Blend compositing overshoots. In case of warm images it makes them too cold, in case of cold images it makes them too warm.

Both results are labelled "Your method". I assume the first is "my" method and the second is "your" method.

But if it overshoots, then you can simply adjust the blending amount. However, I do agree, your method does seem better.

P.S. It is not my method, but the method uses in your first reference. I was just trying to implement what they did in PS with the equivalent in IM.
Post Reply