Relationship between Imagick means and IM7 ?

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
RoundedRectangle
Posts: 6
Joined: 2018-07-13T11:09:23-07:00
Authentication code: 1152

Relationship between Imagick means and IM7 ?

Post by RoundedRectangle » 2018-07-13T11:49:11-07:00

I'm converting a script from Imagick in PHP to command line IM 7.
One thing has held me up for a while now.

This method in PHP:

Code: Select all

$im->getImageChannelMean(Imagick::CHANNEL_ALL);
Returns the following means for 5 arbitrary images I was testing with:

Code: Select all

22282.14894013
30473.356623976
38031.623559618
31193.137382776
13730.786592171
However using "magick identify -verbose" on those same 5 images, here are the means from the "Image statistics: Overall:" section:

Code: Select all

64.0298
118.573
147.983
90.7432
53.4272
What is the relation between these numbers, and how can I get the same number PHP returns (or close to it)?

Any help you can provide would be much appreciated.

snibgo
Posts: 10927
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Relationship between Imagick means and IM7 ?

Post by snibgo » 2018-07-13T12:36:20-07:00

The relationship between some pairs is a factor of 257.
snibgo's IM pages: im.snibgo.com

RoundedRectangle
Posts: 6
Joined: 2018-07-13T11:09:23-07:00
Authentication code: 1152

Re: Relationship between Imagick means and IM7 ?

Post by RoundedRectangle » 2018-07-13T15:10:32-07:00

snibgo wrote:
2018-07-13T12:36:20-07:00
The relationship between some pairs is a factor of 257.
It would make more sense if it were 255.

I'll take a larger sample then and either go with 257 if it's the most common, or average everything.

Thanks

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

Re: Relationship between Imagick means and IM7 ?

Post by fmw42 » 2018-07-13T16:21:20-07:00

That factor is due to your Imagick computing as -depth 8 and Imagemagick computing as -depth 16. Depth 8 has a range of 0 to 255. Depth 16 has a range of 0 to 65535. The factor is 65535/255=257.

If you want 8-bit depths from Q16 Imagemagick, then do

Code: Select all

magick image -format "%[fx:255*mean]\n" info:
or for each channel:

Code: Select all

magick image -format "%[fx:255*mean.r] %[fx:255*mean.g] %[fx:r255*mean.b]\n" info:
Note that fx reports values in the range of 0 to 1

RoundedRectangle
Posts: 6
Joined: 2018-07-13T11:09:23-07:00
Authentication code: 1152

Re: Relationship between Imagick means and IM7 ?

Post by RoundedRectangle » 2018-07-14T02:12:03-07:00

getQuantumDepth() in PHP returns as Q16.

Also after testing about 100 images, there are only 20 around the 257 factor.

The factors themselves range from 127 to 3526, which leaves me scratching my head.

I'm going to sidestep the whole issue and just start fresh without Imagick.

Thanks

snibgo
Posts: 10927
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Relationship between Imagick means and IM7 ?

Post by snibgo » 2018-07-14T03:02:26-07:00

I suspect that your two methods are often measuring different things. Do your images have transparency? Perhaps one calculation includes the alpha channel but the other doesn't. Perhaps one uses transparency and the other uses opacity.

Incidentally, the overall mean of partially transparent images is reported differently for IM V6 and IM v7. Perhaps this accounts for your problem.

Code: Select all

f:\web\im>%IM%convert toes_holed.png -format %[fx:mean] info:
0.158904

f:\web\im>%IMG7%magick toes_holed.png -format %[fx:mean] info:
0.211775
For fully opaque images, they report the same values.

(Tested with v6.9.5-3 and v7.0-7-28 on Windows 8.1.)
snibgo's IM pages: im.snibgo.com

RoundedRectangle
Posts: 6
Joined: 2018-07-13T11:09:23-07:00
Authentication code: 1152

Re: Relationship between Imagick means and IM7 ?

Post by RoundedRectangle » 2018-07-14T03:45:08-07:00

Interesting.

Yes I've got a mix of grayscale, rgb, alpha and no aplha.

Honestly I need to do better normalization of my image set before processing them.

snibgo
Posts: 10927
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Relationship between Imagick means and IM7 ?

Post by snibgo » 2018-07-14T03:58:03-07:00

In general terms, the "mean" of an image that contains transparency is ambiguous. We might actually want the mean of the colour channels only (eg RGB or gray, but not including alpha), but we might want to ignore the colour of pixels that are fully transparent (so a transparent pixel that happens to be black, white or anything else doesn't affect the mean), and so on.

It all depends on what you want the value to signify.
snibgo's IM pages: im.snibgo.com

Post Reply