From 9178e77aef995f64fb8a656d746225cee9f2d839 Mon Sep 17 00:00:00 2001 From: Guillem Palou Visa Date: Tue, 24 Dec 2013 13:25:48 +0100 Subject: [PATCH] Changed variable names for readbility, fixed PEP8 compliance --- skimage/segmentation/_slic.pyx | 89 +++++++++++++------------ skimage/segmentation/tests/test_slic.py | 31 +++++++-- 2 files changed, 75 insertions(+), 45 deletions(-) diff --git a/skimage/segmentation/_slic.pyx b/skimage/segmentation/_slic.pyx index 154de094..21a5adc9 100644 --- a/skimage/segmentation/_slic.pyx +++ b/skimage/segmentation/_slic.pyx @@ -150,9 +150,9 @@ def _slic_cython(double[:, :, :, ::1] image_zyx, def _enforce_label_connectivity_cython(Py_ssize_t[:, :, ::1] segments, - Py_ssize_t n_segments, - int min_size, - int max_size): + Py_ssize_t n_segments, + int min_size, + int max_size): """ Helper function to remove small disconnected regions from the labels Parameters @@ -179,64 +179,71 @@ def _enforce_label_connectivity_cython(Py_ssize_t[:, :, ::1] segments, width = segments.shape[2] #neighborhood arrays - cdef Py_ssize_t[:] ddx = np.array((1,-1,0,0,0,0)) - cdef Py_ssize_t[:] ddy = np.array((0,0,1,-1,0,0)) - cdef Py_ssize_t[:] ddz = np.array((0,0,0,0,1,-1)) + cdef Py_ssize_t[::1] ddx = np.array((1, -1, 0, 0, 0, 0)) + cdef Py_ssize_t[::1] ddy = np.array((0, 0, 1, -1, 0, 0)) + cdef Py_ssize_t[::1] ddz = np.array((0, 0, 0, 0, 1, -1)) #new object with connected segments - cdef Py_ssize_t[:, :, ::1] connected_segments = np.zeros_like(segments) + cdef Py_ssize_t[:, :, ::1] connected_segments \ + = np.zeros_like(segments, dtype=np.intp) - cdef Py_ssize_t current_new_label = 0 + cdef Py_ssize_t current_new_label = 0 cdef Py_ssize_t label = 0 #variables for the breadth first search - cdef Py_ssize_t count = 1 - cdef Py_ssize_t p = 0 + cdef Py_ssize_t current_segment_size = 1 + cdef Py_ssize_t bfs_visited = 0 cdef Py_ssize_t adjacent - cdef Py_ssize_t zz,yy,xx + cdef Py_ssize_t zz, yy, xx - cdef Py_ssize_t[:, ::1] coord_list = np.zeros((max_size,3), dtype=np.intp) + cdef Py_ssize_t[:, ::1] coord_list = np.zeros((max_size, 3), dtype=np.intp) #loop through all image for z in range(depth): for y in range(height): for x in range(width): - if (connected_segments[z,y,x] > 0): + if connected_segments[z, y, x] > 0: continue #find the component size adjacent = 0 - label = segments[z,y,x] + label = segments[z, y, x] current_new_label += 1 - connected_segments[z,y,x] = current_new_label + connected_segments[z, y, x] = current_new_label + current_segment_size = 1 + bfs_visited = 0 + coord_list[bfs_visited, 0] = z + coord_list[bfs_visited, 1] = y + coord_list[bfs_visited, 2] = x - count = 1 - p = 0 - coord_list[p,0] = z - coord_list[p,1] = y - coord_list[p,2] = x - - #perform a breadth first search to find the size of the connected component - while (p != count): + #perform a breadth first search to find + # the size of the connected component + while bfs_visited != current_segment_size: for i in range(6): - zz = coord_list[p,0] + ddz[i] - yy = coord_list[p,1] + ddy[i] - xx = coord_list[p,2] + ddx[i] - if (xx >= 0 and xx < width and yy >= 0 and yy < height and zz >= 0 and zz < depth): - if (segments[zz,yy,xx] == label and connected_segments[zz,yy,xx] == 0): - connected_segments[zz,yy,xx] = current_new_label - coord_list[count,0] = zz - coord_list[count,1] = yy - coord_list[count,2] = xx - count = count + 1 - elif (connected_segments[zz,yy,xx] > 0 and connected_segments[zz,yy,xx] != current_new_label): - adjacent = connected_segments[zz,yy,xx] - p = p + 1 - + zz = coord_list[bfs_visited, 0] + ddz[i] + yy = coord_list[bfs_visited, 1] + ddy[i] + xx = coord_list[bfs_visited, 2] + ddx[i] + if (xx >= 0 and xx < width and + yy >= 0 and yy < height and + zz >= 0 and zz < depth): + if (segments[zz, yy, xx] == label and + connected_segments[zz, yy, xx] == 0): + connected_segments[zz, yy, xx] = \ + current_new_label + coord_list[current_segment_size, 0] = zz + coord_list[current_segment_size, 1] = yy + coord_list[current_segment_size, 2] = xx + current_segment_size += 1 + elif (connected_segments[zz, yy, xx] > 0 and + connected_segments[zz, yy, xx] != current_new_label): + adjacent = connected_segments[zz, yy, xx] + bfs_visited += 1 #change to an adjacent one, like in the original paper - if (count < min_size): - for i in range(count): - connected_segments[coord_list[i,0],coord_list[i,1],coord_list[i,2]] = adjacent + if current_segment_size < min_size: + for i in range(current_segment_size): + connected_segments[coord_list[i, 0], + coord_list[i, 1], + coord_list[i, 2]] = adjacent - return np.asarray(connected_segments) \ No newline at end of file + return np.asarray(connected_segments) diff --git a/skimage/segmentation/tests/test_slic.py b/skimage/segmentation/tests/test_slic.py index 0965e351..be0c2d79 100644 --- a/skimage/segmentation/tests/test_slic.py +++ b/skimage/segmentation/tests/test_slic.py @@ -65,7 +65,7 @@ def test_color_3d(): assert_equal(len(np.unique(seg)), 8) for s, c in zip(slices, range(8)): - assert_equal(seg[s], c+1) + assert_equal(seg[s], c + 1) def test_gray_3d(): @@ -76,7 +76,7 @@ def test_gray_3d(): midpoint = dim_size // 2 slices.append((slice(None, midpoint), slice(midpoint, None))) slices = list(it.product(*slices)) - shades = np.arange(0, 1.000001, 1.0/7) + shades = np.arange(0, 1.000001, 1.0 / 7) for s, sh in zip(slices, shades): img[s] = sh img += 0.001 * rnd.normal(size=img.shape) @@ -87,7 +87,7 @@ def test_gray_3d(): assert_equal(len(np.unique(seg)), 8) for s, c in zip(slices, range(8)): - assert_equal(seg[s], c+1) + assert_equal(seg[s], c + 1) def test_list_sigma(): @@ -106,7 +106,7 @@ def test_spacing(): img = np.array([[1, 1, 1, 0, 0], [1, 1, 0, 0, 0]], np.float) result_non_spaced = np.array([[0, 0, 0, 1, 1], - [0, 0, 1, 1, 1]], np.int) +1 + [0, 0, 1, 1, 1]], np.int) + 1 result_spaced = np.array([[0, 0, 0, 0, 0], [1, 1, 1, 1, 1]], np.int) + 1 img += 0.1 * rnd.normal(size=img.shape) @@ -124,7 +124,30 @@ def test_invalid_lab_conversion(): assert_raises(ValueError, slic, img, multichannel=True, convert2lab=True) +def test_enforce_connectivity(): + img = np.array([[0, 0, 0, 1, 1, 1], + [1, 0, 0, 1, 1, 0], + [0, 0, 0, 1, 1, 0]], np.float) + + segments_connected = slic(img, 2, compactness=0.0001, + enforce_connectivity=True, + convert2lab=False) + segments_disconnected = slic(img, 2, compactness=0.0001, + enforce_connectivity=False, + convert2lab=False) + + result_connected = np.array([[1, 1, 1, 2, 2, 2], + [1, 1, 1, 2, 2, 2], + [1, 1, 1, 2, 2, 2]], np.float) + + result_disconnected = np.array([[1, 1, 1, 2, 2, 2], + [2, 1, 1, 2, 2, 1], + [1, 1, 1, 2, 2, 1]], np.float) + + assert_equal(segments_connected, result_connected) + assert_equal(segments_disconnected, result_disconnected) if __name__ == '__main__': from numpy import testing + testing.run_module_suite()