scikit-image

Форк
0
321 строка · 7.9 Кб
1
import numpy as np
2
from skimage._shared.testing import assert_equal
3
from skimage import data
4
from skimage import transform
5
from skimage.color import rgb2gray
6
from skimage.feature import BRIEF, match_descriptors, corner_peaks, corner_harris
7
from skimage._shared import testing
8

9

10
def test_binary_descriptors_unequal_descriptor_sizes_error():
11
    """Sizes of descriptors of keypoints to be matched should be equal."""
12
    descs1 = np.array([[True, True, False, True], [False, True, False, True]])
13
    descs2 = np.array(
14
        [[True, False, False, True, False], [False, True, True, True, False]]
15
    )
16
    with testing.raises(ValueError):
17
        match_descriptors(descs1, descs2)
18

19

20
def test_binary_descriptors():
21
    descs1 = np.array(
22
        [[True, True, False, True, True], [False, True, False, True, True]]
23
    )
24
    descs2 = np.array(
25
        [[True, False, False, True, False], [False, False, True, True, True]]
26
    )
27
    matches = match_descriptors(descs1, descs2)
28
    assert_equal(matches, [[0, 0], [1, 1]])
29

30

31
def test_binary_descriptors_rotation_crosscheck_false():
32
    """Verify matched keypoints and their corresponding masks results between
33
    image and its rotated version with the expected keypoint pairs with
34
    cross_check disabled."""
35
    img = data.astronaut()
36
    img = rgb2gray(img)
37
    tform = transform.SimilarityTransform(scale=1, rotation=0.15, translation=(0, 0))
38
    rotated_img = transform.warp(img, tform, clip=False)
39

40
    extractor = BRIEF(descriptor_size=512)
41

42
    keypoints1 = corner_peaks(
43
        corner_harris(img), min_distance=5, threshold_abs=0, threshold_rel=0.1
44
    )
45
    extractor.extract(img, keypoints1)
46
    descriptors1 = extractor.descriptors
47

48
    keypoints2 = corner_peaks(
49
        corner_harris(rotated_img), min_distance=5, threshold_abs=0, threshold_rel=0.1
50
    )
51
    extractor.extract(rotated_img, keypoints2)
52
    descriptors2 = extractor.descriptors
53

54
    matches = match_descriptors(descriptors1, descriptors2, cross_check=False)
55

56
    exp_matches1 = np.arange(47)
57
    exp_matches2 = np.array(
58
        [
59
            0,
60
            2,
61
            1,
62
            3,
63
            4,
64
            5,
65
            7,
66
            8,
67
            14,
68
            9,
69
            11,
70
            13,
71
            23,
72
            15,
73
            16,
74
            22,
75
            17,
76
            19,
77
            37,
78
            18,
79
            24,
80
            27,
81
            30,
82
            25,
83
            26,
84
            32,
85
            28,
86
            35,
87
            37,
88
            42,
89
            29,
90
            38,
91
            33,
92
            40,
93
            36,
94
            39,
95
            10,
96
            36,
97
            43,
98
            15,
99
            35,
100
            41,
101
            6,
102
            37,
103
            32,
104
            24,
105
            8,
106
        ]
107
    )
108

109
    assert_equal(matches[:, 0], exp_matches1)
110
    assert_equal(matches[:, 1], exp_matches2)
111

112
    # minkowski takes a different code path, therefore we test it explicitly
113
    matches = match_descriptors(
114
        descriptors1, descriptors2, metric='minkowski', cross_check=False
115
    )
116
    assert_equal(matches[:, 0], exp_matches1)
117
    assert_equal(matches[:, 1], exp_matches2)
118

119
    # it also has an extra parameter
120
    matches = match_descriptors(
121
        descriptors1, descriptors2, metric='minkowski', p=4, cross_check=False
122
    )
123
    assert_equal(matches[:, 0], exp_matches1)
124
    assert_equal(matches[:, 1], exp_matches2)
125

126

127
def test_binary_descriptors_rotation_crosscheck_true():
128
    """Verify matched keypoints and their corresponding masks results between
129
    image and its rotated version with the expected keypoint pairs with
130
    cross_check enabled."""
131
    img = data.astronaut()
132
    img = rgb2gray(img)
133
    tform = transform.SimilarityTransform(scale=1, rotation=0.15, translation=(0, 0))
134
    rotated_img = transform.warp(img, tform, clip=False)
135

136
    extractor = BRIEF(descriptor_size=512)
137

138
    keypoints1 = corner_peaks(
139
        corner_harris(img), min_distance=5, threshold_abs=0, threshold_rel=0.1
140
    )
141
    extractor.extract(img, keypoints1)
142
    descriptors1 = extractor.descriptors
143

144
    keypoints2 = corner_peaks(
145
        corner_harris(rotated_img), min_distance=5, threshold_abs=0, threshold_rel=0.1
146
    )
147
    extractor.extract(rotated_img, keypoints2)
148
    descriptors2 = extractor.descriptors
149

150
    matches = match_descriptors(descriptors1, descriptors2, cross_check=True)
151

152
    exp_matches1 = np.array(
153
        [
154
            0,
155
            1,
156
            2,
157
            3,
158
            4,
159
            5,
160
            6,
161
            7,
162
            8,
163
            9,
164
            10,
165
            11,
166
            12,
167
            13,
168
            14,
169
            15,
170
            16,
171
            17,
172
            19,
173
            20,
174
            21,
175
            22,
176
            23,
177
            24,
178
            26,
179
            27,
180
            28,
181
            29,
182
            30,
183
            31,
184
            32,
185
            33,
186
            34,
187
            38,
188
            41,
189
            42,
190
        ]
191
    )
192
    exp_matches2 = np.array(
193
        [
194
            0,
195
            2,
196
            1,
197
            3,
198
            4,
199
            5,
200
            7,
201
            8,
202
            14,
203
            9,
204
            11,
205
            13,
206
            23,
207
            15,
208
            16,
209
            22,
210
            17,
211
            19,
212
            18,
213
            24,
214
            27,
215
            30,
216
            25,
217
            26,
218
            28,
219
            35,
220
            37,
221
            42,
222
            29,
223
            38,
224
            33,
225
            40,
226
            36,
227
            43,
228
            41,
229
            6,
230
        ]
231
    )
232
    assert_equal(matches[:, 0], exp_matches1)
233
    assert_equal(matches[:, 1], exp_matches2)
234

235

236
def test_max_distance():
237
    descs1 = np.zeros((10, 128))
238
    descs2 = np.zeros((15, 128))
239

240
    descs1[0, :] = 1
241

242
    matches = match_descriptors(
243
        descs1, descs2, metric='euclidean', max_distance=0.1, cross_check=False
244
    )
245
    assert len(matches) == 9
246

247
    matches = match_descriptors(
248
        descs1,
249
        descs2,
250
        metric='euclidean',
251
        max_distance=np.sqrt(128.1),
252
        cross_check=False,
253
    )
254
    assert len(matches) == 10
255

256
    matches = match_descriptors(
257
        descs1, descs2, metric='euclidean', max_distance=0.1, cross_check=True
258
    )
259
    assert_equal(matches, [[1, 0]])
260

261
    matches = match_descriptors(
262
        descs1,
263
        descs2,
264
        metric='euclidean',
265
        max_distance=np.sqrt(128.1),
266
        cross_check=True,
267
    )
268
    assert_equal(matches, [[1, 0]])
269

270

271
def test_max_ratio():
272
    descs1 = 10 * np.arange(10)[:, None].astype(np.float32)
273
    descs2 = 10 * np.arange(15)[:, None].astype(np.float32)
274

275
    descs2[0] = 5.0
276

277
    matches = match_descriptors(
278
        descs1, descs2, metric='euclidean', max_ratio=1.0, cross_check=False
279
    )
280
    assert_equal(len(matches), 10)
281

282
    matches = match_descriptors(
283
        descs1, descs2, metric='euclidean', max_ratio=0.6, cross_check=False
284
    )
285
    assert_equal(len(matches), 10)
286

287
    matches = match_descriptors(
288
        descs1, descs2, metric='euclidean', max_ratio=0.5, cross_check=False
289
    )
290
    assert_equal(len(matches), 9)
291

292
    descs1[0] = 7.5
293

294
    matches = match_descriptors(
295
        descs1, descs2, metric='euclidean', max_ratio=0.5, cross_check=False
296
    )
297
    assert_equal(len(matches), 9)
298

299
    descs2 = 10 * np.arange(1)[:, None].astype(np.float32)
300

301
    matches = match_descriptors(
302
        descs1, descs2, metric='euclidean', max_ratio=1.0, cross_check=False
303
    )
304
    assert_equal(len(matches), 10)
305

306
    matches = match_descriptors(
307
        descs1, descs2, metric='euclidean', max_ratio=0.5, cross_check=False
308
    )
309
    assert_equal(len(matches), 10)
310

311
    descs1 = 10 * np.arange(1)[:, None].astype(np.float32)
312

313
    matches = match_descriptors(
314
        descs1, descs2, metric='euclidean', max_ratio=1.0, cross_check=False
315
    )
316
    assert_equal(len(matches), 1)
317

318
    matches = match_descriptors(
319
        descs1, descs2, metric='euclidean', max_ratio=0.5, cross_check=False
320
    )
321
    assert_equal(len(matches), 1)
322

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

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

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

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