Histogram.compare#
- Histogram.compare(other: Histogram, method: str = 'correlation') float | list[float][source][source]#
Compare with another histogram
- Parameters:
other – histogram to compare with
method – comparison method, see below, case insensitive and can be abbreviated to any unique prefix, defaults to “correlation”
- Raises:
ValueError – the histograms have a different number of bins or planes
ValueError – unknown or ambiguous method
- Returns:
comparison metric, a list if the histograms have more than one plane
- Return type:
Compares each plane of this histogram with the same plane of
other, using the histogram counts. Both are first normalized to sum to one, seepdf, so the histograms can come from images of different sizes. The histograms must have the same number of bins.The methods are the six of OpenCV’s
compareHistand the earth mover’s distance. For a pair of histograms \(H_1\) (this one) and \(H_2\) (other):"correlation": Pearson correlation coefficient, from -1 to 1. A larger value means more similar."chisqr": chi-squared distance, normalized by \(H_1\). A smaller value means more similar."intersection": sum of the smaller of each pair of bins, from 0 to 1. A larger value means more similar."bhattacharyya": Bhattacharyya distance, from 0 to 1, also called"hellinger". A smaller value means more similar."chisqr_alt": alternative chi-squared distance, which is symmetric. A smaller value means more similar."kl_div": Kullback-Leibler divergence of \(H_2\) from \(H_1\). A smaller value means more similar."emd": earth mover’s distance (L1), in units of bins. A smaller value means more similar.
Note
h1.compare(h2)is not always the same ash2.compare(h1)."chisqr"and"kl_div"are not symmetric in the two histograms. The other methods are.Note
The earth mover’s distance is the minimum amount of “work” needed to turn one histogram into the other, where moving a fraction of the pixels by one bin costs that fraction. For 1D histograms it is the sum of the absolute differences of the two cumulative distributions.
Examples
>>> from machinevisiontoolbox import Image >>> h1 = Image.Read("flowers1.png", mono=True).hist() >>> h2 = Image.Read("flowers2.png", mono=True).hist() >>> h1.compare(h2, "correlation") 0.8026170861552143 >>> h1.compare(h2, "kl") # abbreviation of "kl_div" 0.09382081089976703 >>> h2.compare(h1, "kl") # not the same 0.11417981476582818
See also
Added in version 2.5.0.