Grey Card Color Balancing Like Lightroom

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?".
Post Reply
nits.bikash
Posts: 1
Joined: 2017-08-08T06:07:54-07:00
Authentication code: 1151

Grey Card Color Balancing Like Lightroom

Post by nits.bikash »

I am trying to implement Grey Card color balancing of Adobe Lightroom using ImageMagic. I got some information from viewtopic.php?f=1&t=15564&sid=f885b9f7f ... f&start=30 to balance color using grey card:-

Code: Select all

convert MAIN.JPG ^
( +clone ( REFERENCE.JPG -gravity Center -crop "128x128+0+0" -scale "1x1" -negate ) +dither -interpolate Integer -clut ) ^
-compose Overlay -composite ^
FIXED.JPG
I tried this one but the result is different from lightroom. Could anyone tell me why this is not accurate and how will get the accurate image?

Here are the images:-

Original one:-
Image

Balanced using Lightroom:-
Image

Balanced using ImageMagick:-
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Grey Card Color Balancing Like Lightroom

Post by fmw42 »

Try using -color-matrix. I have cropped the image in a region inside the gray card and get its average and divide that into 0.5 to get a ratio of true gray to average gray for each channel. See http://www.imagemagick.org/script/comma ... lor-matrix

In unix syntax, this would be either of the following:

Code: Select all

infile="8Mvyi.jpg"
gray=0.5
rratio=`convert "$infile" -crop 90x40+106+208 +repage -format "%[fx:$gray/mean.r]" info:`
gratio=`convert "$infile" -crop 90x40+106+208 +repage -format "%[fx:$gray/mean.g]" info:`
bratio=`convert "$infile" -crop 90x40+106+208 +repage -format "%[fx:$gray/mean.b]" info:`
echo "$rratio $gratio $bratio"
convert "$infile" -color-matrix \
"$rratio 0 0 \
0 $gratio 0 \
0 0 $bratio" result.jpg

Code: Select all

infile="8Mvyi.jpg"
gray=0.5
declare `convert "$infile" -crop 90x40+106+208 +repage -format "rratio=%[fx:$gray/mean.r]\ngratio=%[fx:$gray/mean.g]\nbratio=%[fx:$gray/mean.b]\n" info:`
echo "$rratio $gratio $bratio"
convert "$infile" -color-matrix \
"$rratio 0 0 \
0 $gratio 0 \
0 0 $bratio" result.jpg
Image


However, if I measure the gray card in the result from Lightroom, I find a gray value, not 0.5 (middle gray), but

Code: Select all

convert YQykH.jpg -crop 90x40+106+208 +repage -format "%[fx:mean]" info:
0.67397

So I use that value, then

Code: Select all

infile="8Mvyi.jpg"
gray=0.67397
declare `convert "$infile" -crop 90x40+106+208 +repage -format "rratio=%[fx:$gray/mean.r]\ngratio=%[fx:$gray/mean.g]\nbratio=%[fx:$gray/mean.b]\n" info:`
echo "$rratio $gratio $bratio"
convert "$infile" -color-matrix \
"$rratio 0 0 \
0 $gratio 0 \
0 0 $bratio" result2.jpg
Image

Adjust the gray=value as desired.

Note that Wikipedia specifies the sRGB color for a gray card as 46.6% or 0.466. See https://en.wikipedia.org/wiki/Middle_gray


Alternately, Lightroom is likely processing this in linear RGB. So this looks like what they are doing:

Code: Select all

infile="8Mvyi.jpg"
gray=0.5
declare `convert "$infile" -colorspace RGB -crop 90x40+106+208 +repage -format "rratio=%[fx:$gray/mean.r]\ngratio=%[fx:$gray/mean.g]\nbratio=%[fx:$gray/mean.b]\n" info:`
echo "$rratio $gratio $bratio"
convert "$infile" -colorspace RGB -color-matrix \
"$rratio 0 0 \
0 $gratio 0 \
0 0 $bratio" -colorspace sRGB result3.jpg
Image
Post Reply