From 12c173908f921b80415d1992c04f03545d87d050 Mon Sep 17 00:00:00 2001 From: Gael Varoquaux Date: Fri, 20 Nov 2015 15:41:01 +0100 Subject: [PATCH 1/3] BUX: in _slic.pyx slice can has steps=None int(None) fails, and slice.steps == None is valid. It is equivalent to 1. --- skimage/segmentation/_slic.pyx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/skimage/segmentation/_slic.pyx b/skimage/segmentation/_slic.pyx index 19fc1627..bde81749 100644 --- a/skimage/segmentation/_slic.pyx +++ b/skimage/segmentation/_slic.pyx @@ -76,7 +76,8 @@ def _slic_cython(double[:, :, :, ::1] image_zyx, # approximate grid size for desired n_segments cdef Py_ssize_t step_z, step_y, step_x slices = regular_grid((depth, height, width), n_segments) - step_z, step_y, step_x = [int(s.step) for s in slices] + step_z, step_y, step_x = [int(s.step if s.step is not None else 1) + for s in slices] cdef Py_ssize_t[:, :, ::1] nearest_segments \ = np.empty((depth, height, width), dtype=np.intp) From bb64e8cfcb4f4b93e5d9cf82d5dd4bebfc31d162 Mon Sep 17 00:00:00 2001 From: emmanuelle Date: Mon, 7 Dec 2015 22:55:20 +0100 Subject: [PATCH 2/3] Added test for slic segmentation: case where more segments than pixels are asked for. --- skimage/segmentation/slic_superpixels.py | 3 ++- skimage/segmentation/tests/test_slic.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/skimage/segmentation/slic_superpixels.py b/skimage/segmentation/slic_superpixels.py index 3eca9401..adc21a65 100644 --- a/skimage/segmentation/slic_superpixels.py +++ b/skimage/segmentation/slic_superpixels.py @@ -155,7 +155,8 @@ def slic(image, n_segments=100, compactness=10., max_iter=10, sigma=0, # initialize cluster centroids for desired number of segments grid_z, grid_y, grid_x = np.mgrid[:depth, :height, :width] slices = regular_grid(image.shape[:3], n_segments) - step_z, step_y, step_x = [int(s.step) for s in slices] + step_z, step_y, step_x = [int(s.step if s.step is not None else 1) + for s in slices] segments_z = grid_z[slices] segments_y = grid_y[slices] segments_x = grid_x[slices] diff --git a/skimage/segmentation/tests/test_slic.py b/skimage/segmentation/tests/test_slic.py index 73629103..4c05d72b 100644 --- a/skimage/segmentation/tests/test_slic.py +++ b/skimage/segmentation/tests/test_slic.py @@ -195,6 +195,19 @@ def test_slic_zero(): assert_equal(seg[10:, 10:], 3) +def test_more_segments_than_pixels(): + rnd = np.random.RandomState(0) + img = np.zeros((20, 21)) + img[:10, :10] = 0.33 + img[10:, :10] = 0.67 + img[10:, 10:] = 1.00 + img += 0.0033 * rnd.normal(size=img.shape) + img[img > 1] = 1 + img[img < 0] = 0 + seg = slic(img, sigma=0, n_segments=500, compactness=1, + multichannel=False, convert2lab=False) + assert np.all(seg.ravel() == np.arange(seg.size)) + if __name__ == '__main__': from numpy import testing From bff2ce0bc415ba3f8695b074b3b9aa9aeef39fa4 Mon Sep 17 00:00:00 2001 From: emmanuelle Date: Mon, 7 Dec 2015 22:59:16 +0100 Subject: [PATCH 3/3] Missing line --- skimage/segmentation/tests/test_slic.py | 1 + 1 file changed, 1 insertion(+) diff --git a/skimage/segmentation/tests/test_slic.py b/skimage/segmentation/tests/test_slic.py index 4c05d72b..83424543 100644 --- a/skimage/segmentation/tests/test_slic.py +++ b/skimage/segmentation/tests/test_slic.py @@ -208,6 +208,7 @@ def test_more_segments_than_pixels(): multichannel=False, convert2lab=False) assert np.all(seg.ravel() == np.arange(seg.size)) + if __name__ == '__main__': from numpy import testing