From 944f79cdce78093931817a40f5123e3cb1918882 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Tue, 12 Jun 2012 09:51:01 -0700 Subject: [PATCH] BUG: Fix background labelling for case when (0, 0) belongs to the background. --- skimage/morphology/ccomp.pyx | 3 +++ skimage/morphology/tests/test_ccomp.py | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/skimage/morphology/ccomp.pyx b/skimage/morphology/ccomp.pyx index 590c5589..a1d5f303 100644 --- a/skimage/morphology/ccomp.pyx +++ b/skimage/morphology/ccomp.pyx @@ -155,6 +155,9 @@ def label(np.ndarray[DTYPE_t, ndim=2] input, raise ValueError('Neighbors must be either 4 or 8.') # Initialize the first row + if data[0, 0] == background: + link_bg(forest_p, 0, &background_node) + for j in range(1, cols): if data[0, j] == background: link_bg(forest_p, j, &background_node) diff --git a/skimage/morphology/tests/test_ccomp.py b/skimage/morphology/tests/test_ccomp.py index 8fb98009..cb092b4c 100644 --- a/skimage/morphology/tests/test_ccomp.py +++ b/skimage/morphology/tests/test_ccomp.py @@ -61,5 +61,26 @@ class TestConnectedComponents: [0, 0, 1], [-1, -1, -1]]) + def test_background_two_regions(self): + x = np.array([[0, 0, 6], + [0, 0, 6], + [5, 5, 5]]) + + assert_array_equal(label(x, background=0), + [[-1, -1, 0], + [-1, -1, 0], + [ 1, 1, 1]]) + + def test_background_one_region_center(self): + x = np.array([[0, 0, 0], + [0, 1, 0], + [0, 0, 0]]) + + assert_array_equal(label(x, neighbors=4, background=0), + [[-1, -1, -1], + [-1, 0, -1], + [-1, -1, -1]]) + + if __name__ == "__main__": run_module_suite()