Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial (non-profit) use, ONLY.

    For use of my scripts in commercial (for-profit) environments or non-free applications, please contact me (Fred Weinhaus) for licensing arrangements. My email address is fmw at alink dot net.

    If you: 1) redistribute, 2) incorporate any of these scripts into other free applications or 3) reprogram them in another scripting language, then you must contact me for permission, especially if the result might be used in a commercial or for-profit environment.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

    Please read the Pointers For Use on my home page to properly install and customize my scripts.

SIMILAR


Computes the normalized cross correlation similarity metric between two equal dimensioned images

Download Script

last modified: December 06, 2023



USAGE: similar [-m mode] infile1 infile2
USAGE: similar [-h or -help]

-m ..... mode ..... colorspace mode; g (for grayscale) or rgb; default=g

PURPOSE: To compute the normalized cross correlation similarity metric between two equal dimensioned images.

DESCRIPTION: SIMILAR computes the normalized cross correlation similarity metric between two equal dimensioned images. The normalized cross correlation metric measures how similar two images are, not how different they are. The range of ncc metric values is between 0 (dissimilar) and 1 (similar). If mode=g, then the two images will be converted to grayscale. If mode=rgb, then the two images first will be converted to colorspace=rgb. Next, the ncc similarity metric will be computed for each channel. Finally, they will be combined into an rms value. NOTE: this metric does not work for constant color channels as it produces an ncc metric = 0/0 for that channel. Thus it is not advised to run the script with either image having a totally opaque or totally transparent alpha channel that is enabled.

ARGUMENTS:

-m mode ... MODE for colorspace to use when applying the normalized cross correlation metric on the two images. If mode=g, then the two image will be converted to grayscale. If mode=rgb, then the ncc similarity metric will be computed for each channel and then combined as its rms value. Default=g

NOTE: For best accuracy, this script should be run in HDRI compilation of IM.

For reference on the normalized cross correlation metric, see http://en.wikipedia.org/wiki/Cross-correlation#Normalized_cross-correlation

CAVEAT: No guarantee that this script will work on all platforms, nor that trapping of inconsistent parameters is complete and foolproof. Use At Your Own Risk.


EXAMPLES

(provided by user kinder, see http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=15264)

Image 1

Image 2

NCC Metric
(-m g)

NCC Metric
(-m rgb)

1a.jpg

1b.jpg

0.876676

0.876031
red=0.876637
green=0.876892
blue=0.874563

2a.jpg

2b.jpg

0.321169

0.338112
red=0.302048
green=0.334282
blue=0.374141

3a.jpg

3b.jpg

0.358507

0.358502
red=0.390713
green=0.34917
blue=0.333158

4a.jpg

4b.jpg

0.808273

0.806428
red=0.799328
green=0.808636
blue=0.811272;

1a.jpg

1a.jpg

0.9999

0.999894
red=0.999856
green=0.999916
blue=0.999909

1a.jpg

convert 1a.jpg -negate 1an.jpg

1.54359e-07

1.50228e-07
red=1.35298e-07
green=1.53664e-07
blue=1.60583e-07

convert cyclops.png -fuzz 10%
-transparent white cyclopst10.png

convert cyclops.png -fuzz 3%
-transparent white cyclopst3.png

1.0677

0.989118
red=0.999885
green=0.9999
blue=0.999907



What the script does is as follows (for any channel):

  • Computes the mean and standard deviation of both images
  • Subtracts the mean from each image
  • Multiplies pixel by pixel the mean subtracted images
  • Gets the mean of the product (equivalent to the
    pixel-by-pixel sum the product image divided by
    the total number of pixels
  • Computes the normalized cross correlation metric by
    dividing the above mean by the product of the two
    standard deviations

This is equivalent to the following IM commands for the case of mode=g:

  • convert $infile1 -colorspace gray $tmp1
  • convert $infile2 -colorspace gray $tmp2
  • m1=`convert $tmp1 -format "%[fx:mean]" info:`
  • m2=`convert $tmp2 -format "%[fx:mean]" info:`
  • m12=`convert xc: -format "%[fx:$m1*$m2]" info:`
  • s1=`convert $tmp1 -format "%[fx:standard_deviation]" info:`
  • s2=`convert $tmp2 -format "%[fx:standard_deviation]" info:`
  • ncc=`convert $tmp1 $tmp2 -compose mathematics \
    -set option:compose:args "1,-$m1,-$m2,$m12" -composite miff:- |\
    convert - -format "%[fx:mean/($s1*$s2)]" info:`
  • echo "$ncc"