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.

HISTCOMPARE


Computes one of several metrics characterizing the difference between the histograms of two images.

Download Script

last modified: April 30, 2021



USAGE: histcompare [-p processing] [-m metric] [-n normalization] [-c] infile1 infile2
USAGE: cumhist [-h or -help]

-p ... processing ...... processing of images; choices are:
........................ lab (convert to LAB colorspace first),
........................ gray (convert to grayscale first),
........................ rec601luma (convert to rec601luma first),
........................ rec709luma (convert to rec709luma first),
........................ global (append all channels into 1 grayscale image),
........................ and all (process each channel of input separately);
........................ default=gray
-m ... metric .......... difference metrics; choices are: correlation (co),
........................ chisquare (ch), intersection (i), bhattacharyya (b),
........................ earthmover (e), jeffrey (j) or all (a); default=correlation
-n ... normalization ... normalization of count values; comma separate pair
........................ minval,maxval; counts are scaled so that
........................ minimum histogram count becomes minval and
........................ maximum histogram count becomes maxval;
........................ default=no scaling
-c .................... divide the histogram counts by the cumulative count

PURPOSE: To compute one of several metrics characterizing the difference between the histograms of two images.

DESCRIPTION: HISTCOMPARE computes one of several metrics characterizing the difference between the histograms of two images. The user may choose either to process the images to grayscale, to append all the channels into one grayscale image or process each channel separately. The choice of metrics are: correlation, chisquare, intersection, bhattacharyya, earthmover, jeffrey or all. See the references below for the metric formulae.

ARGUMENTS:

-p processing ... PROCESSING of the images. The choices are:
lab (convert to LAB colorspace first),
gray (convert to grayscale first),
rec601luma (convert to rec601luma first),
rec709luma (convert to rec709luma first),
global (append all channels into one grayscale image),
and all (process each channel of input separately).
The default=gray

-m metric ... METRIC is the desired difference metric. The choices are: correlation (co), chisquare (ch), intersection (i), bhattacharyya (b), earthmover (e), jeffrey (j) or all (a). The default=correlation.

-n normalization ... NORMALIZATION of histogram counts. Values are specified as a comma separate pair, minval,maxval. The counts are scaled so that the minimum histogram count becomes minval and the maximum histogram count becomes maxval. The default=no scaling.

-c ... divide the histogram counts by the CUMULATIVE count

REFERENCES:
http://siri.lmao.sk/fiit/DSO/Prednasky/7%20a%20Histogram%20based%20methods/7%20a%20Histogram%20based%20methods.pdf
(for correlation, chisquare, intersection and bhattacharyya)
http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_comparison/histogram_comparison.html
(for correlation, intersection and bhattacharyya).
http://www.cs.umd.edu/~djacobs/CMSC426/Histograms.pdf and
http://www-prima.inrialpes.fr/jlc/papers/eccv96.pdf
(for symmetric chisquare)
http://en.wikipedia.org/wiki/Earth_Mover%27s_Distance
(for earthmover).
http://robotics.stanford.edu/~rubner/papers/rubnerIjcv00.pdf
(for jeffrey)

NOTE: This script will process only the first frame/page of a multiframe or multipage image.

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


Example 1 - Global Histogram

Input Images
(source)

Arguments:

Metric Score

-p global -n 0,1 -m correlation

global correlation = 0.766422

-p global -n 0,1 -m chisquare

global chisquare = 6.79822

-p global -n 0,1 -m intersection

global intersection = 53.3874

-p global -n 0,1 -m bhattacharyya

global bhattacharyya = 0.26314

-p global -n 0,1 -m earthmover

global earthmover = 1613.9

-p global -n 0,1 -m jeffrey

global jeffrey = 7.19684



Example 2 - All (sRGB) Histograms

Input Images

Arguments:

Metric Score

-p all -n 0,1 -m correlation

red correlation = 0.871993
green correlation = 0.462451
blue correlation = 0.513214

-p all -n 0,1 -m chisquare

red chisquare = 4.43458
green chisquare = 10.9668
blue chisquare1 = 10.5793

-p all -n 0,1 -m intersection

red intersection = 34.9194
green intersection = 30.524
blue intersection = 28.1282

-p all -n 0,1 -m bhattacharyya

red bhattacharyya = 0.30614
green bhattacharyya = 0.358233
blue bhattacharyya = 0.324998

-p all -n 0,1 -m earthmover

red earthmover = -845.67
green earthmover = 2702.64
blue earthmover = 4299.23

-p all -n 0,1 -m jeffrey

red jeffrey = 4.97448
green jeffrey = 11.9008
blue jeffrey = 11.3759



What the script does is as follows:

  • Generates the a zero filled and sorted textual histogram for each image
  • Computes the desired metric to get a measure of the histograms' difference

See the script for actual code details.