1import numpy as np
2from skimage import data
3from skimage.segmentation import felzenszwalb
4
5from skimage._shared import testing
6from skimage._shared.testing import (assert_greater, test_parallel,
7                                     assert_equal, assert_array_equal,
8                                     assert_warns, assert_no_warnings,
9                                     expected_warnings)
10
11
12@test_parallel()
13def test_grey():
14    # very weak tests.
15    img = np.zeros((20, 21))
16    img[:10, 10:] = 0.2
17    img[10:, :10] = 0.4
18    img[10:, 10:] = 0.6
19    seg = felzenszwalb(img, sigma=0)
20    # we expect 4 segments:
21    assert_equal(len(np.unique(seg)), 4)
22    # that mostly respect the 4 regions:
23    for i in range(4):
24        hist = np.histogram(img[seg == i], bins=[0, 0.1, 0.3, 0.5, 1])[0]
25        assert_greater(hist[i], 40)
26
27
28def test_minsize():
29    # single-channel:
30    img = data.coins()[20:168, 0:128]
31    for min_size in np.arange(10, 100, 10):
32        segments = felzenszwalb(img, min_size=min_size, sigma=3)
33        counts = np.bincount(segments.ravel())
34        # actually want to test greater or equal.
35        assert_greater(counts.min() + 1, min_size)
36    # multi-channel:
37    coffee = data.coffee()[::4, ::4]
38    for min_size in np.arange(10, 100, 10):
39        segments = felzenszwalb(coffee, min_size=min_size, sigma=3)
40        counts = np.bincount(segments.ravel())
41        # actually want to test greater or equal.
42        assert_greater(counts.min() + 1, min_size)
43
44
45@testing.parametrize('channel_axis', [0, -1])
46def test_3D(channel_axis):
47    grey_img = np.zeros((10, 10))
48    rgb_img = np.zeros((10, 10, 3))
49    three_d_img = np.zeros((10, 10, 10))
50
51    rgb_img = np.moveaxis(rgb_img, -1, channel_axis)
52    with assert_no_warnings():
53        felzenszwalb(grey_img, channel_axis=-1)
54        felzenszwalb(grey_img, channel_axis=None)
55        felzenszwalb(rgb_img, channel_axis=channel_axis)
56    with assert_warns(RuntimeWarning):
57        felzenszwalb(three_d_img, channel_axis=channel_axis)
58    with testing.raises(ValueError):
59        felzenszwalb(rgb_img, channel_axis=None)
60        felzenszwalb(three_d_img, channel_axis=None)
61
62
63def test_3D_multichannel_deprecation():
64    rgb_img = np.zeros((10, 10, 3))
65    with expected_warnings(["`multichannel` is a deprecated argument"]):
66        felzenszwalb(rgb_img, multichannel=True)
67    with expected_warnings(["Providing the `multichannel` argument"]):
68        felzenszwalb(rgb_img, 1, 0.8, 2, True)
69
70
71def test_color():
72    # very weak tests.
73    img = np.zeros((20, 21, 3))
74    img[:10, :10, 0] = 1
75    img[10:, :10, 1] = 1
76    img[10:, 10:, 2] = 1
77    seg = felzenszwalb(img, sigma=0)
78    # we expect 4 segments:
79    assert_equal(len(np.unique(seg)), 4)
80    assert_array_equal(seg[:10, :10], 0)
81    assert_array_equal(seg[10:, :10], 2)
82    assert_array_equal(seg[:10, 10:], 1)
83    assert_array_equal(seg[10:, 10:], 3)
84
85
86def test_merging():
87    # test region merging in the post-processing step
88    img = np.array([[0, 0.3], [0.7, 1]])
89    # With scale=0, only the post-processing is performed.
90    seg = felzenszwalb(img, scale=0, sigma=0, min_size=2)
91    # we expect 2 segments:
92    assert_equal(len(np.unique(seg)), 2)
93    assert_array_equal(seg[0, :], 0)
94    assert_array_equal(seg[1, :], 1)
95