scikit-image
103 строки · 3.3 Кб
1import numpy as np
2import pytest
3from numpy import sqrt, ceil
4from numpy.testing import assert_almost_equal
5
6from skimage import data
7from skimage import img_as_float
8from skimage.feature import daisy
9
10
11def test_daisy_color_image_unsupported_error():
12img = np.zeros((20, 20, 3))
13with pytest.raises(ValueError):
14daisy(img)
15
16
17def test_daisy_desc_dims():
18img = img_as_float(data.astronaut()[:128, :128].mean(axis=2))
19rings = 2
20histograms = 4
21orientations = 3
22descs = daisy(img, rings=rings, histograms=histograms, orientations=orientations)
23assert descs.shape[2] == (rings * histograms + 1) * orientations
24
25rings = 4
26histograms = 5
27orientations = 13
28descs = daisy(img, rings=rings, histograms=histograms, orientations=orientations)
29assert descs.shape[2] == (rings * histograms + 1) * orientations
30
31
32def test_descs_shape():
33img = img_as_float(data.astronaut()[:256, :256].mean(axis=2))
34radius = 20
35step = 8
36descs = daisy(img, radius=radius, step=step)
37assert descs.shape[0] == ceil((img.shape[0] - radius * 2) / float(step))
38assert descs.shape[1] == ceil((img.shape[1] - radius * 2) / float(step))
39
40img = img[:-1, :-2]
41radius = 5
42step = 3
43descs = daisy(img, radius=radius, step=step)
44assert descs.shape[0] == ceil((img.shape[0] - radius * 2) / float(step))
45assert descs.shape[1] == ceil((img.shape[1] - radius * 2) / float(step))
46
47
48@pytest.mark.parametrize('dtype', [np.float32, np.float64])
49def test_daisy_sigmas_and_radii(dtype):
50img = data.astronaut()[:64, :64].mean(axis=2).astype(dtype, copy=False)
51sigmas = [1, 2, 3]
52radii = [1, 2]
53descs = daisy(img, sigmas=sigmas, ring_radii=radii)
54assert descs.dtype == img.dtype
55
56
57def test_daisy_incompatible_sigmas_and_radii():
58img = img_as_float(data.astronaut()[:64, :64].mean(axis=2))
59sigmas = [1, 2]
60radii = [1, 2]
61with pytest.raises(ValueError):
62daisy(img, sigmas=sigmas, ring_radii=radii)
63
64
65def test_daisy_normalization():
66img = img_as_float(data.astronaut()[:64, :64].mean(axis=2))
67
68descs = daisy(img, normalization='l1')
69for i in range(descs.shape[0]):
70for j in range(descs.shape[1]):
71assert_almost_equal(np.sum(descs[i, j, :]), 1)
72descs_ = daisy(img)
73assert_almost_equal(descs, descs_)
74
75descs = daisy(img, normalization='l2')
76for i in range(descs.shape[0]):
77for j in range(descs.shape[1]):
78assert_almost_equal(sqrt(np.sum(descs[i, j, :] ** 2)), 1)
79
80orientations = 8
81descs = daisy(img, orientations=orientations, normalization='daisy')
82desc_dims = descs.shape[2]
83for i in range(descs.shape[0]):
84for j in range(descs.shape[1]):
85for k in range(0, desc_dims, orientations):
86assert_almost_equal(
87sqrt(np.sum(descs[i, j, k : k + orientations] ** 2)), 1
88)
89
90img = np.zeros((50, 50))
91descs = daisy(img, normalization='off')
92for i in range(descs.shape[0]):
93for j in range(descs.shape[1]):
94assert_almost_equal(np.sum(descs[i, j, :]), 0)
95
96with pytest.raises(ValueError):
97daisy(img, normalization='does_not_exist')
98
99
100def test_daisy_visualization():
101img = img_as_float(data.astronaut()[:32, :32].mean(axis=2))
102descs, descs_img = daisy(img, visualize=True)
103assert descs_img.shape == (32, 32, 3)
104