Merge pull request #1449 from stefanv/clear_border_non_binary

Allow clear_border to operate on labeled images
This commit is contained in:
Juan Nunez-Iglesias
2015-12-09 11:18:40 +11:00
2 changed files with 61 additions and 19 deletions
+28 -17
View File
@@ -1,37 +1,41 @@
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, in_place=False):
"""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.
in_place : bool, optional
Whether or not to manipulate the labels array in-place.
Returns
-------
image : (N, M) array
labels : (N, M) array
Cleared binary image.
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 +44,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 +58,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])
@@ -63,6 +71,9 @@ def clear_border(image, buffer_size=0, bgval=0):
# create mask for pixels to clear
mask = label_mask[labels.ravel()].reshape(labels.shape)
if not in_place:
image = image.copy()
# clear border pixels
image[mask] = bgval
@@ -1,5 +1,5 @@
import numpy as np
from numpy.testing import assert_array_equal
from numpy.testing import assert_array_equal, assert_
from skimage.segmentation import clear_border
@@ -24,9 +24,40 @@ 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, 3, 5, 4, 2],
[3, 4, 5, 4, 2],
[3, 3, 2, 1, 2]])
result = clear_border(image)
expected = np.array([[0, 0, 0, 0, 0],
[0, 0, 5, 4, 0],
[0, 4, 5, 4, 0],
[0, 0, 0, 0, 0]])
assert_array_equal(result, expected)
assert_(not np.all(image == result))
def test_clear_border_non_binary_inplace():
image = np.array([[1, 2, 3, 1, 2],
[3, 3, 5, 4, 2],
[3, 4, 5, 4, 2],
[3, 3, 2, 1, 2]])
result = clear_border(image, in_place=True)
expected = np.array([[0, 0, 0, 0, 0],
[0, 0, 5, 4, 0],
[0, 4, 5, 4, 0],
[0, 0, 0, 0, 0]])
assert_array_equal(result, expected)
assert_array_equal(image, result)
if __name__ == "__main__":
np.testing.run_module_suite()