scikit-image

Форк
0
110 строк · 3.1 Кб
1
import pytest
2
import copy
3

4
import numpy as np
5

6
from skimage._shared.testing import assert_array_equal
7
from skimage import data
8
from skimage.feature import BRIEF, corner_peaks, corner_harris
9
from skimage._shared import testing
10

11

12
def test_color_image_unsupported_error():
13
    """Brief descriptors can be evaluated on gray-scale images only."""
14
    img = np.zeros((20, 20, 3))
15
    keypoints = np.asarray([[7, 5], [11, 13]])
16
    with testing.raises(ValueError):
17
        BRIEF().extract(img, keypoints)
18

19

20
@pytest.mark.parametrize('dtype', ['float32', 'float64', 'uint8', 'int'])
21
def test_normal_mode(dtype):
22
    """Verify the computed BRIEF descriptors with expected for normal mode."""
23
    img = data.coins().astype(dtype)
24

25
    keypoints = corner_peaks(
26
        corner_harris(img), min_distance=5, threshold_abs=0, threshold_rel=0.1
27
    )
28

29
    extractor = BRIEF(descriptor_size=8, sigma=2)
30

31
    extractor.extract(img, keypoints[:8])
32

33
    expected = np.array(
34
        [
35
            [1, 1, 1, 0, 1, 1, 0, 1],
36
            [0, 1, 1, 0, 1, 1, 0, 0],
37
            [1, 1, 1, 0, 1, 1, 0, 1],
38
            [0, 0, 0, 1, 0, 0, 1, 0],
39
            [0, 1, 1, 0, 1, 1, 0, 0],
40
            [0, 1, 1, 0, 1, 1, 1, 0],
41
            [1, 1, 1, 0, 1, 1, 0, 1],
42
            [1, 0, 1, 0, 0, 1, 1, 0],
43
        ],
44
        dtype=bool,
45
    )
46

47
    assert_array_equal(extractor.descriptors, expected)
48

49

50
@pytest.mark.parametrize('dtype', ['float32', 'float64', 'uint8', 'int'])
51
def test_uniform_mode(dtype):
52
    """Verify the computed BRIEF descriptors with expected for uniform mode."""
53
    img = data.coins().astype(dtype)
54

55
    keypoints = corner_peaks(
56
        corner_harris(img), min_distance=5, threshold_abs=0, threshold_rel=0.1
57
    )
58

59
    extractor = BRIEF(descriptor_size=8, sigma=2, mode='uniform', rng=1)
60
    BRIEF(descriptor_size=8, sigma=2, mode='uniform', rng=1)
61

62
    extractor.extract(img, keypoints[:8])
63

64
    expected = np.array(
65
        [
66
            [0, 1, 0, 1, 0, 1, 1, 0],
67
            [0, 1, 0, 0, 0, 1, 0, 1],
68
            [0, 1, 0, 0, 0, 1, 1, 1],
69
            [1, 0, 1, 0, 1, 0, 1, 1],
70
            [0, 0, 1, 0, 0, 1, 0, 1],
71
            [0, 1, 0, 1, 0, 1, 0, 1],
72
            [0, 1, 0, 0, 0, 1, 1, 1],
73
            [1, 0, 1, 1, 1, 0, 0, 1],
74
        ],
75
        dtype=bool,
76
    )
77

78
    assert_array_equal(extractor.descriptors, expected)
79

80

81
def test_unsupported_mode():
82
    with testing.raises(ValueError):
83
        BRIEF(mode='foobar')
84

85

86
@pytest.mark.parametrize('dtype', ['float32', 'float64', 'uint8', 'int'])
87
def test_border(dtype):
88
    img = np.zeros((100, 100), dtype=dtype)
89
    keypoints = np.array([[1, 1], [20, 20], [50, 50], [80, 80]])
90

91
    extractor = BRIEF(patch_size=41, rng=1)
92
    extractor.extract(img, keypoints)
93

94
    assert extractor.descriptors.shape[0] == 3
95
    assert_array_equal(extractor.mask, (False, True, True, True))
96

97

98
def test_independent_rng():
99
    img = np.zeros((100, 100), dtype=int)
100
    keypoints = np.array([[1, 1], [20, 20], [50, 50], [80, 80]])
101

102
    rng = np.random.default_rng()
103
    extractor = BRIEF(patch_size=41, rng=rng)
104

105
    x = copy.deepcopy(extractor.rng).random()
106
    rng.random()
107
    extractor.extract(img, keypoints)
108
    z = copy.deepcopy(extractor.rng).random()
109

110
    assert x == z
111

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

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

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

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