Statistics()

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
User avatar
rmabry
Posts: 148
Joined: 2004-04-13T11:25:27-07:00

Statistics()

Post by rmabry » 2008-11-18T07:00:03-07:00

What's the proper way to grab different chunks of the statistics array returned by $image->Statistics()? At the moment I only need the means of the RGB in the normalized range [0,1], and I kludged it like so:

my @stats = $image->Statistics();
my $maxQ = -1+ (1<<$image->Get('depth'));
my ($rMean, $gMean, $bMean) = @stats[3,8,13];
printf("\n(* means: %g, %g, %g *)\n", $rMean/$maxQ, $gMean/$maxQ, $bMean/$maxQ);


It's the bold-faced line that is the worst form, obviously. What should I use? It's clear which array elements to use, as the the doc says,

"The returned values are an array of depth, minima, maxima, mean, and standard deviation values in RGB, CMYK, RGBA, or CMYKA order (depending on the image type)."

As for the normalization, I'll take suggestions there, too. I gather that Statistics() returns different depths for different channels, but the values of the data in the channels seem to be given relative to the maximum depth of the channels, which I presume is the return of $image->Depth(). Or is it relative to the QuantumDepth?

That's a lot of gathering, seeming and presuming, and I don't feel too secure with it.

By the way, the method for getting the QuantumDepth ought to be in the PerlMagick doc; I found it on this forum (magick's post): "Image::Magick->QuantumDepth returns the PerlMagick quantum depth."

Rick

User avatar
magick
Site Admin
Posts: 10849
Joined: 2003-05-31T11:32:55-07:00

Re: Statistics()

Post by magick » 2008-11-18T08:14:31-07:00

We documented the PerlMagick constants (available tomorrow).

You found a bug in Statistics() and we have a patch for the next point release of ImageMagick. The values returned should be normalized so they return consistent results no matter what the depth of the image or the QuantumDepth of the ImageMagick release.

User avatar
rmabry
Posts: 148
Joined: 2004-04-13T11:25:27-07:00

Re: Statistics()

Post by rmabry » 2008-11-19T19:29:57-07:00

What to make of this? The code I quoted,


Code: Select all

use Image::Magick;
my $quantumDepth = Image::Magick->QuantumDepth;
print "QuantumDepth = $quantumDepth\n";
[/size]
gives

Argument "Image::Magick" isn't numeric in subroutine entry at F:/Perl/site/lib/Image/Magick.pm line 43.

but does also give

QuantumDepth = 16

Rick

User avatar
magick
Site Admin
Posts: 10849
Joined: 2003-05-31T11:32:55-07:00

Re: Statistics()

Post by magick » 2008-11-19T19:45:33-07:00

We cannot reproduce the problem. We're using Perl 5.10.0. Try adding 0 to Image::Magick->QuantumDepth so it switches to a numeric context.

User avatar
rmabry
Posts: 148
Joined: 2004-04-13T11:25:27-07:00

Re: Statistics()

Post by rmabry » 2008-11-19T20:40:05-07:00

magick wrote:We cannot reproduce the problem. We're using Perl 5.10.0.
I shoulda mentioned it. I'm so:

This is perl, v5.8.8 built for MSWin32-x86-multi-thread

Version: ImageMagick 6.4.5 2008-10-24 Q16 http://www.imagemagick.org
Try adding 0 to Image::Magick->QuantumDepth so it switches to a numeric context.
Same results with this

my $quantumDepth = 0+Image::Magick->QuantumDepth;

and this

my $quantumDepth = 0.+Image::Magick->QuantumDepth;

User avatar
rmabry
Posts: 148
Joined: 2004-04-13T11:25:27-07:00

Re: Statistics()

Post by rmabry » 2018-01-18T03:49:58-07:00

rmabry wrote:
2008-11-18T07:00:03-07:00
What's the proper way to grab different chunks of the statistics array returned by $image->Statistics()?
It slipped my mind at the time (one short decade ago) to follow up on this, as we got a bit sidetracked (as we will again). In returning to the original perl script that got this started, I found that my implementation no longer worked, because the array elements have moved or expanded. Had I followed up properly at the time this might have been averted. Namely, I meant to suggest that the elements should be named, rather than (or rather, in addition to) being deposited as just an array. The docs presently say this:
Statistics() returns the image statistics for each channel in the image. The returned values are an array of depth, minima, maxima, mean, standard deviation, kurtosis, skewness, and entropy values in RGB, CMYK, RGBA, or CMYKA order (depending on the image type).

@statistics = $image->Statistics();
Since the previous post, the elements kurtosis, skewness, and entropy have been added, which accounts for the present failure of my script.

The current (and former) usage is a pain to resolve if you just want the means or whatever, not to mention the obvious possibility of more broken code down the road.

How hard would it be to allow for the bits to be picked off with identifiers that could have some longevity? For instance, perhaps, along the lines of such:

$redMean = $image->Statistics('mean-red')

@rgbMeans = $image->Statistics('mean[rgb]')

or even

($alphaMax,$redMax,$blueMax) = $image->Statistics('max[arb]')

Cheers all, and belated greetings to Anthony, Fred, and The Wizard.

User avatar
rmabry
Posts: 148
Joined: 2004-04-13T11:25:27-07:00

Re: Statistics()

Post by rmabry » 2018-01-18T04:16:57-07:00

And for the side(tracked) issue ...
magick wrote:We cannot reproduce the problem. We're using Perl 5.10.0.
The problem was with this, 10 years ago, but it is the same today:

Code: Select all

Image::Magick->QuantumDepth
returns the correct value, but gives a (strict) warning. On my system (Centos 6.9; ImageMagick 7.0.7-15 Q16 x86_64 2018-01-17; Perl v5.10.1) I get
Argument "Image::Magick" isn't numeric in subroutine entry at /usr/local/lib64/perl5/Image/Magick/Q16HDRI.pm line 56.
(The module was named Magick.pm at the time.)

I wonder if the problem was not reproduced due to the non-strict invocation.

At the the end of the Perlmagick installation instructions is this:
Finally, verify the PerlMagick install worked properly, type

perl -MImage::Magick -le 'print Image::Magick->QuantumDepth'
If I do that, no error message appears, probably because it isn't strictly invoked. I get the warning this way:

Code: Select all

perl -w -MImage::Magick -le 'print Image::Magick->QuantumDepth'
Apologies if this belongs in the Bugs forum.

Rick

User avatar
magick
Site Admin
Posts: 10849
Joined: 2003-05-31T11:32:55-07:00

Re: Statistics()

Post by magick » 2018-01-18T17:42:16-07:00

Thanks for the problem report. We can reproduce it and will have a patch to fix it in GIT master branch @ https://github.com/ImageMagick/ImageMagick later today. The patch will be available in the beta releases of ImageMagick @ https://www.imagemagick.org/download/beta/ by sometime tomorrow.

User avatar
rmabry
Posts: 148
Joined: 2004-04-13T11:25:27-07:00

Re: Statistics()

Post by rmabry » 2018-01-19T02:53:51-07:00

magick wrote:
2018-01-18T17:42:16-07:00
Thanks for the problem report. We can reproduce it and will have a patch to fix it [...]
Excellent. Any thoughts on the main part of the thread (safer access to Statistics array elements)?

Rick

Post Reply