Calculate brightness? What about shape?

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
wobsie

Calculate brightness? What about shape?

Post by wobsie »

Is there an easy way to calculate the brightness of an entire image? I want to compare brightness values of thousands of images.

Also, if I have a black & white image, is there a way to calculate how clustered the black pixels are? I'm subtracting the background out of camera images and thresholding it to generate a binary image. Because the image background jiggles over time, the differenced-and-thresholded image isn't very clean -- there are lots of streaks and dots in addition to the dark ovals (it's the dark ovals that I'm interested in). I need a way to distinguish the images containing only the streaks and dots from the ones with the dark ovals. I thought there might be a way to calculate how clustered the black pixels are. Is there?

Thanks.....
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Calculate brightness? What about shape?

Post by fmw42 »

First it is not clear what you mean by brightness. You can get IM to tell you what the average gray level value is for a grayscale image (in IM 6.3.9.1+) using:

identify -format "%[mean]" <image>

or

convert <image> -format "%[mean]" info:

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

If you are on an earlier IM version, you can extract it from the -verbose info: as follows:

data=`convert <image> -verbose info:`
echo "$data" | sed -n '/^.*Mean:.*[(]\([0-9.]*\).*$/{ s//\1/; p; q; }'

or in one line as

echo "$(convert <image> -verbose info:)" | sed -n '/^.*Mean:.*[(]\([0-9.]*\).*$/{ s//\1/; p; q; }'

With respect to your second issue, can you post some links to pictures to explain your problem?
wobsie

Re: Calculate brightness? What about shape?

Post by wobsie »

fmw42 wrote:With respect to your second issue, can you post some links to pictures to explain your problem?
I'd like to calculate a metric so that I can tell "clustered" from "scattered."

Clustered:
Image

Scattered:
Image

One way that I've seen this done (in GIS) is to calculate the average "patch" size where a "patch" is a continuous blob of a single color. But, for one, I don't know how to do that in ImageMagick, and it seems like a very slow operation.

Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Calculate brightness? What about shape?

Post by fmw42 »

I will try to look at this more over the weekend. But my first thought is to do a fairly large median filter (or a number of iterations of a binary morphologic filter) to remove small specks and fill in holes. What is left are the blobs.

How do you define a patch size? How big is it? What is the minimum number of pixels in a patch? Are patches nearly the same size? There are many factors.

See my answer to another recent question at

viewtopic.php?f=1&t=10889

He has now written something custom. Perhaps if appropriate to your problem he will share with you.

IM does not have blob analysis functions to my knowledge.
Post Reply