diff --git a/src/machinevisiontoolbox/ImageSpatial.py b/src/machinevisiontoolbox/ImageSpatial.py index b996f6f1..90a66de4 100644 --- a/src/machinevisiontoolbox/ImageSpatial.py +++ b/src/machinevisiontoolbox/ImageSpatial.py @@ -1052,7 +1052,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: @@ -1074,6 +1075,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. @@ -1082,15 +1089,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_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 diff --git a/tests/test_image_spatial_extra.py b/tests/test_image_spatial_extra.py index 34825335..afe6ba81 100644 --- a/tests/test_image_spatial_extra.py +++ b/tests/test_image_spatial_extra.py @@ -413,6 +413,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