From 7444b7a80e5fd15f62412bd04984c605d4939013 Mon Sep 17 00:00:00 2001 From: Jeremy Metz Date: Fri, 13 May 2016 16:17:24 +0100 Subject: [PATCH 1/8] Updated clear_border to allow Nd matrix input --- skimage/segmentation/_clear_border.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/skimage/segmentation/_clear_border.py b/skimage/segmentation/_clear_border.py index a44f0785..fd8897a4 100644 --- a/skimage/segmentation/_clear_border.py +++ b/skimage/segmentation/_clear_border.py @@ -45,17 +45,21 @@ def clear_border(labels, buffer_size=0, bgval=0, in_place=False): """ image = labels - rows, cols = image.shape - if buffer_size >= rows or buffer_size >= cols: + if any( ( buffer_size >= s for s in image.shape)): raise ValueError("buffer size may not be greater than image size") # create borders with buffer_size borders = np.zeros_like(image, dtype=np.bool_) ext = buffer_size + 1 - borders[:ext] = True - borders[- ext:] = True - borders[:, :ext] = True - borders[:, - ext:] = True + slstart = slice(ext) + slend = slice(-ext, None) + slices = [slice(s) for s in image.shape] + for d in range(image.ndim): + slicedim = slices.copy() + slicedim[d] = slstart + borders[slicedim] = True + slicedim[d] = slend + borders[slicedim] = True # Re-label, in case we are dealing with a binary image # and to get consistent labeling From 6361f3745ebee80c8345a0c7a330f1be3bcc3347 Mon Sep 17 00:00:00 2001 From: Jeremy Metz Date: Sat, 14 May 2016 14:43:46 +0100 Subject: [PATCH 2/8] Added test_clear_border_3d --- .../segmentation/tests/test_clear_border.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/skimage/segmentation/tests/test_clear_border.py b/skimage/segmentation/tests/test_clear_border.py index b626ea2b..c300d186 100644 --- a/skimage/segmentation/tests/test_clear_border.py +++ b/skimage/segmentation/tests/test_clear_border.py @@ -28,6 +28,36 @@ def test_clear_border(): assert_array_equal(result, 2 * np.ones_like(image)) +def test_clear_border_3d(): + image = np.array([ + [[0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0], + [1, 0, 0, 0]], + [[0, 0, 0, 0], + [0, 1, 1, 0], + [0, 0, 1, 0], + [0, 0, 0, 0]], + [[0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0]], + ]) + # test default case + result = clear_border(image.copy()) + ref = image.copy() + ref[0, 3, 0] = 0 + assert_array_equal(result, ref) + + # test buffer + result = clear_border(image.copy(), 1) + assert_array_equal(result, np.zeros(result.shape)) + + # test background value + 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], From 168410f42422af647f41c6f5422fd6ae18381621 Mon Sep 17 00:00:00 2001 From: Jeremy Metz Date: Sat, 14 May 2016 14:52:07 +0100 Subject: [PATCH 3/8] Added additional 3d analogs of clear_border tests --- .../segmentation/tests/test_clear_border.py | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/skimage/segmentation/tests/test_clear_border.py b/skimage/segmentation/tests/test_clear_border.py index c300d186..4ee297eb 100644 --- a/skimage/segmentation/tests/test_clear_border.py +++ b/skimage/segmentation/tests/test_clear_border.py @@ -74,6 +74,42 @@ def test_clear_border_non_binary(): assert_(not np.all(image == result)) +def test_clear_border_non_binary_3d(): + image3d = np.array( + [[[1, 2, 3, 1, 2], + [3, 3, 3, 4, 2], + [3, 4, 3, 4, 2], + [3, 3, 2, 1, 2]], + [[1, 2, 3, 1, 2], + [3, 3, 5, 4, 2], + [3, 4, 5, 4, 2], + [3, 3, 2, 1, 2]], + [[1, 2, 3, 1, 2], + [3, 3, 3, 4, 2], + [3, 4, 3, 4, 2], + [3, 3, 2, 1, 2]], + ) + + result = clear_border(image3d) + expected = np.array( + [[[0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0]], + [[0, 0, 0, 0, 0], + [0, 0, 5, 0, 0], + [0, 0, 5, 0, 0], + [0, 0, 0, 0, 0]], + [[0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0]], + ) + + assert_array_equal(result, expected) + assert_(not np.all(image3d == result)) + + def test_clear_border_non_binary_inplace(): image = np.array([[1, 2, 3, 1, 2], [3, 3, 5, 4, 2], @@ -89,5 +125,41 @@ def test_clear_border_non_binary_inplace(): assert_array_equal(result, expected) assert_array_equal(image, result) + +def test_clear_border_non_binary_inplace_3d(): + image3d = np.array( + [[[1, 2, 3, 1, 2], + [3, 3, 3, 4, 2], + [3, 4, 3, 4, 2], + [3, 3, 2, 1, 2]], + [[1, 2, 3, 1, 2], + [3, 3, 5, 4, 2], + [3, 4, 5, 4, 2], + [3, 3, 2, 1, 2]], + [[1, 2, 3, 1, 2], + [3, 3, 3, 4, 2], + [3, 4, 3, 4, 2], + [3, 3, 2, 1, 2]], + ) + + result = clear_border(image3d, in_place=True) + expected = np.array( + [[[0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0]], + [[0, 0, 0, 0, 0], + [0, 0, 5, 0, 0], + [0, 0, 5, 0, 0], + [0, 0, 0, 0, 0]], + [[0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0]], + ) + + assert_array_equal(result, expected) + assert_array_equal(image3d, result) + if __name__ == "__main__": np.testing.run_module_suite() From 9305ec138cf223d7fefb4dd910713e6a3ff4469e Mon Sep 17 00:00:00 2001 From: Jeremy Metz Date: Sat, 14 May 2016 16:33:27 +0100 Subject: [PATCH 4/8] Updated doctring to Nd --- skimage/segmentation/_clear_border.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/skimage/segmentation/_clear_border.py b/skimage/segmentation/_clear_border.py index fd8897a4..f219b3ca 100644 --- a/skimage/segmentation/_clear_border.py +++ b/skimage/segmentation/_clear_border.py @@ -5,12 +5,10 @@ from ..measure import label 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. - Parameters ---------- - labels : (N, M) array of int - Label or binary image. + labels : (M[, N[, ..., P]]) array of int + Label or binary image buffer_size : int, optional The width of the border examined. By default, only objects that touch the outside of the image are removed. @@ -21,7 +19,7 @@ def clear_border(labels, buffer_size=0, bgval=0, in_place=False): Returns ------- - labels : (N, M) array + labels : (M[, N[, ..., P]]) array Cleared binary image. Examples From 40e161fbb3ffdb35ccc1debdeeee874b00591670 Mon Sep 17 00:00:00 2001 From: Jeremy Metz Date: Sat, 14 May 2016 17:33:54 +0100 Subject: [PATCH 5/8] Py2 compatibility - changed list.copy --- skimage/segmentation/_clear_border.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/segmentation/_clear_border.py b/skimage/segmentation/_clear_border.py index f219b3ca..a16f65ba 100644 --- a/skimage/segmentation/_clear_border.py +++ b/skimage/segmentation/_clear_border.py @@ -53,7 +53,7 @@ def clear_border(labels, buffer_size=0, bgval=0, in_place=False): slend = slice(-ext, None) slices = [slice(s) for s in image.shape] for d in range(image.ndim): - slicedim = slices.copy() + slicedim = list(slices) slicedim[d] = slstart borders[slicedim] = True slicedim[d] = slend From c0798fa8153e6f5c52f752c5bec3e845a293d35b Mon Sep 17 00:00:00 2001 From: Jeremy Metz Date: Sat, 14 May 2016 18:17:30 +0100 Subject: [PATCH 6/8] Fixed missing close brackets on test data creation --- skimage/segmentation/tests/test_clear_border.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skimage/segmentation/tests/test_clear_border.py b/skimage/segmentation/tests/test_clear_border.py index 4ee297eb..9d8f9b50 100644 --- a/skimage/segmentation/tests/test_clear_border.py +++ b/skimage/segmentation/tests/test_clear_border.py @@ -88,7 +88,7 @@ def test_clear_border_non_binary_3d(): [3, 3, 3, 4, 2], [3, 4, 3, 4, 2], [3, 3, 2, 1, 2]], - ) + ]) result = clear_border(image3d) expected = np.array( @@ -104,7 +104,7 @@ def test_clear_border_non_binary_3d(): [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], - ) + ]) assert_array_equal(result, expected) assert_(not np.all(image3d == result)) From 00734a1c77643129fedc7d1354db983401853153 Mon Sep 17 00:00:00 2001 From: Jeremy Metz Date: Sat, 14 May 2016 18:17:30 +0100 Subject: [PATCH 7/8] Fixed missing close brackets on test data creation --- skimage/segmentation/tests/test_clear_border.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/skimage/segmentation/tests/test_clear_border.py b/skimage/segmentation/tests/test_clear_border.py index 4ee297eb..34ba5979 100644 --- a/skimage/segmentation/tests/test_clear_border.py +++ b/skimage/segmentation/tests/test_clear_border.py @@ -88,7 +88,7 @@ def test_clear_border_non_binary_3d(): [3, 3, 3, 4, 2], [3, 4, 3, 4, 2], [3, 3, 2, 1, 2]], - ) + ]) result = clear_border(image3d) expected = np.array( @@ -104,7 +104,7 @@ def test_clear_border_non_binary_3d(): [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], - ) + ]) assert_array_equal(result, expected) assert_(not np.all(image3d == result)) @@ -140,7 +140,7 @@ def test_clear_border_non_binary_inplace_3d(): [3, 3, 3, 4, 2], [3, 4, 3, 4, 2], [3, 3, 2, 1, 2]], - ) + ]) result = clear_border(image3d, in_place=True) expected = np.array( @@ -156,7 +156,7 @@ def test_clear_border_non_binary_inplace_3d(): [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], - ) + ]) assert_array_equal(result, expected) assert_array_equal(image3d, result) From 2f3533c3871d116ffdff542dd43535c4ed322c22 Mon Sep 17 00:00:00 2001 From: Jeremy Metz Date: Sun, 15 May 2016 20:52:56 +0100 Subject: [PATCH 8/8] Updated docstring for improved readability --- skimage/segmentation/_clear_border.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/skimage/segmentation/_clear_border.py b/skimage/segmentation/_clear_border.py index a16f65ba..3a920e44 100644 --- a/skimage/segmentation/_clear_border.py +++ b/skimage/segmentation/_clear_border.py @@ -7,8 +7,8 @@ def clear_border(labels, buffer_size=0, bgval=0, in_place=False): Parameters ---------- - labels : (M[, N[, ..., P]]) array of int - Label or binary image + labels : (M[, N[, ..., P]]) array of int or bool + Imaging data labels. buffer_size : int, optional The width of the border examined. By default, only objects that touch the outside of the image are removed. @@ -19,8 +19,8 @@ def clear_border(labels, buffer_size=0, bgval=0, in_place=False): Returns ------- - labels : (M[, N[, ..., P]]) array - Cleared binary image. + out : (M[, N[, ..., P]]) array + Imaging data labels with cleared borders Examples --------