diff --git a/skimage/segmentation/felzenszwalb.py b/skimage/segmentation/felzenszwalb.py index be879e13..54846102 100644 --- a/skimage/segmentation/felzenszwalb.py +++ b/skimage/segmentation/felzenszwalb.py @@ -67,8 +67,8 @@ def felzenszwalb_segmentation(image, scale=1, sigma=0.8): # put pixels in same segment only if in the same segment in all images # we do this by combining the channels to one number - n0 = max(segmentations[0]) - n1 = max(segmentations[1]) + n0 = segmentations[0].max() + 1 + n1 = segmentations[1].max() + 1 hasher = np.array([n1 * n0, n0, 1]) segmentations = np.dstack(segmentations).reshape(-1, n_channels) segmentation = np.dot(segmentations, hasher) diff --git a/skimage/segmentation/tests/test_felzenszwalb.py b/skimage/segmentation/tests/test_felzenszwalb.py new file mode 100644 index 00000000..d645ab9d --- /dev/null +++ b/skimage/segmentation/tests/test_felzenszwalb.py @@ -0,0 +1,39 @@ +import numpy as np +from numpy.testing import assert_equal, assert_array_equal +from nose.tools import assert_greater +from skimage.segmentation import felzenszwalb_segmentation + + +def test_grey(): + # very weak tests. This algorithm is pretty unstable. + img = np.zeros((20, 20)) + img[:10, 10:] = 0.2 + img[10:, :10] = 0.4 + img[10:, 10:] = 0.6 + seg = felzenszwalb_segmentation(img, sigma=0) + # we expect 4 segments: + assert_equal(len(np.unique(seg)), 4) + # that mostly respect the 4 regions: + for i in xrange(4): + hist = np.histogram(img[seg == i], bins=[0, 0.1, 0.3, 0.5, 1])[0] + assert_greater(hist[i], 40) + + +def test_color(): + # very weak tests. This algorithm is pretty unstable. + img = np.zeros((20, 20, 3)) + img[:10, :10, 0] = 1 + img[10:, :10, 1] = 1 + img[10:, 10:, 2] = 1 + seg = felzenszwalb_segmentation(img, sigma=0) + # we expect 4 segments: + assert_equal(len(np.unique(seg)), 4) + assert_array_equal(seg[:10, :10], 0) + assert_array_equal(seg[10:, :10], 3) + assert_array_equal(seg[:10, 10:], 1) + assert_array_equal(seg[10:, 10:], 2) + + +if __name__ == '__main__': + from numpy import testing + testing.run_module_suite()