diff --git a/skimage/segmentation/boundaries.py b/skimage/segmentation/boundaries.py index 1a7e1245..f36091bd 100644 --- a/skimage/segmentation/boundaries.py +++ b/skimage/segmentation/boundaries.py @@ -41,7 +41,7 @@ def _find_boundaries_subpixel(label_img): for index in np.ndindex(label_img_expanded.shape): if edges[index]: values = np.unique(windows[index].ravel()) - if len(values) > 2: # single value and max_label + if len(values) > 2: # single value and max_label boundaries[index] = True return boundaries @@ -51,9 +51,9 @@ def find_boundaries(label_img, connectivity=1, mode='thick', background=0): Parameters ---------- - label_img : array of int - An array in which different regions are labeled with different - integers. + label_img : array of int or bool + An array in which different regions are labeled with either different + integers or boolean values. connectivity: int in {1, ..., `label_img.ndim`}, optional A pixel is considered a boundary pixel if any of its neighbors has a different label. `connectivity` controls which pixels are @@ -144,7 +144,20 @@ def find_boundaries(label_img, connectivity=1, mode='thick', background=0): [0, 0, 0, 1, 0, 1, 0], [0, 0, 0, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0]], dtype=uint8) + >>> bool_image = np.array([[False, False, False, False, False], + ... [False, False, False, False, False], + ... [False, False, True, True, True], + ... [False, False, True, True, True], + ... [False, False, True, True, True]], dtype=np.bool) + >>> find_boundaries(bool_image) + array([[False, False, False, False, False], + [False, False, True, True, True], + [False, True, True, True, True], + [False, True, True, False, False], + [False, True, True, False, False]], dtype=bool) """ + if label_img.dtype == 'bool': + label_img = label_img.astype(np.uint8) ndim = label_img.ndim selem = ndi.generate_binary_structure(ndim, connectivity) if mode != 'subpixel': diff --git a/skimage/segmentation/tests/test_boundaries.py b/skimage/segmentation/tests/test_boundaries.py index e6e401ac..b8f7a4ae 100644 --- a/skimage/segmentation/tests/test_boundaries.py +++ b/skimage/segmentation/tests/test_boundaries.py @@ -25,6 +25,19 @@ def test_find_boundaries(): assert_array_equal(result, ref) +def test_find_boundaries_bool(): + image = np.zeros((5, 5), dtype=np.bool) + image[2:5, 2:5] = True + + ref = np.array([[False, False, False, False, False], + [False, False, True, True, True], + [False, True, True, True, True], + [False, True, True, False, False], + [False, True, True, False, False]], dtype=np.bool) + result = find_boundaries(image) + assert_array_equal(result, ref) + + def test_mark_boundaries(): image = np.zeros((10, 10)) label_image = np.zeros((10, 10), dtype=np.uint8) @@ -61,6 +74,27 @@ def test_mark_boundaries(): assert_array_equal(result, ref) +def test_mark_boundaries_bool(): + image = np.zeros((10, 10), dtype=np.bool) + label_image = np.zeros((10, 10), dtype=np.uint8) + label_image[2:7, 2:7] = 1 + + ref = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 0, 0, 0], + [0, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 1, 1, 0, 0, 0, 1, 1, 0, 0], + [0, 1, 1, 0, 0, 0, 1, 1, 0, 0], + [0, 1, 1, 0, 0, 0, 1, 1, 0, 0], + [0, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) + + marked = mark_boundaries(image, label_image, color=white, mode='thick') + result = np.mean(marked, axis=-1) + assert_array_equal(result, ref) + + def test_mark_boundaries_subpixel(): labels = np.array([[0, 0, 0, 0], [0, 0, 5, 0],