Retrieving the individual Image Channel Statistics

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
User avatar
yazinsai
Posts: 13
Joined: 2012-05-14T21:39:07-07:00
Authentication code: 13

Retrieving the individual Image Channel Statistics

Post 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:

Code: Select all

identify -verbose rose.jpg
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?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Retrieving the individual Image Channel Statistics

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
yazinsai
Posts: 13
Joined: 2012-05-14T21:39:07-07:00
Authentication code: 13

Re: Retrieving the individual Image Channel Statistics

Post by yazinsai »

Works! Thanks Anthony
When you learn, teach. When you get, give.
- Maya Angelou
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Retrieving the individual Image Channel Statistics

Post 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"
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
yazinsai
Posts: 13
Joined: 2012-05-14T21:39:07-07:00
Authentication code: 13

Re: Retrieving the individual Image Channel Statistics

Post 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
When you learn, teach. When you get, give.
- Maya Angelou
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Retrieving the individual Image Channel Statistics

Post 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. :-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Retrieving the individual Image Channel Statistics

Post 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!
User avatar
yazinsai
Posts: 13
Joined: 2012-05-14T21:39:07-07:00
Authentication code: 13

Re: Retrieving the individual Image Channel Statistics

Post 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!
When you learn, teach. When you get, give.
- Maya Angelou
User avatar
yazinsai
Posts: 13
Joined: 2012-05-14T21:39:07-07:00
Authentication code: 13

Re: Retrieving the individual Image Channel Statistics

Post 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. :-)
When you learn, teach. When you get, give.
- Maya Angelou
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Retrieving the individual Image Channel Statistics

Post 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
User avatar
yazinsai
Posts: 13
Joined: 2012-05-14T21:39:07-07:00
Authentication code: 13

Re: Retrieving the individual Image Channel Statistics

Post by yazinsai »

I knew I probably missed it; thanks again!
fmw42 wrote:
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
When you learn, teach. When you get, give.
- Maya Angelou
Post Reply