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.

HAMMING


Computes the percent hamming distance between two binary string perceptual hashes..

Download Script

last modified: January 29, 2022



USAGE: hamming [-p1 phash1] [-p2 phash2] [-s source] [-m method] [infile1] [infile2]
USAGE: hamming [-h|-help]

-p1 .. phash1 ...... optional first perceptual hash to compare to the second perceptual
.................... hash; binary string of values; number of binary digits must match
.................... that of the second perceptual hash; takes precedence over
.................... perceptual hashes provided in two optional input images
-p2 .. phash2 ...... optional second perceptual hash to compare to the first perceptual
.................... hash; binary string of values; number of binary digits must match
.................... that of the first perceptual hash; takes precedence over
.................... perceptual hashes provided in two optional input images
-s ... source ...... source location in infiles' meta date to retrieve the perceptual
.................... hash values; choices are: the "comment" or "label" fields in
.................... the images' meta data; default=comment
-m ... method ...... hash method identifies which perceptual hash value to retrieve
.................... from the two images' meta data fields; choices are: bmh, bdh,
.................... avh, pfh; default=bmh

PURPOSE: To compute the percent hamming distance between two binary string perceptual hashes.

DESCRIPTION: HAMMING computes the percent hamming distance between two binary string perceptual hashes. The two hashes strings may be privided directly or they may be extracted from two input images in which one or more has hashes have been stored for one of several hash methods. The potential hash methods that may be stored in the images are as follows:

Block Mean Hash (bmh): The preprocess image is scale to 16x16 pixels, which is the same as averaging non-overlapping blocks of 16x16 pixels. The hash is computed by comparing the global mean of the 256x256 image to each of the 16x16 pixel value. If the value is larger than the global mean, the hash is set to 1; otherwise 0. This forms a 256 length binary string for the hash.

Block Difference Hash (bdh): The preprocessed image is scale to 9x8 pixels. Then each pixel is subtracted from its neighbor to the right, thresholded at 0 and divided by 255. This forms a 128 length binary string. The preprocessed image is scale again, but this time to 8x9 pixels. Then each pixel is subtracted from its neighbor below, thresholded at 0 and divided by 255. This forms another 128 length binary string. Finally the two 128 length strings are appended to form a 256 length string for the binary hash.

Annular Variance Hash (avh): The preprocessed image is converted to 256x256 polar format with the columns representing angles and the rows representing radius. Thus each row is an annulus (circular ring) of the preprocessed image. The variance of each row is computed to get the variance as a function of radius as a list of 256 values. Each value is compared to the next one. If the next one is larger, then a value of 1 is assigned; othewise a value of 0 is assigned. This produces a 255 length binary string for the hash.

Polar FFT Hash (pfh): The preprocessed image is converted to 256x256 polar format with the columns representing angles and the rows representing radius. The FFT magnitude is then computed and the center 15x15 pixels are extracted and listed as 225 graylevel values in row first order. Each pixel's graylevel is then compared to the next one. If the next one is larger, then a value of 1 is assigned; othewise a value of 0 is assigned. This produces a 224 length binary string for the hash.

References: https://en.wikipedia.org/wiki/Hamming_distance https://classroom.synonym.com/calculate-hamming-distance-2656.html

ARGUMENTS:

-p1 phash1 ... PHASH1 is the optional first perceptual hash to compare to the second perceptual hash. The hash is a binary string of values. The number of binary digits in the string must match that of the second perceptual hash. Using PHASH1 and PHASH2 takes precedence over perceptual hashes provided in two optional input images. Infiles do not need to be provided if both phash1 and phash2 are provided.

-p2 phash2 ... PHASH21 is the optional second perceptual hash to compare to the first perceptual hash. The hash is a binary string of values. The number of binary digits in the string must match that of the second perceptual hash. Using PHASH1 and PHASH2 takes precedence over perceptual hashes provided in two optional input images. Infiles do not need to be provided if both phash1 and phash2 are provided.

-s source ... SOURCE location in in the two infiles' meta date to retrieve the perceptual hash values. The choices are: the "comment" (c) or "label" (l) fields in the images' meta data. The default=comment.

-m method ... METHOD identifies which perceptual hash value to retrieve from the two images' meta data fields. More than one hash may be stored in the meta data field, but only one may be specified for computing the hamming distance. The choices are: bmh (block mean hash), bdh (block difference hash), avh (annular variance hash), pfh (polar fft hash). The default=bmh.

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.



Example 1 --- Two Different Images

Original Images

  

hamming -s comment -m bmh lena.png barn.jpg
39.8



Example 2 --- Two Similar Images

Original Images

  

hamming -m bmh -s comment lena.png lena_g1p2_r125x_gnoise.png
1.6