From 573298455499beb89eb7bd3dec011778d2d0b501 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Mon, 23 Nov 2026 08:00:40 +1000 Subject: [PATCH 1/2] fix: treat any non-zero pixel as foreground in distance_transform distance_transform() inverted the image as 255 - x, so it only worked on images whose pixels were 0 or 255. For any other values there was no zero pixel left and the result was about 65000 everywhere, including for the example in its own docstring, which uses the value 1. Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/ImageSpatial.py | 20 ++++++++++++++------ tests/test_image_spatial_extra.py | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/machinevisiontoolbox/ImageSpatial.py b/src/machinevisiontoolbox/ImageSpatial.py index 23a98018..7fe48873 100644 --- a/src/machinevisiontoolbox/ImageSpatial.py +++ b/src/machinevisiontoolbox/ImageSpatial.py @@ -1048,7 +1048,8 @@ def distance_transform( :rtype: :class:`Image` Compute the distance transform. For each zero input pixel, compute its - distance to the nearest non-zero input pixel. + distance to the nearest non-zero input pixel. Any non-zero value counts + as foreground, so the input need not be binary or have the values 0 and 255. Example: @@ -1070,6 +1071,12 @@ def distance_transform( to zero. - The signed-distance function is ``image.distance_transform() - image.distance_transform(invert=True)`` + .. versionchanged:: 2.5.0 + Any non-zero pixel is treated as foreground. Previously the image + was inverted as ``255 - x``, which only worked for images whose + pixels were 0 or 255. For any other values there was no zero pixel + left, and the result was about 65000 everywhere. + :references: - |RVC3|, Section 11.6.4. @@ -1078,15 +1085,16 @@ def distance_transform( :seealso: `opencv.distanceTransform `_ """ self._opencv_type_check(self._A, "single-channel", "CV_8U") - # OpenCV does distance to nearest zero pixel + # OpenCV does distance of each non-zero pixel to the nearest zero pixel # this function does distance to nearest non-zero pixel by default, - # and the OpenCV thing if invert=True + # and the OpenCV thing if invert=True. Any non-zero value is + # foreground, so make a 0/1 image rather than inverting as 255 - x. if invert: # distance to nearest zero pixel - im = self.array_as("uint8") + im = (self._A != 0).astype(np.uint8) else: - # distance to nearest non-zero pixel, invert the image - im = self.invert().array_as("uint8") + # distance to nearest non-zero pixel + im = (self._A == 0).astype(np.uint8) normdict = { "L1": cv2.DIST_L1, diff --git a/tests/test_image_spatial_extra.py b/tests/test_image_spatial_extra.py index d7ff5395..2138e81c 100644 --- a/tests/test_image_spatial_extra.py +++ b/tests/test_image_spatial_extra.py @@ -402,6 +402,27 @@ def test_distance_to_the_nearest_set_pixel(self): assert d.array[20, 25] == pytest.approx(5, abs=0.3) assert d.array[10, 20] == pytest.approx(10, abs=0.6) + @pytest.mark.parametrize("value", [1, 7, 128, 254, 255]) + def test_any_non_zero_value_is_foreground(self, value): + reference = np.zeros((41, 41), dtype=np.uint8) + reference[20, 20] = 255 + arr = np.zeros((41, 41), dtype=np.uint8) + arr[20, 20] = value + for invert in (False, True): + nt.assert_array_equal( + Image(arr).distance_transform(invert=invert).array, + Image(reference).distance_transform(invert=invert).array, + ) + + def test_foreground_pixels_have_different_values(self): + arr = np.zeros((21, 21), dtype=np.uint8) + arr[5, 5] = 3 + arr[15, 15] = 200 + d = Image(arr).distance_transform(norm="L1") + assert d.array[5, 5] == 0 and d.array[15, 15] == 0 + assert d.array[5, 10] == pytest.approx(5) + assert d.array[15, 10] == pytest.approx(5) + def test_distance_to_the_nearest_zero_pixel(self, binary): d = binary.distance_transform(invert=True) assert d.array[0, 0] == 0 From 1f3776a7c9da3ff68b3ae3d460776ba219b5e144 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Mon, 23 Nov 2026 09:28:06 +1000 Subject: [PATCH 2/2] test: correct test_distance_transform for non-zero foreground The test used an image of ones with one zero pixel and expected a non-zero corner pixel to have a positive distance, which only held because the old code mishandled values other than 0 and 255. A non-zero pixel is foreground, so its distance is 0, and the zero pixel is one pixel from the foreground. Co-Authored-By: Claude Sonnet 5.5 --- tests/test_image_morph.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/test_image_morph.py b/tests/test_image_morph.py index 691bd5b2..be12fe1d 100644 --- a/tests/test_image_morph.py +++ b/tests/test_image_morph.py @@ -322,7 +322,14 @@ def test_distance_transform(self): img = Image(array) dist = img.distance_transform() self.assertEqual(dist.shape, img.shape) - self.assertGreater(dist.array[0, 0], 0) + # non-zero pixels are foreground, so their distance is zero + self.assertEqual(dist.array[0, 0], 0) + # the zero pixel is one pixel from the nearest non-zero pixel + self.assertAlmostEqual(float(dist.array[5, 5]), 1, delta=0.1) + # inverted, the distance is to the nearest zero pixel + inv = img.distance_transform(invert=True) + self.assertEqual(inv.array[5, 5], 0) + self.assertGreater(inv.array[0, 0], 0) def test_erode_bool(self): # bool image: erode should shrink the True region