Add in-place operation

This commit is contained in:
Stefan van der Walt
2015-05-08 22:32:29 -07:00
parent 3b0179e4c1
commit 6fc4e11b9a
2 changed files with 25 additions and 4 deletions
+7 -2
View File
@@ -3,7 +3,7 @@ import numpy as np
from ..measure import label
def clear_border(labels, buffer_size=0, bgval=0):
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 directly to the input.
@@ -17,11 +17,13 @@ def clear_border(labels, buffer_size=0, bgval=0):
that touch the outside of the image are removed.
bgval : float or int, optional
Cleared objects are set to this value.
in_place : bool, optional
Whether or not to manipulate the labels array in-place.
Returns
-------
labels : (N, M) array
Cleared binary image. Note that the input label image is modified.
Cleared binary image.
Examples
--------
@@ -69,6 +71,9 @@ def clear_border(labels, 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
@@ -34,14 +34,30 @@ def test_clear_border_non_binary():
[3, 4, 5, 4, 2],
[3, 3, 2, 1, 2]])
result = clear_border(image.copy())
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()