From 53785e89cfca04eed1935d20ebee41a9a4079c3a Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Sun, 22 Nov 2026 21:23:26 +1000 Subject: [PATCH] fix: do not change the template in similarity with a z metric similarity(T, "zncc") and "zssd" removed the template's mean with T_im -= np.mean(T_im), which changes the caller's template in place, so a second call used a different template. On a uint8 image or template the same line raised UFuncTypeError, because a float mean cannot be subtracted in place from uint8. The offsets are now removed into new float32 arrays, which also is the type cv2.matchTemplate needs for the result to be meaningful. The two xfail tests that recorded the bugs are now ordinary tests, with a z-SSD test on uint8 images. Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/ImageSpatial.py | 7 +++++-- tests/test_image_spatial_extra.py | 26 ++++++++++++++---------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/machinevisiontoolbox/ImageSpatial.py b/src/machinevisiontoolbox/ImageSpatial.py index f3adea6b..cc9d4814 100644 --- a/src/machinevisiontoolbox/ImageSpatial.py +++ b/src/machinevisiontoolbox/ImageSpatial.py @@ -1546,8 +1546,11 @@ def similarity(self, T: Any, metric: str = "zncc") -> Any: im = self._A T_im = T._A if metric[0] == "z": - T_im -= np.mean(T_im) # remove offset from template - im = im - np.mean(im) # remove offset from image + # remove the offset from the template and from the image, into new + # float32 arrays: the template belongs to the caller, and an 8-bit + # image cannot be changed in place by a float mean + T_im = T_im.astype(np.float32) - np.mean(T_im, dtype=np.float32) + im = im.astype(np.float32) - np.mean(im, dtype=np.float32) try: out = cv2.matchTemplate(image=im, templ=T_im, method=metricdict[metric]) diff --git a/tests/test_image_spatial_extra.py b/tests/test_image_spatial_extra.py index 73712110..6a3fb254 100644 --- a/tests/test_image_spatial_extra.py +++ b/tests/test_image_spatial_extra.py @@ -505,25 +505,29 @@ def test_template_is_unchanged_by_ncc(self, scene): image.similarity(template, "ncc") nt.assert_array_equal(template.array, before) - @pytest.mark.xfail( - strict=True, - reason="the z metrics subtract the template mean in place, which " - "changes the caller's template", - ) def test_template_is_unchanged_by_zncc(self, scene): image, template = scene before = template.array.copy() image.similarity(template, "zncc") nt.assert_array_equal(template.array, before) - @pytest.mark.xfail( - strict=True, - raises=Exception, - reason="the in-place mean subtraction fails on a uint8 image", - ) def test_zncc_on_uint8_images(self): rng = np.random.default_rng(1) arr = rng.integers(0, 255, (20, 20), dtype=np.uint8) image = Image(arr) - out = image.similarity(Image(arr[5:8, 5:8].copy()), "zncc") + template = Image(arr[5:8, 5:8].copy()) + out = image.similarity(template, "zncc") assert out.shape == (18, 18) + v, u = np.unravel_index(np.argmax(out.array), out.shape) + assert (u, v) == (5, 5) + assert out.array[v, u] == pytest.approx(1.0, abs=1e-3) + # neither the image nor the template is changed + nt.assert_array_equal(image.array, arr) + nt.assert_array_equal(template.array, arr[5:8, 5:8]) + + def test_zssd_on_uint8_images(self): + rng = np.random.default_rng(2) + arr = rng.integers(0, 255, (20, 20), dtype=np.uint8) + out = Image(arr).similarity(Image(arr[5:8, 5:8].copy()), "zssd") + v, u = np.unravel_index(np.argmin(out.array), out.shape) + assert (u, v) == (5, 5)