diff --git a/skimage/measure/_ccomp.pyx b/skimage/measure/_ccomp.pyx index 5344aa9a..98502579 100644 --- a/skimage/measure/_ccomp.pyx +++ b/skimage/measure/_ccomp.pyx @@ -42,9 +42,20 @@ ctypedef struct bginfo: # A pixel has neighbors that have already been scanned. -# In the paper, the pixel is denoted by E and its neighbors -# by A, B, C and D (in 2D) - see doc for function get_shape_info() +# In the paper, the pixel is denoted by E and its neighbors: +# +# z=1 z=0 x +# ----------------------> +# | A B C F G H +# | D E . I J K +# | . . . L M N +# | +# y V +# +# D_ea represents offset of A from E etc. - see the definition of +# get_shape_info cdef enum: + # the 0D neighbor # D_ee, # We don't need D_ee # the 1D neighbor D_ed, @@ -106,15 +117,8 @@ cdef shape_info get_shape_info(inarr_shape): res.numels = res.x * res.y * res.z - # Our point of interest is E. - # z=1 z=0 x - # ----------------------> - # | A B C F G H - # | D E . I J K - # | . . . L M N - # | - # y V - # + # When reading this for the first time, look at the diagram by the enum + # definition above (keyword D_ee) # Difference between E and G is (x=0, y=-1, z=-1), E and A (-1, -1, 0) etc. # Here, it is recalculated to linear (raveled) indices of flattened arrays # with their last (=contiguous) dimension is x. @@ -278,7 +282,7 @@ def label(input, DTYPE_t neighbors=8, background=None, return_num=False): Image to label. neighbors : {4, 8}, int, optional Whether to use 4- or 8-connectivity. - In 3D, 4-connectivity means connected pixels share have to share face, + In 3D, 4-connectivity means connected pixels have to share face, whereas with 8-connectivity, they have to share only edge or vertex. background : int, optional Consider all pixels with this value as background pixels, and label @@ -389,7 +393,7 @@ cdef DTYPE_t resolve_labels(DTYPE_t *data_p, DTYPE_t *forest_p, for i in range(shapeinfo.numels): if i == bg.background_node: - data_p[i] = -1 + data_p[i] = bg.background_val elif i == forest_p[i]: # We have stumbled across a root which is something new to us (root # is the LOWEST of all prov. labels that are equivalent to it) diff --git a/skimage/morphology/tests/test_ccomp.py b/skimage/morphology/tests/test_ccomp.py index 490c32d3..b33bec4e 100644 --- a/skimage/morphology/tests/test_ccomp.py +++ b/skimage/morphology/tests/test_ccomp.py @@ -6,7 +6,10 @@ from warnings import catch_warnings from skimage._shared.utils import skimage_deprecation np.random.seed(0) -BGL = -1 + +# The background label value +# is supposed to be changed to 0 soon +BG = -1 class TestConnectedComponents: @@ -191,12 +194,12 @@ class TestConnectedComponents3d: [1, 0, 2], [1, 1, 1]]) lb = x.copy() - lb[0] = np.array([[0, BGL, BGL], - [0, BGL, BGL], - [BGL, BGL, BGL]]) - lb[1] = np.array([[BGL, BGL, BGL], - [BGL, 0, 1], - [BGL, BGL, BGL]]) + lb[0] = np.array([[0, BG, BG], + [0, BG, BG], + [BG, BG, BG]]) + lb[1] = np.array([[BG, BG, BG], + [BG, 0, 1], + [BG, BG, BG]]) with catch_warnings(): assert_array_equal(label(x), lnb) @@ -212,12 +215,12 @@ class TestConnectedComponents3d: [5, 0, 0], [0, 0, 0]]) lb = x.copy() - lb[0] = np.array([[BGL, BGL, 0], - [BGL, BGL, 0], - [1, 1, 1]]) - lb[1] = np.array([[0, 0, BGL], - [1, BGL, BGL], - [BGL, BGL, BGL]]) + lb[0] = np.array([[BG, BG, 0], + [BG, BG, 0], + [1, 1, 1]]) + lb[1] = np.array([[0, 0, BG], + [1, BG, BG], + [BG, BG, BG]]) res = label(x, background=0) assert_array_equal(res, lb) @@ -226,7 +229,7 @@ class TestConnectedComponents3d: x = np.zeros((3, 3, 3), int) x[1, 1, 1] = 1 - lb = np.ones_like(x) * BGL + lb = np.ones_like(x) * BG lb[1, 1, 1] = 0 assert_array_equal(label(x, neighbors=4, background=0), lb)