scikit-image

Форк
0
180 строк · 6.1 Кб
1
import numpy as np
2
import pytest
3
from numpy.testing import assert_almost_equal, assert_equal
4

5
from skimage import data
6
from skimage._shared.testing import run_in_parallel, xfail, arch32
7
from skimage.feature import ORB
8
from skimage.util.dtype import _convert
9

10

11
img = data.coins()
12

13

14
@run_in_parallel()
15
@pytest.mark.parametrize('dtype', ['float32', 'float64', 'uint8', 'uint16', 'int64'])
16
def test_keypoints_orb_desired_no_of_keypoints(dtype):
17
    _img = _convert(img, dtype)
18
    detector_extractor = ORB(n_keypoints=10, fast_n=12, fast_threshold=0.20)
19
    detector_extractor.detect(_img)
20

21
    exp_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
    )
24
    exp_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

28
    exp_scales = np.array([1, 1, 1.44, 1, 1.728, 1, 1, 1, 1.2, 1])
29

30
    exp_orientations = np.array(
31
        [
32
            -53.97446153,
33
            59.5055285,
34
            -96.01885186,
35
            -149.70789506,
36
            -94.70171899,
37
            -45.76429535,
38
            -51.49752849,
39
            113.57081195,
40
            63.30428063,
41
            -79.56091118,
42
        ]
43
    )
44
    exp_response = np.array(
45
        [
46
            1.01168357,
47
            0.82934145,
48
            0.67784179,
49
            0.57176438,
50
            0.56637459,
51
            0.52248355,
52
            0.43696175,
53
            0.42992376,
54
            0.37700486,
55
            0.36126832,
56
        ]
57
    )
58

59
    if np.dtype(dtype) == np.float32:
60
        assert detector_extractor.scales.dtype == np.float32
61
        assert detector_extractor.responses.dtype == np.float32
62
        assert detector_extractor.orientations.dtype == np.float32
63
    else:
64
        assert detector_extractor.scales.dtype == np.float64
65
        assert detector_extractor.responses.dtype == np.float64
66
        assert detector_extractor.orientations.dtype == np.float64
67

68
    assert_almost_equal(exp_rows, detector_extractor.keypoints[:, 0])
69
    assert_almost_equal(exp_cols, detector_extractor.keypoints[:, 1])
70
    assert_almost_equal(exp_scales, detector_extractor.scales)
71
    assert_almost_equal(exp_response, detector_extractor.responses, 5)
72
    assert_almost_equal(
73
        exp_orientations, np.rad2deg(detector_extractor.orientations), 4
74
    )
75

76
    detector_extractor.detect_and_extract(img)
77
    assert_almost_equal(exp_rows, detector_extractor.keypoints[:, 0])
78
    assert_almost_equal(exp_cols, detector_extractor.keypoints[:, 1])
79

80

81
@pytest.mark.parametrize('dtype', ['float32', 'float64', 'uint8', 'uint16', 'int64'])
82
def test_keypoints_orb_less_than_desired_no_of_keypoints(dtype):
83
    _img = _convert(img, dtype)
84
    detector_extractor = ORB(
85
        n_keypoints=15, fast_n=12, fast_threshold=0.33, downscale=2, n_scales=2
86
    )
87
    detector_extractor.detect(_img)
88

89
    exp_rows = np.array([108.0, 203.0, 140.0, 65.0, 58.0])
90
    exp_cols = np.array([293.0, 267.0, 202.0, 130.0, 291.0])
91

92
    exp_scales = np.array([1.0, 1.0, 1.0, 1.0, 1.0])
93

94
    exp_orientations = np.array(
95
        [151.93906, -56.90052, -79.46341, -59.42996, -158.26941]
96
    )
97

98
    exp_response = np.array([-0.1764169, 0.2652126, -0.0324343, 0.0400902, 0.2667641])
99

100
    assert_almost_equal(exp_rows, detector_extractor.keypoints[:, 0])
101
    assert_almost_equal(exp_cols, detector_extractor.keypoints[:, 1])
102
    assert_almost_equal(exp_scales, detector_extractor.scales)
103
    assert_almost_equal(exp_response, detector_extractor.responses)
104
    assert_almost_equal(
105
        exp_orientations, np.rad2deg(detector_extractor.orientations), 3
106
    )
107

108
    detector_extractor.detect_and_extract(img)
109
    assert_almost_equal(exp_rows, detector_extractor.keypoints[:, 0])
110
    assert_almost_equal(exp_cols, detector_extractor.keypoints[:, 1])
111

112

113
@xfail(
114
    condition=arch32,
115
    reason=(
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
)
122
def test_descriptor_orb():
123
    detector_extractor = ORB(fast_n=12, fast_threshold=0.20)
124
    exp_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
        ],
147
        dtype=bool,
148
    )
149

150
    detector_extractor.detect(img)
151
    detector_extractor.extract(
152
        img,
153
        detector_extractor.keypoints,
154
        detector_extractor.scales,
155
        detector_extractor.orientations,
156
    )
157

158
    assert_equal(exp_descriptors, detector_extractor.descriptors[100:120, 10:20])
159

160
    detector_extractor.detect_and_extract(img)
161
    assert_equal(exp_descriptors, detector_extractor.descriptors[100:120, 10:20])
162
    keypoints_count = detector_extractor.keypoints.shape[0]
163
    assert keypoints_count == detector_extractor.descriptors.shape[0]
164
    assert keypoints_count == detector_extractor.orientations.shape[0]
165
    assert keypoints_count == detector_extractor.responses.shape[0]
166
    assert keypoints_count == detector_extractor.scales.shape[0]
167

168

169
def test_no_descriptors_extracted_orb():
170
    img = np.ones((128, 128))
171
    detector_extractor = ORB()
172
    with pytest.raises(RuntimeError):
173
        detector_extractor.detect_and_extract(img)
174

175

176
def test_img_too_small_orb():
177
    img = data.brick()[:64, :64]
178
    detector_extractor = ORB(downscale=2, n_scales=8)
179
    detector_extractor.detect(img)
180
    detector_extractor.detect_and_extract(img)
181

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

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

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

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