8000 Make find_boundaries symmetric · jni/scikit-image@85509c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 85509c3

Browse files
committed
Make find_boundaries symmetric
Fixes scikit-image#738
1 parent 7c11861 commit 85509c3

File tree

2 files changed

+48
-28
lines changed

2 files changed

+48
-28
lines changed

skimage/segmentation/boundaries.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
import numpy as np
2-
from ..morphology import dilation, square
2+
from scipy import ndimage as nd
3+
from ..morphology import dilation, erosion, square
34
from ..util import img_as_float
45
from ..color import gray2rgb
56
from .._shared.utils import deprecated
67

78

8-
def find_boundaries(label_img):
9-
"""Return bool array where boundaries between labeled regions are True."""
10-
boundaries = np.zeros(label_img.shape, dtype=np.bool)
11-
boundaries[1:, :] += label_img[1:, :] != label_img[:-1, :]
12-
boundaries[:, 1:] += label_img[:, 1:] != label_img[:, :-1]
9+
def find_boundaries(label_img, connectivity=1):
10+
"""Return bool array where boundaries between labeled regions are True.
11+
12+
Parameters
13+
----------
14+
label_img : array of int
15+
An array in which different regions are labeled with different
16+
integers.
17+
connectivity: int in {1, ..., `label_img.ndim`}, optional
18+
A pixel is considered a boundary pixel if any of its neighbors
19+
has a different label. `connectivity` controls which pixels are
20+
considered neighbors. A connectivity of 1 (default) means
21+
pixels sharing an edge (in 2D) or a face (in 3D) will be
22+
considered neighbors. A connectivity of `label_img.ndim` means
23+
pixels sharing a corner will be considered neighbors.
24+
25+
Returns
26+
-------
27+
boundaries : array of bool, same shape as `label_img`
28+
A bool image where `True` represents a boundary pixel.
29+
"""
30+
selem = nd.generate_binary_structure(label_img.ndim, connectivity)
31+
boundaries = dilation(label_img, selem) != erosion(label_img, selem)
1332
return boundaries
1433

1534

@@ -35,7 +54,7 @@ def mark_boundaries(image, label_img, color=(1, 1, 0),
3554

3655
boundaries = find_boundaries(label_img)
3756
if outline_color is not None:
38-
outer_boundaries = dilation(boundaries.astype(np.uint8), square(2))
57+
outer_boundaries = dilation(boundaries.astype(np.uint8), square(3))
3958
image[outer_boundaries != 0, :] = np.array(outline_color)
4059
image[boundaries, :] = np.array(color)
4160
return image

skimage/segmentation/tests/test_boundaries.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ def test_find_boundaries():
88
image[2:7, 2:7] = 1
99

1010
ref = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
11-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
12-
[0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
13-
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
14-
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
15-
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
16-
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
11+
[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
12+
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
13+
[0, 1, 1, 0, 0, 0, 1, 1, 0, 0],
14+
[0, 1, 1, 0, 0, 0, 1, 1, 0, 0],
15+
[0, 1, 1, 0, 0, 0, 1, 1, 0, 0],
16+
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
1717
[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
1818
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1919
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
@@ -28,27 +28,28 @@ def test_mark_boundaries():
2828
label_image[2:7, 2:7] = 1
2929

3030
ref = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
31-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
32-
[0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
33-
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
34-
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
35-
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
36-
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
31+
[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
32+
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
33+
[0, 1, 1, 0, 0, 0, 1, 1, 0, 0],
34+
[0, 1, 1, 0, 0, 0, 1, 1, 0, 0],
35+
[0, 1, 1, 0, 0, 0, 1, 1, 0, 0],
36+
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
3737
[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
3838
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
3939
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
40+
4041
result = mark_boundaries(image, label_image, color=(1, 1, 1)).mean(axis=2)
4142
assert_array_equal(result, ref)
4243

43-
ref = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
44-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
45-
[0, 0, 1, 1, 1, 1, 1, 1, 2, 0],
46-
[0, 0, 1, 2, 2, 2, 2, 1, 2, 0],
47-
[0, 0, 1, 2, 0, 0, 0, 1, 2, 0],
48-
[0, 0, 1, 2, 0, 0, 0, 1, 2, 0],
49-
[0, 0, 1, 2, 0, 0, 0, 1, 2, 0],
50-
[0, 0, 1, 1, 1, 1, 1, 2, 2, 0],
51-
[0, 0, 2, 2, 2, 2, 2, 2, 0, 0],
44+
ref = np.array([[0, 2, 2, 2, 2, 2, 2, 2, 0, 0],
45+
[2, 2, 1, 1, 1, 1, 1, 2, 2, 0],
46+
[2, 1, 1, 1, 1, 1, 1, 1, 2, 0],
47+
[2, 1, 1, 2, 2, 2, 1, 1, 2, 0],
48+
[2, 1, 1, 2, 0, 2, 1, 1, 2, 0],
49+
[2, 1, 1, 2, 2, 2, 1, 1, 2, 0],
50+
[2, 1, 1, 1, 1, 1, 1, 1, 2, 0],
51+
[2, 2, 1, 1, 1, 1, 1, 2, 2, 0],
52+
[0, 2, 2, 2, 2, 2, 2, 2, 0, 0],
5253
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
5354
result = mark_boundaries(image, label_image, color=(1, 1, 1),
5455
outline_color=(2, 2, 2)).mean(axis=2)

0 commit comments

Comments
 (0)
0