Skip to content

feat: add Histogram.compare with seven comparison methods - #203

Merged
petercorke merged 3 commits into
mainfrom
feat/histogram-compare
Nov 21, 2026
Merged

petercorke merged 3 commits into
mainfrom
feat/histogram-compare

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

Adds Histogram.compare(other, method="correlation"), which compares each plane of one histogram with the same plane of another and returns a float (one plane) or a list of floats (several planes).

  • Seven methods: OpenCV's six compareHist methods (correlation, chisqr, intersection, bhattacharyya, chisqr_alt, kl_div) and the earth mover's distance (emd). hellinger is accepted as another name for bhattacharyya, which is the same calculation in OpenCV.
  • The method name is case insensitive and can be any unique prefix, so "corr" works. An exact name wins, so "chisqr" is not ambiguous with "chisqr_alt", but "chi" raises a ValueError that lists the choices.
  • Both histograms are normalized with Histogram.pdf first, so images of different sizes can be compared.
  • The EMD is the sum of absolute differences of the two cumulative distributions, in units of bins. For 1D histograms this is exact, needs no OpenCV, and is easy to read. cv2.EMDL1 does not exist in OpenCV 5.0, and cv2.EMD needs signatures, so this is simpler.
  • h1.compare(h2) is not always the same as h2.compare(h1): chisqr and kl_div are asymmetric. The docstring says so and says which methods mean "more similar" with a larger number and which with a smaller one.
  • A different number of bins or planes, an empty histogram, or an object that is not a Histogram raises an exception.

Testing

11 new tests in tests/test_image_whole_features.py cover identical histograms, the EMD of a known 3-bin shift, asymmetry, image-size independence, abbreviations, bad names, color images and mismatches. I broke the code two ways to check they fail (no normalization, and no absolute value in the EMD), and they did. mypy passes with zero errors and all 51 tests in that file pass, on OpenCV 5.0 only. Not run: OpenCV 4, the docs build, and the full suite (left to CI).

The docstring has .. versionadded:: 2.5.0, assuming this ships as the next minor release.

This change was written with AI assistance (Claude Code) and the diff has been reviewed by the maintainer.

🤖 Generated with Claude Code

Compares each plane with the same plane of another histogram using OpenCV's six
compareHist methods and the earth mover's distance, after normalizing both to
sum to one. The method name is case insensitive and can be any unique prefix.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 38 complexity · 0 duplication

Metric Results
Complexity 38
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@petercorke
petercorke merged commit 9dae5d0 into main Nov 21, 2026
34 checks passed
@petercorke
petercorke deleted the feat/histogram-compare branch November 21, 2026 21:47
@github-actions github-actions Bot mentioned this pull request Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant