Added a test for slic_zero and some other changes based on feedback from Github.

This commit is contained in:
Michal Romaniuk
2014-01-26 18:01:59 +00:00
parent 0c4adddf81
commit ee3ca829c6
2 changed files with 27 additions and 5 deletions
+4 -5
View File
@@ -16,8 +16,7 @@ def _slic_cython(double[:, :, :, ::1] image_zyx,
float step,
Py_ssize_t max_iter,
double[::1] spacing,
bint slic_zero
):
bint slic_zero):
"""Helper function for SLIC segmentation.
Parameters
@@ -99,8 +98,8 @@ def _slic_cython(double[:, :, :, ::1] image_zyx,
cdef double[::1] max_dist_color = np.ones(n_segments, dtype=np.double)
cdef double dist_color
# The reference implementation calls this invxywt
cdef double zyx_wt = float(1) / (step ** 2)
# The reference implementation (Achanta et al.) calls this invxywt
cdef double spatial_weight = float(1) / (step ** 2)
for i in range(max_iter):
change = 0
@@ -127,7 +126,7 @@ def _slic_cython(double[:, :, :, ::1] image_zyx,
for y in range(y_min, y_max):
dy = (sy * (cy - y)) ** 2
for x in range(x_min, x_max):
dist_center = (dz + dy + (sx * (cx - x)) ** 2) * zyx_wt
dist_center = (dz + dy + (sx * (cx - x)) ** 2) * spatial_weight
dist_color = 0
for c in range(3, n_features):
dist_color += (image_zyx[z, y, x, c - 3]
+23
View File
@@ -148,6 +148,29 @@ def test_enforce_connectivity():
assert_equal(segments_disconnected, result_disconnected)
def test_slic_zero():
# Same as test_color_2d but with slic_zero=True
rnd = np.random.RandomState(0)
img = np.zeros((20, 21, 3))
img[:10, :10, 0] = 1
img[10:, :10, 1] = 1
img[10:, 10:, 2] = 1
img += 0.01 * rnd.normal(size=img.shape)
img[img > 1] = 1
img[img < 0] = 0
with warnings.catch_warnings():
warnings.simplefilter("ignore")
seg = slic(img, n_segments=4, sigma=0, slic_zero=True)
# we expect 4 segments
assert_equal(len(np.unique(seg)), 4)
assert_equal(seg.shape, img.shape[:-1])
assert_equal(seg[:10, :10], 0)
assert_equal(seg[10:, :10], 2)
assert_equal(seg[:10, 10:], 1)
assert_equal(seg[10:, 10:], 3)
if __name__ == '__main__':
from numpy import testing