Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial 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

SIMILAR


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

Download Script

last modified: September 26, 2017



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

0.876972

2a.jpg

2b.jpg

0.30973

0.331926

3a.jpg

3b.jpg

0.240242

0.240434

4a.jpg

4b.jpg

0.807486

0.806117

1a.jpg

1a.jpg

1.0042

1.00107

1a.jpg

convert 1a.jpg -negate 1an.jpg

0

0

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

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

0.977871

0.987846



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"