scikit-image
180 строк · 6.1 Кб
1import numpy as np
2import pytest
3from numpy.testing import assert_almost_equal, assert_equal
4
5from skimage import data
6from skimage._shared.testing import run_in_parallel, xfail, arch32
7from skimage.feature import ORB
8from skimage.util.dtype import _convert
9
10
11img = data.coins()
12
13
14@run_in_parallel()
15@pytest.mark.parametrize('dtype', ['float32', 'float64', 'uint8', 'uint16', 'int64'])
16def test_keypoints_orb_desired_no_of_keypoints(dtype):
17_img = _convert(img, dtype)
18detector_extractor = ORB(n_keypoints=10, fast_n=12, fast_threshold=0.20)
19detector_extractor.detect(_img)
20
21exp_rows = np.array(
22[141.0, 108.0, 214.56, 131.0, 214.272, 67.0, 206.0, 177.0, 108.0, 141.0]
23)
24exp_cols = np.array(
25[323.0, 328.0, 282.24, 292.0, 281.664, 85.0, 260.0, 284.0, 328.8, 267.0]
26)
27
28exp_scales = np.array([1, 1, 1.44, 1, 1.728, 1, 1, 1, 1.2, 1])
29
30exp_orientations = np.array(
31[
32-53.97446153,
3359.5055285,
34-96.01885186,
35-149.70789506,
36-94.70171899,
37-45.76429535,
38-51.49752849,
39113.57081195,
4063.30428063,
41-79.56091118,
42]
43)
44exp_response = np.array(
45[
461.01168357,
470.82934145,
480.67784179,
490.57176438,
500.56637459,
510.52248355,
520.43696175,
530.42992376,
540.37700486,
550.36126832,
56]
57)
58
59if np.dtype(dtype) == np.float32:
60assert detector_extractor.scales.dtype == np.float32
61assert detector_extractor.responses.dtype == np.float32
62assert detector_extractor.orientations.dtype == np.float32
63else:
64assert detector_extractor.scales.dtype == np.float64
65assert detector_extractor.responses.dtype == np.float64
66assert detector_extractor.orientations.dtype == np.float64
67
68assert_almost_equal(exp_rows, detector_extractor.keypoints[:, 0])
69assert_almost_equal(exp_cols, detector_extractor.keypoints[:, 1])
70assert_almost_equal(exp_scales, detector_extractor.scales)
71assert_almost_equal(exp_response, detector_extractor.responses, 5)
72assert_almost_equal(
73exp_orientations, np.rad2deg(detector_extractor.orientations), 4
74)
75
76detector_extractor.detect_and_extract(img)
77assert_almost_equal(exp_rows, detector_extractor.keypoints[:, 0])
78assert_almost_equal(exp_cols, detector_extractor.keypoints[:, 1])
79
80
81@pytest.mark.parametrize('dtype', ['float32', 'float64', 'uint8', 'uint16', 'int64'])
82def test_keypoints_orb_less_than_desired_no_of_keypoints(dtype):
83_img = _convert(img, dtype)
84detector_extractor = ORB(
85n_keypoints=15, fast_n=12, fast_threshold=0.33, downscale=2, n_scales=2
86)
87detector_extractor.detect(_img)
88
89exp_rows = np.array([108.0, 203.0, 140.0, 65.0, 58.0])
90exp_cols = np.array([293.0, 267.0, 202.0, 130.0, 291.0])
91
92exp_scales = np.array([1.0, 1.0, 1.0, 1.0, 1.0])
93
94exp_orientations = np.array(
95[151.93906, -56.90052, -79.46341, -59.42996, -158.26941]
96)
97
98exp_response = np.array([-0.1764169, 0.2652126, -0.0324343, 0.0400902, 0.2667641])
99
100assert_almost_equal(exp_rows, detector_extractor.keypoints[:, 0])
101assert_almost_equal(exp_cols, detector_extractor.keypoints[:, 1])
102assert_almost_equal(exp_scales, detector_extractor.scales)
103assert_almost_equal(exp_response, detector_extractor.responses)
104assert_almost_equal(
105exp_orientations, np.rad2deg(detector_extractor.orientations), 3
106)
107
108detector_extractor.detect_and_extract(img)
109assert_almost_equal(exp_rows, detector_extractor.keypoints[:, 0])
110assert_almost_equal(exp_cols, detector_extractor.keypoints[:, 1])
111
112
113@xfail(
114condition=arch32,
115reason=(
116'Known test failure on 32-bit platforms. See links for '
117'details: '
118'https://github.com/scikit-image/scikit-image/issues/3091 '
119'https://github.com/scikit-image/scikit-image/issues/2529'
120),
121)
122def test_descriptor_orb():
123detector_extractor = ORB(fast_n=12, fast_threshold=0.20)
124exp_descriptors = np.array(
125[
126[0, 0, 0, 1, 0, 0, 0, 1, 0, 1],
127[1, 1, 0, 1, 0, 0, 0, 1, 0, 1],
128[1, 1, 0, 0, 1, 0, 0, 0, 1, 1],
129[1, 1, 1, 0, 0, 0, 1, 1, 1, 0],
130[0, 0, 0, 1, 0, 1, 1, 1, 1, 1],
131[1, 0, 0, 1, 1, 0, 0, 0, 1, 0],
132[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
133[1, 1, 1, 0, 1, 1, 1, 1, 0, 0],
134[1, 1, 1, 1, 0, 0, 0, 1, 1, 1],
135[0, 1, 1, 0, 0, 1, 1, 0, 1, 1],
136[1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
137[1, 0, 0, 0, 0, 1, 0, 1, 1, 1],
138[1, 0, 1, 1, 1, 0, 1, 0, 1, 0],
139[0, 0, 1, 1, 0, 0, 0, 0, 1, 1],
140[0, 1, 1, 0, 0, 0, 1, 0, 0, 1],
141[0, 1, 1, 0, 0, 0, 1, 1, 1, 1],
142[0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
143[0, 0, 1, 1, 1, 1, 0, 1, 1, 0],
144[0, 0, 1, 1, 1, 0, 1, 0, 0, 1],
145[0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
146],
147dtype=bool,
148)
149
150detector_extractor.detect(img)
151detector_extractor.extract(
152img,
153detector_extractor.keypoints,
154detector_extractor.scales,
155detector_extractor.orientations,
156)
157
158assert_equal(exp_descriptors, detector_extractor.descriptors[100:120, 10:20])
159
160detector_extractor.detect_and_extract(img)
161assert_equal(exp_descriptors, detector_extractor.descriptors[100:120, 10:20])
162keypoints_count = detector_extractor.keypoints.shape[0]
163assert keypoints_count == detector_extractor.descriptors.shape[0]
164assert keypoints_count == detector_extractor.orientations.shape[0]
165assert keypoints_count == detector_extractor.responses.shape[0]
166assert keypoints_count == detector_extractor.scales.shape[0]
167
168
169def test_no_descriptors_extracted_orb():
170img = np.ones((128, 128))
171detector_extractor = ORB()
172with pytest.raises(RuntimeError):
173detector_extractor.detect_and_extract(img)
174
175
176def test_img_too_small_orb():
177img = data.brick()[:64, :64]
178detector_extractor = ORB(downscale=2, n_scales=8)
179detector_extractor.detect(img)
180detector_extractor.detect_and_extract(img)
181