clt60
clt60

Reputation: 63902

ImageMagick command to Image::Magick code

I found the following ImageMagick command in some forum and works nicely for compare images.

convert image1 image2 -compose Difference -composite  -format '%[fx:mean*100]' info:

The result is one floating point number and low values (like 0.5 and such) mean: the images are similar.

Using the attached images, it produces the number: 0.0419167. (the images are very similar)

I want to use Image::Magick (perlmagick). The problem is i don't know how to achieve the same result with perlmagick. The following works, and creates the composite, (black image using the attached images)

#!/usr/bin/env perl
use 5.014;
use strict;
use warnings;
use Data::Dumper;
use Image::Magick;

my $i1 = Image::Magick->new;
$i1->Read('s1.jpg');
my $i2 = Image::Magick->new;
$i2->Read('s2.jpg');

$i1->Composite(image => $i2, compose=>'Difference');
$i1->Display();

The question is, how to convert the result to an number, e.g. how to achieve the

    ... -format '%[fx:mean*100]' info:

part of the above command in PerlMagick for getting only the above "number"?

Is someone want test, attaching two images:

enter image description here enter image description here

Upvotes: 1

Views: 255

Answers (2)

bytepusher
bytepusher

Reputation: 1578

I am guessing you want to call

my $format = $iI->Fx( expression=>'mean*100' );

This should do the same thing as what you had on command line.

see here for more detailed documentation of fx in PerlMagick

( there is an example fx line on the page )

On the same page: search for @statistics.

Seems to me the mean is accessible via

my @stats = $i1->Statistics;
my $mean = $stats[3]; # hash would be nice, mean is 4th according to docs
print "$mean\n"; # outputs something like .0413 for me

Not sure if this is what you need, but that is how I found the 'mean', whether this is precisely what fx mean does I am not certain and honestly not willing to understand the entire doc on the fx method ;)

BTW, the script I still had was based on Randall Schwartz's post

Upvotes: 1

clt60
clt60

Reputation: 63902

Just found the answer.

Image::Magick has the Statistics method, so calling:

my @stat = $i1->Statistics();
say Dumper \@stat

prints image stats for each channel, like:

$VAR1 = [
      '8',
      '0',
      '0.168627450980392',
      '0.0418661437908497',    <--- mean RED
      '0.0236850206077085',
      '-0.250788990333716',
      '0.312238727773058',
      '8',
      '0',
      '0.133333333333333',
      '0.0385273202614379',     <- mean GREEN
      '0.0193189321997568',
      '-0.435325792918546',
      '0.0686645009183608',
      '8',
      '0',
      '0.23921568627451',
      '0.0453563725490196',     <- mean BLUE
      '0.0301331898766906',
      '0.309072091600589',
      '0.66336367830764'
    ];

e.g. averaging the 3 numbers, got the wanted number: 0.0419166122004357

use List::Util qw(sum)
my $s = sum @stat[3,10,17];
say $s/3;

From the docs: misc methods

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).

Upvotes: 0

Related Questions