Getting average LAB color from a 16 bit TIFF LAB file

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
cjmi
Posts: 11
Joined: 2012-06-28T20:31:04-07:00
Authentication code: 13

Getting average LAB color from a 16 bit TIFF LAB file

Post by cjmi »

I have a 16 bit TIFF LAB file.

QUESTION 1 - Is this the best way for me to find out the average color in LAB colorspace? Since the initial TIFF is already in LAB colorspace, I want to use a method that just averages the colors in LAB -- without jumping through RGB at all. I want to avoid any type of calculations with colorspace conversions.

Code: Select all

// library already initialized, image already loaded from a file
image.colorSpace(LABColorspace);  // I guess this might be detected automatically and not needed, once I actually have a 16 bit TIFF LAB file... Right now I only have an 8 bit TIFF RGB file, but that's another post...
image.scale(Geometry(1, 1));
ColorRGB rgb(croppedImage.pixelColor(0, 0));
cout << rgb.red() << rgb.green() << rgb.blue() << endl;
QUESTION 2 - Using this method, I'm getting "0.150942/0.496986/0.537743", when Photoshop comes up with a LAB average of L:15 a:8 b:0. It's looking to me like rgb.red() * 100 will give me L, but I'm not sure how to get a and b. I read at "http://www.imagemagick.org/Usage/color_basics/" that red() stores L, green() stores a, and blue() stores b.

QUESTION 3 - In Photoshop, I've only seen integer values for RGB or LAB. Are decimal values for LAB permissible, and I've just been seeing rounded values in Photoshop?
cjmi
Posts: 11
Joined: 2012-06-28T20:31:04-07:00
Authentication code: 13

Re: Getting average LAB color from a 16 bit TIFF LAB file

Post by cjmi »

... I see a definition of "-colorspace" as "to change the way IM stores the colors of an image within memory... So I'm thinking I need to get a working LAB file, and then I won't need the image.colorspace() call.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Getting average LAB color from a 16 bit TIFF LAB file

Post by fmw42 »

IM mostly reports color values such as mean, min, max in a normalized range 0 to 1. Thus you can multiply by 100 to get percent or 255 to get 8-bit values.

Just for example
identify -verbose rose:
...

Colorspace: sRGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
Channel statistics:
Red:
min: 35 (0.137255)
max: 255 (1)
mean: 145.712 (0.57142)
standard deviation: 69.2953 (0.271746)
kurtosis: -1.385
skewness: 0.14637
Green:
min: 22 (0.0862745)
max: 255 (1)
mean: 89.2602 (0.35004)
standard deviation: 52.4698 (0.205764)
kurtosis: 2.63898
skewness: 1.81893
Blue:
min: 24 (0.0941176)
max: 255 (1)
mean: 80.4683 (0.315562)
standard deviation: 55.1114 (0.216123)
kurtosis: 3.01818
skewness: 1.9795
Image statistics:
Overall:
min: 22 (0.0862745)
max: 255 (1)
mean: 105.147 (0.412341)
standard deviation: 59.4199 (0.233019)
kurtosis: 1.2733
skewness: 1.45983


The number in parenthesis is the normalize 0 to 1 range and the other number is the raw value in the depth of the image.

More directly, for example, to get the red channel mean:

convert rose: -format "%[fx:mean.r]" info:
0.57142


convert rose: -format "%[fx:round(255*mean.r)]" info:
146

see
http://www.imagemagick.org/script/escape.php
http://www.imagemagick.org/script/fx.php

If you are already in LAB colorspace, then then using mean.r will give you the L mean, mean.g will give you the A mean and mean.b will give you the B mean.
Post Reply