Fred's ImageMagick Scripts


    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:

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


Compares two images' shapes using metrics computed from Hu image moments.

Download Script

last modified: December 21, 2023

USAGE: shapecompare [-m metric] [-t type] [-T thresh] [-e edgemode] [-i invert] infile1 infile2
USAGE: shapecompare [-h or -help]

-m ... metric ..... compare difference metric; choices are: sad, said, sand, rssd, all;
................... default=sad
-t ... type ....... type of image to convert input images for compare processing;
................... choices are: grayscale (g), binary (b) or outline (o);
................... default=grayscale
-T ... thresh ..... threshold for converting to binary; 0<=thresh<=100 (percent);
................... default=50
-e ... edgemode ... mode of morphologic edge processing; choices are: edge, edgin or
................... edgeout; default=edge
-i ... invert ..... invert (negate) colors in input image; choices are: yes (y) or
................... no (n); default=no

PURPOSE: To compares two images' shapes using metrics computed from Hu image moments.

DESCRIPTION: SHAPECOMPARE compares two images' shapes using metrics computed from Hu Image Moments. Several metrics are available. The image will first be converted to grayscale and can then be converted to binary or an edge outline. The script is designed to work with primarily binary or outline shapes on black backgrounds. However, grayscale shapes on black background or antialiased black/white shapes on black backgrounds should work, also. My analysis (see chart) indicates that outline mode does not work as well as the other two modes.


-m metric ... METRIC is the compare metric to compute the difference between the two images. Smaller is better than larger. The choices are: sad (sum absolute difference), said (sum absolute inverse difference), sand (sum absolute normalized difference), rssd (root sum squared difference) or all (report all of them). The default=sad.

The equations for the metrics are:
SAD = sum( abs(Hu1 - Hu2) )
SAID = sum( abs(1/Hu1 - 1/Hu2) )
SAND = sum( abs(Hu1 - Hu2) / sqrt( abs(Hu1*Hu2) ) )
RSSD = sqrt( sum( (Hu1 - Hu2)^2 ) )
where there are 7 different (log) Hu image moments that are summed

-t type ... TYPE of image to convert the input images for processing. The choices are: grayscale (g), binary (b) or outline (o). The default=grayscale.

-T thresh ... THRESH is the threshold for converting to binary. Values are: 0<=thresh<=100 (percent). The default=50.

-e edgemode. EDGEMODE is the mode of morphologic edge processing. The choices are: edge, edgin or edgeout. Edge will be 2 pixels thick. The other two will be only 1 pixel thick. All 3 use a diamond shaped kernel. The edgein will be on the white side and the edgeout will be on the black side. The default=edge.

-i invert ... INVERT (negate) the colors in input image as preprocessing. Choices are: yes (y) or no (n). The default=no


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.


(source images)

Reference Image:


Test Image:


Compare Results:

What the script does is as follows:

  • Convert images to grayscale and optionally binary or outline
  • Computes the 7 Hu image moments for each image
  • Convert the moments to log format
  • Compute the comparison metric scores between the reference and test images.