Page 1 of 1
Retrieving the individual Image Channel Statistics
Posted: 2012-05-14T21:52:59-07:00
by yazinsai
Hi, I'm developing a website that uploads an image and reads the colorspace and Mean for each of the R,G and B channels.
I've been able to read the colorspace using identify -format %[colorspace] rose.jpg
But I'm stuck in trying to figure out how to read the Mean for each Channel..
The output for:
includes the details i'm looking for:
Code: Select all
...
Channel statistics:
Red:
min: 0 (0)
max: 255 (1)
mean: 231.673 (0.908523) [b]<!-- This is what i'm after[/b]
standard deviation: 66.5237 (0.260877)
kurtosis: 7.3051
skewness: -3.00254
Green:
min: 0 (0)
max: 255 (1)
mean: 227.871 (0.89361) [b]<!-- This too[/b]
standard deviation: 70.4308 (0.276199)
kurtosis: 5.16135
skewness: -2.59435
Blue:
min: 0 (0)
max: 255 (1)
mean: 224.366 (0.879867) [b]<!-- And this, please[/b]
standard deviation: 77.2012 (0.30275)
kurtosis: 3.677
skewness: -2.35433
...
What is the most efficient way of extracting this information from the image?
Re: Retrieving the individual Image Channel Statistics
Posted: 2012-05-14T22:23:27-07:00
by anthony
The "sed" command can search for and extract the desired part of the line.
Code: Select all
identify -verbose rose: | \
sed -n 's/.*\(Red\|Green\|Blue\|Overall\):.*/\1 /p; s/.*mean:.*(\(.*\)).*/\1/p'
Red
0.57142
Green
0.35004
Blue
0.315562
Overall
0.412341
Re: Retrieving the individual Image Channel Statistics
Posted: 2012-05-14T23:16:51-07:00
by yazinsai
Works! Thanks Anthony
Re: Retrieving the individual Image Channel Statistics
Posted: 2012-05-14T23:28:27-07:00
by anthony
Ask no questions, and you shall be told no lies
Comment on your signature...
Ask no questions and the world will not advance!
But this quote comes to mind...
All men must die, it was their single common heritage.
But a book need never die and should not be killed;
books were the immortal part of man.
-- Robert A. Heinlein, "Farnham's Freehold"
Re: Retrieving the individual Image Channel Statistics
Posted: 2012-05-15T00:20:54-07:00
by yazinsai
Hey is there any way that I can get the colorspace in the same command so it's more efficient than running "identify" twice? Thanks!
P.S: Nice quote

Mine's not fitting for a forum where questions are meant to be asked. Got it changed
anthony wrote:The "sed" command can search for and extract the desired part of the line.
Code: Select all
identify -verbose rose: | \
sed -n 's/.*\(Red\|Green\|Blue\|Overall\):.*/\1 /p; s/.*mean:.*(\(.*\)).*/\1/p'
Red
0.57142
Green
0.35004
Blue
0.315562
Overall
0.412341
Re: Retrieving the individual Image Channel Statistics
Posted: 2012-05-16T21:16:29-07:00
by anthony
yazinsai wrote:Hey is there any way that I can get the colorspace in the same command so it's more efficient than running "identify" twice? Thanks!
Code: Select all
identify -verbose rose: | \
sed -n 's/.*\(Red\|Green\|Blue\|Overall\):.*/\1 /p; s/.*mean:.*(\(.*\)).*/\1/p; /Colorspace:/p'
You can either find and modify the line using
s/..../...../p or just print the line using
/..../p
"sed" is a very versitile 'stream editor', and you can write some very complex text processing with it. Others that are in some ways simplier, but with a different focus, is "awk" and "perl".
Note however that this is 'shell' specific, and not really ImageMagick, but I help when I can.

Re: Retrieving the individual Image Channel Statistics
Posted: 2012-05-16T21:40:44-07:00
by fmw42
why not just do:
convert rose: -format "%[colorspace]\n%[fx:mean.r]\n%[fx:mean.g]\n%[fx:mean.b]\n%[fx:mean]" info:
sRGB
0.57142
0.35004
0.315563
0.412341
P.S. when I do
identify -verbose rose: | \
sed -n 's/.*\(Red\|Green\|Blue\|Overall\):.*/\1 /p; s/.*mean:.*(\(.*\)).*/\1/p'
I do not get the names: red, green, blue and overall listed to the terminal as Anthony showed earlier!
Re: Retrieving the individual Image Channel Statistics
Posted: 2012-05-17T01:02:33-07:00
by yazinsai
You Sir are amazing! I didn't find the mean.r/g/b references in the -format page! THANK YOU!!
fmw42 wrote:why not just do:
convert rose: -format "%[colorspace]\n%[fx:mean.r]\n%[fx:mean.g]\n%[fx:mean.b]\n%[fx:mean]" info:
sRGB
0.57142
0.35004
0.315563
0.412341
P.S. when I do
identify -verbose rose: | \
sed -n 's/.*\(Red\|Green\|Blue\|Overall\):.*/\1 /p; s/.*mean:.*(\(.*\)).*/\1/p'
I do not get the names: red, green, blue and overall listed to the terminal as Anthony showed earlier!
Re: Retrieving the individual Image Channel Statistics
Posted: 2012-05-17T01:04:08-07:00
by yazinsai
Thanks Anthony! I did find a guide that explains how SED works in detail, but need to get around to reading that sometime. Really appreciate your help..
anthony wrote:yazinsai wrote:Hey is there any way that I can get the colorspace in the same command so it's more efficient than running "identify" twice? Thanks!
Code: Select all
identify -verbose rose: | \
sed -n 's/.*\(Red\|Green\|Blue\|Overall\):.*/\1 /p; s/.*mean:.*(\(.*\)).*/\1/p; /Colorspace:/p'
You can either find and modify the line using
s/..../...../p or just print the line using
/..../p
"sed" is a very versitile 'stream editor', and you can write some very complex text processing with it. Others that are in some ways simplier, but with a different focus, is "awk" and "perl".
Note however that this is 'shell' specific, and not really ImageMagick, but I help when I can.

Re: Retrieving the individual Image Channel Statistics
Posted: 2012-05-17T11:23:35-07:00
by fmw42
You Sir are amazing! I didn't find the mean.r/g/b references in the -format page! THANK YOU!!
You have to do that via fx escapes. see
http://www.imagemagick.org/Usage/transform/#fx_escapes
and
http://www.imagemagick.org/script/fx.php
Re: Retrieving the individual Image Channel Statistics
Posted: 2012-05-17T12:25:35-07:00
by yazinsai
I knew I probably missed it; thanks again!