Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/machinevisiontoolbox/ImagePointFeatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,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(
Expand Down
38 changes: 29 additions & 9 deletions tests/test_image_point_features_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,35 @@
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):

Check warning on line 322 in tests/test_image_point_features_extra.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/test_image_point_features_extra.py#L322

Redefining built-in '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):

Check warning on line 328 in tests/test_image_point_features_extra.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/test_image_point_features_extra.py#L328

Redefining built-in '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 TestMatchNeedsDescriptors:
Expand Down
Loading