Labels start at 0, for backward compatibility

Code is PEP8 compliant
This commit is contained in:
Guillem Palou Visa
2013-12-24 18:45:55 +01:00
parent e2724d32c9
commit 4982b00f0c
3 changed files with 32 additions and 31 deletions
+12 -11
View File
@@ -116,7 +116,7 @@ def _slic_cython(double[:, :, :, ::1] image_zyx,
- segments[k, c]) ** 2
if distance[z, y, x] > dist_center:
# segments start at 1
nearest_segments[z, y, x] = k+1
nearest_segments[z, y, x] = k
distance[z, y, x] = dist_center
change = 1
@@ -133,7 +133,7 @@ def _slic_cython(double[:, :, :, ::1] image_zyx,
for y in range(height):
for x in range(width):
#compensate the label offset 1
k = nearest_segments[z, y, x] - 1
k = nearest_segments[z, y, x]
n_segment_elems[k] += 1
segments[k, 0] += z
segments[k, 1] += y
@@ -183,9 +183,9 @@ def _enforce_label_connectivity_cython(Py_ssize_t[:, :, ::1] segments,
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
#new object with connected segments initialized to -1
cdef Py_ssize_t[:, :, ::1] connected_segments \
= np.zeros_like(segments, dtype=np.intp)
= -1 * np.ones_like(segments, dtype=np.intp)
cdef Py_ssize_t current_new_label = 0
cdef Py_ssize_t label = 0
@@ -203,12 +203,11 @@ def _enforce_label_connectivity_cython(Py_ssize_t[:, :, ::1] segments,
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]
current_new_label += 1
connected_segments[z, y, x] = current_new_label
current_segment_size = 1
bfs_visited = 0
@@ -223,18 +222,18 @@ def _enforce_label_connectivity_cython(Py_ssize_t[:, :, ::1] segments,
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 (0 <= xx < width and
0 <= yy < height and
0 <= zz < depth):
if (segments[zz, yy, xx] == label and
connected_segments[zz, yy, xx] == 0):
connected_segments[zz, yy, xx] == -1):
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
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
@@ -245,5 +244,7 @@ def _enforce_label_connectivity_cython(Py_ssize_t[:, :, ::1] segments,
connected_segments[coord_list[i, 0],
coord_list[i, 1],
coord_list[i, 2]] = adjacent
else:
current_new_label += 1
return np.asarray(connected_segments)
+1 -1
View File
@@ -171,7 +171,7 @@ def slic(image, n_segments=100, compactness=10., max_iter=10, sigma=None,
labels = _slic_cython(image, segments, max_iter, spacing)
if (enforce_connectivity):
if enforce_connectivity:
segment_size = depth * height * width / n_segments
labels = _enforce_label_connectivity_cython(labels,
n_segments,
+19 -19
View File
@@ -21,10 +21,10 @@ def test_color_2d():
# we expect 4 segments
assert_equal(len(np.unique(seg)), 4)
assert_equal(seg.shape, img.shape[:-1])
assert_equal(seg[:10, :10], 1)
assert_equal(seg[10:, :10], 3)
assert_equal(seg[:10, 10:], 2)
assert_equal(seg[10:, 10:], 4)
assert_equal(seg[:10, :10], 0)
assert_equal(seg[10:, :10], 2)
assert_equal(seg[:10, 10:], 1)
assert_equal(seg[10:, 10:], 3)
def test_gray_2d():
@@ -41,10 +41,10 @@ def test_gray_2d():
assert_equal(len(np.unique(seg)), 4)
assert_equal(seg.shape, img.shape)
assert_equal(seg[:10, :10], 1)
assert_equal(seg[10:, :10], 3)
assert_equal(seg[:10, 10:], 2)
assert_equal(seg[10:, 10:], 4)
assert_equal(seg[:10, :10], 0)
assert_equal(seg[10:, :10], 2)
assert_equal(seg[:10, 10:], 1)
assert_equal(seg[10:, 10:], 3)
def test_color_3d():
@@ -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)
def test_gray_3d():
@@ -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)
def test_list_sigma():
@@ -96,7 +96,7 @@ def test_list_sigma():
[0, 0, 0, 1, 1, 1]], np.float)
img += 0.1 * rnd.normal(size=img.shape)
result_sigma = np.array([[0, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 1, 1]], np.int) + 1
[0, 0, 0, 1, 1, 1]], np.int)
seg_sigma = slic(img, n_segments=2, sigma=[1, 50, 1], multichannel=False)
assert_equal(seg_sigma, result_sigma)
@@ -106,9 +106,9 @@ 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)
result_spaced = np.array([[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1]], np.int) + 1
[1, 1, 1, 1, 1]], np.int)
img += 0.1 * rnd.normal(size=img.shape)
seg_non_spaced = slic(img, n_segments=2, sigma=0, multichannel=False,
compactness=1.0)
@@ -136,13 +136,13 @@ def test_enforce_connectivity():
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_connected = np.array([[0, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 1, 1]], 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)
result_disconnected = np.array([[0, 0, 0, 1, 1, 1],
[1, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 0]], np.float)
assert_equal(segments_connected, result_connected)
assert_equal(segments_disconnected, result_disconnected)