perlimagemagickperlmagick

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


Solution

  • 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