Skip to content

fix: compute the distance between feature sets correctly - #234

Merged
petercorke merged 2 commits into
mainfrom
bug/feature-distance-sizes
Nov 22, 2026
Merged

petercorke merged 2 commits into
mainfrom
bug/feature-distance-sizes

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

features.distance(other), the matrix of descriptor distances between two feature sets, was wrong in two ways. Its loop also wrote D[j, i] = d for every pair, which:

  1. raised IndexError for sets of different sizes, because D[j, i] is out of range when j runs over the second set; and
  2. gave silently wrong answers for two different sets of the same size. The (j, i) iteration runs after (i, j), so it overwrote D[i, j] with the distance from b[i] to a[j], and the upper triangle of the matrix held the transposed values. Only a set compared with itself, which is symmetric, came out right.

The D[j, i] line is removed, so D[i, j] is the distance from the i-th feature of the first set to the j-th of the second, for the L1, L2 and ncc measures.

The first case was found by the new point-feature tests (PR 221), which recorded it as a strict xfail test. The second I found while fixing it, because those tests compared a set with a copy of itself, which hides it.

Testing

The xfail test is replaced by tests that compare distance() with a pair-by-pair calculation for sizes (3, 3), (3, 4), (4, 2), (1, 3) and (3, 1), with different descriptors in each set, for L1, L2 and ncc. Without the fix 11 of the 14 distance tests fail; with it all pass. tests/test_image_point_features_extra.py and tests/test_image_point_features.py: 140 passed and 4 xfail, each of which belongs to a different bug. mypy reports no issues. Not run: CI on Windows or OpenCV 4 (CI will).

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

🤖 Generated with Claude Code

distance() also wrote D[j, i] = d for every pair. For feature sets of different
sizes that index is out of range, so it raised IndexError. For two different
sets of the same size it was silently wrong: the (j, i) iteration, which runs
after (i, j), overwrote D[i, j] with the distance from b[i] to a[j], so the
upper triangle of the matrix held the transposed values. Only a set compared
with itself, which is symmetric, came out right.

Remove the D[j, i] line, so D[i, j] is the distance from the i-th feature of
the first set to the j-th of the second.

The xfail test that recorded the IndexError is replaced by tests of every pair
for several sizes and the L1, L2 and ncc measures, against a pair-by-pair
calculation. They fail without the fix.

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

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 2 high

Alerts:
⚠ 2 issues (≤ 0 issues of at least minor severity)

Results:
2 new issues

Category Results
ErrorProne 2 high

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
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 b3eaf64 into main Nov 22, 2026
33 of 34 checks passed
@petercorke
petercorke deleted the bug/feature-distance-sizes branch November 22, 2026 20:57
@github-actions github-actions Bot mentioned this pull request Nov 22, 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