scikit-image

Форк
0
252 строки · 7.8 Кб
1
import numpy as np
2

3
from skimage.measure import label
4
import skimage.measure._ccomp as ccomp
5

6
from skimage._shared import testing
7
from skimage._shared.testing import assert_array_equal
8

9
BG = 0  # background value
10

11

12
class TestConnectedComponents:
13
    def setup_method(self):
14
        self.x = np.array(
15
            [
16
                [0, 0, 3, 2, 1, 9],
17
                [0, 1, 1, 9, 2, 9],
18
                [0, 0, 1, 9, 9, 9],
19
                [3, 1, 1, 5, 3, 0],
20
            ]
21
        )
22

23
        self.labels = np.array(
24
            [
25
                [0, 0, 1, 2, 3, 4],
26
                [0, 5, 5, 4, 2, 4],
27
                [0, 0, 5, 4, 4, 4],
28
                [6, 5, 5, 7, 8, 0],
29
            ]
30
        )
31

32
        # No background - there is no label 0, instead, labelling starts with 1
33
        # and all labels are incremented by 1.
34
        self.labels_nobg = self.labels + 1
35
        # The 0 at lower right corner is isolated, so it should get a new label
36
        self.labels_nobg[-1, -1] = 10
37

38
        # We say that background value is 9 (and bg label is 0)
39
        self.labels_bg_9 = self.labels_nobg.copy()
40
        self.labels_bg_9[self.x == 9] = 0
41
        # Then, where there was the label 5, we now expect 4 etc.
42
        # (we assume that the label of value 9 would normally be 5)
43
        self.labels_bg_9[self.labels_bg_9 > 5] -= 1
44

45
    def test_basic(self):
46
        assert_array_equal(label(self.x), self.labels)
47

48
        # Make sure data wasn't modified
49
        assert self.x[0, 2] == 3
50

51
        # Check that everything works if there is no background
52
        assert_array_equal(label(self.x, background=99), self.labels_nobg)
53
        # Check that everything works if background value != 0
54
        assert_array_equal(label(self.x, background=9), self.labels_bg_9)
55

56
    def test_random(self):
57
        x = (np.random.rand(20, 30) * 5).astype(int)
58
        labels = label(x)
59

60
        n = labels.max()
61
        for i in range(n):
62
            values = x[labels == i]
63
            assert np.all(values == values[0])
64

65
    def test_diag(self):
66
        x = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]])
67
        assert_array_equal(label(x), x)
68

69
    def test_4_vs_8(self):
70
        x = np.array([[0, 1], [1, 0]], dtype=int)
71

72
        assert_array_equal(label(x, connectivity=1), [[0, 1], [2, 0]])
73
        assert_array_equal(label(x, connectivity=2), [[0, 1], [1, 0]])
74

75
    def test_background(self):
76
        x = np.array([[1, 0, 0], [1, 1, 5], [0, 0, 0]])
77

78
        assert_array_equal(label(x), [[1, 0, 0], [1, 1, 2], [0, 0, 0]])
79

80
        assert_array_equal(label(x, background=0), [[1, 0, 0], [1, 1, 2], [0, 0, 0]])
81

82
    def test_background_two_regions(self):
83
        x = np.array([[0, 0, 6], [0, 0, 6], [5, 5, 5]])
84

85
        res = label(x, background=0)
86
        assert_array_equal(res, [[0, 0, 1], [0, 0, 1], [2, 2, 2]])
87

88
    def test_background_one_region_center(self):
89
        x = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]])
90

91
        assert_array_equal(
92
            label(x, connectivity=1, background=0), [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
93
        )
94

95
    def test_return_num(self):
96
        x = np.array([[1, 0, 6], [0, 0, 6], [5, 5, 5]])
97

98
        assert_array_equal(label(x, return_num=True)[1], 3)
99

100
        assert_array_equal(label(x, background=-1, return_num=True)[1], 4)
101

102

103
class TestConnectedComponents3d:
104
    def setup_method(self):
105
        self.x = np.zeros((3, 4, 5), int)
106
        self.x[0] = np.array(
107
            [[0, 3, 2, 1, 9], [0, 1, 9, 2, 9], [0, 1, 9, 9, 9], [3, 1, 5, 3, 0]]
108
        )
109

110
        self.x[1] = np.array(
111
            [[3, 3, 2, 1, 9], [0, 3, 9, 2, 1], [0, 3, 3, 1, 1], [3, 1, 3, 3, 0]]
112
        )
113

114
        self.x[2] = np.array(
115
            [[3, 3, 8, 8, 0], [2, 3, 9, 8, 8], [2, 3, 0, 8, 0], [2, 1, 0, 0, 0]]
116
        )
117

118
        self.labels = np.zeros((3, 4, 5), int)
119

120
        self.labels[0] = np.array(
121
            [[0, 1, 2, 3, 4], [0, 5, 4, 2, 4], [0, 5, 4, 4, 4], [1, 5, 6, 1, 0]]
122
        )
123

124
        self.labels[1] = np.array(
125
            [[1, 1, 2, 3, 4], [0, 1, 4, 2, 3], [0, 1, 1, 3, 3], [1, 5, 1, 1, 0]]
126
        )
127

128
        self.labels[2] = np.array(
129
            [[1, 1, 7, 7, 0], [8, 1, 4, 7, 7], [8, 1, 0, 7, 0], [8, 5, 0, 0, 0]]
130
        )
131

132
    def test_basic(self):
133
        labels = label(self.x)
134
        assert_array_equal(labels, self.labels)
135

136
        assert self.x[0, 0, 2] == 2, "Data was modified!"
137

138
    def test_random(self):
139
        x = (np.random.rand(20, 30) * 5).astype(int)
140
        labels = label(x)
141

142
        n = labels.max()
143
        for i in range(n):
144
            values = x[labels == i]
145
            assert np.all(values == values[0])
146

147
    def test_diag(self):
148
        x = np.zeros((3, 3, 3), int)
149
        x[0, 2, 2] = 1
150
        x[1, 1, 1] = 1
151
        x[2, 0, 0] = 1
152
        assert_array_equal(label(x), x)
153

154
    def test_4_vs_8(self):
155
        x = np.zeros((2, 2, 2), int)
156
        x[0, 1, 1] = 1
157
        x[1, 0, 0] = 1
158
        label4 = x.copy()
159
        label4[1, 0, 0] = 2
160
        assert_array_equal(label(x, connectivity=1), label4)
161
        assert_array_equal(label(x, connectivity=3), x)
162

163
    def test_connectivity_1_vs_2(self):
164
        x = np.zeros((2, 2, 2), int)
165
        x[0, 1, 1] = 1
166
        x[1, 0, 0] = 1
167
        label1 = x.copy()
168
        label1[1, 0, 0] = 2
169
        assert_array_equal(label(x, connectivity=1), label1)
170
        assert_array_equal(label(x, connectivity=3), x)
171

172
    def test_background(self):
173
        x = np.zeros((2, 3, 3), int)
174
        x[0] = np.array([[1, 0, 0], [1, 0, 0], [0, 0, 0]])
175
        x[1] = np.array([[0, 0, 0], [0, 1, 5], [0, 0, 0]])
176

177
        lnb = x.copy()
178
        lnb[0] = np.array([[1, 2, 2], [1, 2, 2], [2, 2, 2]])
179
        lnb[1] = np.array([[2, 2, 2], [2, 1, 3], [2, 2, 2]])
180
        lb = x.copy()
181
        lb[0] = np.array([[1, BG, BG], [1, BG, BG], [BG, BG, BG]])
182
        lb[1] = np.array([[BG, BG, BG], [BG, 1, 2], [BG, BG, BG]])
183

184
        assert_array_equal(label(x), lb)
185
        assert_array_equal(label(x, background=-1), lnb)
186

187
    def test_background_two_regions(self):
188
        x = np.zeros((2, 3, 3), int)
189
        x[0] = np.array([[0, 0, 6], [0, 0, 6], [5, 5, 5]])
190
        x[1] = np.array([[6, 6, 0], [5, 0, 0], [0, 0, 0]])
191
        lb = x.copy()
192
        lb[0] = np.array([[BG, BG, 1], [BG, BG, 1], [2, 2, 2]])
193
        lb[1] = np.array([[1, 1, BG], [2, BG, BG], [BG, BG, BG]])
194

195
        res = label(x, background=0)
196
        assert_array_equal(res, lb)
197

198
    def test_background_one_region_center(self):
199
        x = np.zeros((3, 3, 3), int)
200
        x[1, 1, 1] = 1
201

202
        lb = np.ones_like(x) * BG
203
        lb[1, 1, 1] = 1
204

205
        assert_array_equal(label(x, connectivity=1, background=0), lb)
206

207
    def test_return_num(self):
208
        x = np.array([[1, 0, 6], [0, 0, 6], [5, 5, 5]])
209

210
        assert_array_equal(label(x, return_num=True)[1], 3)
211
        assert_array_equal(label(x, background=-1, return_num=True)[1], 4)
212

213
    def test_1D(self):
214
        x = np.array((0, 1, 2, 2, 1, 1, 0, 0))
215
        xlen = len(x)
216
        y = np.array((0, 1, 2, 2, 3, 3, 0, 0))
217
        reshapes = (
218
            (xlen,),
219
            (1, xlen),
220
            (xlen, 1),
221
            (1, xlen, 1),
222
            (xlen, 1, 1),
223
            (1, 1, xlen),
224
        )
225
        for reshape in reshapes:
226
            x2 = x.reshape(reshape)
227
            labelled = label(x2)
228
            assert_array_equal(y, labelled.flatten())
229

230
    def test_nd(self):
231
        x = np.ones((1, 2, 3, 4))
232
        with testing.raises(NotImplementedError):
233
            label(x)
234

235

236
class TestSupport:
237
    def test_reshape(self):
238
        shapes_in = ((3, 1, 2), (1, 4, 5), (3, 1, 1), (2, 1), (1,))
239
        for shape in shapes_in:
240
            shape = np.array(shape)
241
            numones = sum(shape == 1)
242
            inp = np.random.random(shape)
243

244
            fixed, swaps = ccomp.reshape_array(inp)
245
            shape2 = fixed.shape
246
            # now check that all ones are at the beginning
247
            for i in range(numones):
248
                assert shape2[i] == 1
249

250
            back = ccomp.undo_reshape_array(fixed, swaps)
251
            # check that the undo works as expected
252
            assert_array_equal(inp, back)
253

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.