scikit-image

Форк
0
103 строки · 3.3 Кб
1
import numpy as np
2
import pytest
3
from numpy import sqrt, ceil
4
from numpy.testing import assert_almost_equal
5

6
from skimage import data
7
from skimage import img_as_float
8
from skimage.feature import daisy
9

10

11
def test_daisy_color_image_unsupported_error():
12
    img = np.zeros((20, 20, 3))
13
    with pytest.raises(ValueError):
14
        daisy(img)
15

16

17
def test_daisy_desc_dims():
18
    img = img_as_float(data.astronaut()[:128, :128].mean(axis=2))
19
    rings = 2
20
    histograms = 4
21
    orientations = 3
22
    descs = daisy(img, rings=rings, histograms=histograms, orientations=orientations)
23
    assert descs.shape[2] == (rings * histograms + 1) * orientations
24

25
    rings = 4
26
    histograms = 5
27
    orientations = 13
28
    descs = daisy(img, rings=rings, histograms=histograms, orientations=orientations)
29
    assert descs.shape[2] == (rings * histograms + 1) * orientations
30

31

32
def test_descs_shape():
33
    img = img_as_float(data.astronaut()[:256, :256].mean(axis=2))
34
    radius = 20
35
    step = 8
36
    descs = daisy(img, radius=radius, step=step)
37
    assert descs.shape[0] == ceil((img.shape[0] - radius * 2) / float(step))
38
    assert descs.shape[1] == ceil((img.shape[1] - radius * 2) / float(step))
39

40
    img = img[:-1, :-2]
41
    radius = 5
42
    step = 3
43
    descs = daisy(img, radius=radius, step=step)
44
    assert descs.shape[0] == ceil((img.shape[0] - radius * 2) / float(step))
45
    assert descs.shape[1] == ceil((img.shape[1] - radius * 2) / float(step))
46

47

48
@pytest.mark.parametrize('dtype', [np.float32, np.float64])
49
def test_daisy_sigmas_and_radii(dtype):
50
    img = data.astronaut()[:64, :64].mean(axis=2).astype(dtype, copy=False)
51
    sigmas = [1, 2, 3]
52
    radii = [1, 2]
53
    descs = daisy(img, sigmas=sigmas, ring_radii=radii)
54
    assert descs.dtype == img.dtype
55

56

57
def test_daisy_incompatible_sigmas_and_radii():
58
    img = img_as_float(data.astronaut()[:64, :64].mean(axis=2))
59
    sigmas = [1, 2]
60
    radii = [1, 2]
61
    with pytest.raises(ValueError):
62
        daisy(img, sigmas=sigmas, ring_radii=radii)
63

64

65
def test_daisy_normalization():
66
    img = img_as_float(data.astronaut()[:64, :64].mean(axis=2))
67

68
    descs = daisy(img, normalization='l1')
69
    for i in range(descs.shape[0]):
70
        for j in range(descs.shape[1]):
71
            assert_almost_equal(np.sum(descs[i, j, :]), 1)
72
    descs_ = daisy(img)
73
    assert_almost_equal(descs, descs_)
74

75
    descs = daisy(img, normalization='l2')
76
    for i in range(descs.shape[0]):
77
        for j in range(descs.shape[1]):
78
            assert_almost_equal(sqrt(np.sum(descs[i, j, :] ** 2)), 1)
79

80
    orientations = 8
81
    descs = daisy(img, orientations=orientations, normalization='daisy')
82
    desc_dims = descs.shape[2]
83
    for i in range(descs.shape[0]):
84
        for j in range(descs.shape[1]):
85
            for k in range(0, desc_dims, orientations):
86
                assert_almost_equal(
87
                    sqrt(np.sum(descs[i, j, k : k + orientations] ** 2)), 1
88
                )
89

90
    img = np.zeros((50, 50))
91
    descs = daisy(img, normalization='off')
92
    for i in range(descs.shape[0]):
93
        for j in range(descs.shape[1]):
94
            assert_almost_equal(np.sum(descs[i, j, :]), 0)
95

96
    with pytest.raises(ValueError):
97
        daisy(img, normalization='does_not_exist')
98

99

100
def test_daisy_visualization():
101
    img = img_as_float(data.astronaut()[:32, :32].mean(axis=2))
102
    descs, descs_img = daisy(img, visualize=True)
103
    assert descs_img.shape == (32, 32, 3)
104

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

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

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

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