From ba883ec716ef28e290252e46734e94321e60d870 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Sun, 22 Nov 2026 21:19:39 +1000 Subject: [PATCH] fix: compute the distance between feature sets correctly 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 --- .../ImagePointFeatures.py | 1 - tests/test_image_point_features_extra.py | 38 ++++++++++++++----- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/machinevisiontoolbox/ImagePointFeatures.py b/src/machinevisiontoolbox/ImagePointFeatures.py index b50c3dd0..5161e38d 100644 --- a/src/machinevisiontoolbox/ImagePointFeatures.py +++ b/src/machinevisiontoolbox/ImagePointFeatures.py @@ -664,7 +664,6 @@ def distance(self, other: "BaseFeature2D", metric: str = "L2") -> np.ndarray: else: d = np.linalg.norm(des1[i, :] - des2[j, :], ord=metric_dict[metric]) D[i, j] = d - D[j, i] = d return D def match( diff --git a/tests/test_image_point_features_extra.py b/tests/test_image_point_features_extra.py index 50f16a71..4e4e82db 100644 --- a/tests/test_image_point_features_extra.py +++ b/tests/test_image_point_features_extra.py @@ -329,15 +329,35 @@ def test_needs_descriptors(self): with pytest.raises(ValueError, match="no descriptors"): make_features(descriptors=False).distance(make_features()) - @pytest.mark.xfail( - strict=True, - raises=IndexError, - reason="distance() also writes D[j, i], which is out of range unless " - "both feature sets are the same size", - ) - def test_different_sizes(self): - D = make_features(3).distance(make_features(4)) - assert D.shape == (3, 4) + @staticmethod + def by_hand(a, b, ord): + """Distance from every descriptor of a to every descriptor of b.""" + return np.array([[np.linalg.norm(x - y, ord=ord) for y in b] for x in a]) + + @pytest.mark.parametrize("sizes", [(3, 3), (3, 4), (4, 2), (1, 3), (3, 1)]) + @pytest.mark.parametrize("metric, ord", [("L1", 1), ("L2", 2)]) + def test_every_pair_of_different_sets(self, sizes, metric, ord): + # different descriptors in each set, so the matrix is not symmetric and + # D[i, j] must be the distance from a[i] to b[j] + rng = np.random.default_rng(0) + a = make_features(sizes[0]) + b = make_features(sizes[1]) + a._descriptor = rng.normal(size=(sizes[0], 5)).astype(np.float32) + b._descriptor = rng.normal(size=(sizes[1], 5)).astype(np.float32) + D = a.distance(b, metric) + assert D.shape == sizes + nt.assert_allclose( + D, self.by_hand(a._descriptor, b._descriptor, ord), rtol=1e-5 + ) + + def test_ncc_of_different_sets(self): + rng = np.random.default_rng(1) + a, b = make_features(3), make_features(4) + a._descriptor = rng.normal(size=(3, 5)).astype(np.float32) + b._descriptor = rng.normal(size=(4, 5)).astype(np.float32) + nt.assert_allclose( + a.distance(b, "ncc"), a._descriptor @ b._descriptor.T, rtol=1e-5 + ) class TestMatch: