diff --git a/skimage/segmentation/_clear_border.py b/skimage/segmentation/_clear_border.py index 266e17e2..318f8790 100644 --- a/skimage/segmentation/_clear_border.py +++ b/skimage/segmentation/_clear_border.py @@ -1,37 +1,39 @@ import numpy as np -from scipy.ndimage import label +#from scipy.ndimage import label +from ..measure import label -def clear_border(image, buffer_size=0, bgval=0): - """Clear objects connected to image border. +def clear_border(labels, buffer_size=0, bgval=0): + """Clear objects connected to the label image border. - The changes will be applied to the input image. + The changes will be applied directly to the input. Parameters ---------- - image : (N, M) array - Binary image. + labels : (N, M) array of int + Label or binary image. buffer_size : int, optional - Define additional buffer around image border. + The width of the border examined. By default, only objects + that touch the outside of the image are removed. bgval : float or int, optional - Value for cleared objects. + Cleared objects are set to this value. Returns ------- - image : (N, M) array - Cleared binary image. + labels : (N, M) array + Cleared binary image. Note that the input label image is modified. Examples -------- >>> import numpy as np >>> from skimage.segmentation import clear_border - >>> image = np.array([[0, 0, 0, 0, 0, 0, 0, 1, 0], - ... [0, 0, 0, 0, 1, 0, 0, 0, 0], - ... [1, 0, 0, 1, 0, 1, 0, 0, 0], - ... [0, 0, 1, 1, 1, 1, 1, 0, 0], - ... [0, 1, 1, 1, 1, 1, 1, 1, 0], - ... [0, 0, 0, 0, 0, 0, 0, 0, 0]]) - >>> clear_border(image) + >>> labels = np.array([[0, 0, 0, 0, 0, 0, 0, 1, 0], + ... [0, 0, 0, 0, 1, 0, 0, 0, 0], + ... [1, 0, 0, 1, 0, 1, 0, 0, 0], + ... [0, 0, 1, 1, 1, 1, 1, 0, 0], + ... [0, 1, 1, 1, 1, 1, 1, 1, 0], + ... [0, 0, 0, 0, 0, 0, 0, 0, 0]]) + >>> clear_border(labels) array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 1, 0, 0, 0], @@ -40,6 +42,7 @@ def clear_border(image, buffer_size=0, bgval=0): [0, 0, 0, 0, 0, 0, 0, 0, 0]]) """ + image = labels rows, cols = image.shape if buffer_size >= rows or buffer_size >= cols: @@ -53,7 +56,10 @@ def clear_border(image, buffer_size=0, bgval=0): borders[:, :ext] = True borders[:, - ext:] = True - labels, number = label(image) + # Re-label, in case we are dealing with a binary image + # and to get consistent labeling + labels = label(image, background=0) + 1 + number = np.max(labels) + 1 # determine all objects that are connected to borders borders_indices = np.unique(labels[borders]) diff --git a/skimage/segmentation/tests/test_clear_border.py b/skimage/segmentation/tests/test_clear_border.py index 5d6852cf..67739426 100644 --- a/skimage/segmentation/tests/test_clear_border.py +++ b/skimage/segmentation/tests/test_clear_border.py @@ -24,9 +24,24 @@ def test_clear_border(): assert_array_equal(result, np.zeros(result.shape)) # test background value - result = clear_border(image.copy(), 1, 2) + result = clear_border(image.copy(), buffer_size=1, bgval=2) assert_array_equal(result, 2 * np.ones_like(image)) +def test_clear_border_non_binary(): + image = np.array([[1, 2, 3, 1, 2], + [3, 4, 5, 4, 2], + [3, 4, 5, 4, 2], + [3, 3, 2, 1, 2]]) + + result = clear_border(image.copy()) + expected = np.array([[0, 0, 0, 0, 0], + [0, 4, 5, 4, 0], + [0, 4, 5, 4, 0], + [0, 0, 0, 0, 0]]) + + assert_array_equal(result, expected) + + if __name__ == "__main__": np.testing.run_module_suite()