scikit-image

Форк
0
/
test_profile.py 
284 строки · 7.6 Кб
1
import numpy as np
2

3
from ..._shared.testing import assert_equal, assert_almost_equal
4
from ..profile import profile_line
5

6
image = np.arange(100).reshape((10, 10)).astype(float)
7

8

9
def test_horizontal_rightward():
10
    prof = profile_line(image, (0, 2), (0, 8), order=0, mode='constant')
11
    expected_prof = np.arange(2, 9)
12
    assert_equal(prof, expected_prof)
13

14

15
def test_horizontal_leftward():
16
    prof = profile_line(image, (0, 8), (0, 2), order=0, mode='constant')
17
    expected_prof = np.arange(8, 1, -1)
18
    assert_equal(prof, expected_prof)
19

20

21
def test_vertical_downward():
22
    prof = profile_line(image, (2, 5), (8, 5), order=0, mode='constant')
23
    expected_prof = np.arange(25, 95, 10)
24
    assert_equal(prof, expected_prof)
25

26

27
def test_vertical_upward():
28
    prof = profile_line(image, (8, 5), (2, 5), order=0, mode='constant')
29
    expected_prof = np.arange(85, 15, -10)
30
    assert_equal(prof, expected_prof)
31

32

33
def test_45deg_right_downward():
34
    prof = profile_line(image, (2, 2), (8, 8), order=0, mode='constant')
35
    expected_prof = np.array([22, 33, 33, 44, 55, 55, 66, 77, 77, 88])
36
    # repeats are due to aliasing using nearest neighbor interpolation.
37
    # to see this, imagine a diagonal line with markers every unit of
38
    # length traversing a checkerboard pattern of squares also of unit
39
    # length. Because the line is diagonal, sometimes more than one
40
    # marker will fall on the same checkerboard box.
41
    assert_almost_equal(prof, expected_prof)
42

43

44
def test_45deg_right_downward_interpolated():
45
    prof = profile_line(image, (2, 2), (8, 8), order=1, mode='constant')
46
    expected_prof = np.linspace(22, 88, 10)
47
    assert_almost_equal(prof, expected_prof)
48

49

50
def test_45deg_right_upward():
51
    prof = profile_line(image, (8, 2), (2, 8), order=1, mode='constant')
52
    expected_prof = np.arange(82, 27, -6)
53
    assert_almost_equal(prof, expected_prof)
54

55

56
def test_45deg_left_upward():
57
    prof = profile_line(image, (8, 8), (2, 2), order=1, mode='constant')
58
    expected_prof = np.arange(88, 21, -22.0 / 3)
59
    assert_almost_equal(prof, expected_prof)
60

61

62
def test_45deg_left_downward():
63
    prof = profile_line(image, (2, 8), (8, 2), order=1, mode='constant')
64
    expected_prof = np.arange(28, 83, 6)
65
    assert_almost_equal(prof, expected_prof)
66

67

68
def test_pythagorean_triangle_right_downward():
69
    prof = profile_line(image, (1, 1), (7, 9), order=0, mode='constant')
70
    expected_prof = np.array([11, 22, 23, 33, 34, 45, 56, 57, 67, 68, 79])
71
    assert_equal(prof, expected_prof)
72

73

74
def test_pythagorean_triangle_right_downward_interpolated():
75
    prof = profile_line(image, (1, 1), (7, 9), order=1, mode='constant')
76
    expected_prof = np.linspace(11, 79, 11)
77
    assert_almost_equal(prof, expected_prof)
78

79

80
pyth_image = np.zeros((6, 7), float)
81
line = ((1, 2, 2, 3, 3, 4), (1, 2, 3, 3, 4, 5))
82
below = ((2, 2, 3, 4, 4, 5), (0, 1, 2, 3, 4, 4))
83
above = ((0, 1, 1, 2, 3, 3), (2, 2, 3, 4, 5, 6))
84
pyth_image[line] = 1.8
85
pyth_image[below] = 0.6
86
pyth_image[above] = 0.6
87

88

89
def test_pythagorean_triangle_right_downward_linewidth():
90
    prof = profile_line(
91
        pyth_image, (1, 1), (4, 5), linewidth=3, order=0, mode='constant'
92
    )
93
    expected_prof = np.ones(6)
94
    assert_almost_equal(prof, expected_prof)
95

96

97
def test_pythagorean_triangle_right_upward_linewidth():
98
    prof = profile_line(
99
        pyth_image[::-1, :], (4, 1), (1, 5), linewidth=3, order=0, mode='constant'
100
    )
101
    expected_prof = np.ones(6)
102
    assert_almost_equal(prof, expected_prof)
103

104

105
def test_pythagorean_triangle_transpose_left_down_linewidth():
106
    prof = profile_line(
107
        pyth_image.T[:, ::-1], (1, 4), (5, 1), linewidth=3, order=0, mode='constant'
108
    )
109
    expected_prof = np.ones(6)
110
    assert_almost_equal(prof, expected_prof)
111

112

113
def test_reduce_func_mean():
114
    prof = profile_line(
115
        pyth_image,
116
        (0, 1),
117
        (3, 1),
118
        linewidth=3,
119
        order=0,
120
        reduce_func=np.mean,
121
        mode='reflect',
122
    )
123
    expected_prof = pyth_image[:4, :3].mean(1)
124
    assert_almost_equal(prof, expected_prof)
125

126

127
def test_reduce_func_max():
128
    prof = profile_line(
129
        pyth_image,
130
        (0, 1),
131
        (3, 1),
132
        linewidth=3,
133
        order=0,
134
        reduce_func=np.max,
135
        mode='reflect',
136
    )
137
    expected_prof = pyth_image[:4, :3].max(1)
138
    assert_almost_equal(prof, expected_prof)
139

140

141
def test_reduce_func_sum():
142
    prof = profile_line(
143
        pyth_image,
144
        (0, 1),
145
        (3, 1),
146
        linewidth=3,
147
        order=0,
148
        reduce_func=np.sum,
149
        mode='reflect',
150
    )
151
    expected_prof = pyth_image[:4, :3].sum(1)
152
    assert_almost_equal(prof, expected_prof)
153

154

155
def test_reduce_func_mean_linewidth_1():
156
    prof = profile_line(
157
        pyth_image,
158
        (0, 1),
159
        (3, 1),
160
        linewidth=1,
161
        order=0,
162
        reduce_func=np.mean,
163
        mode='constant',
164
    )
165
    expected_prof = pyth_image[:4, 1]
166
    assert_almost_equal(prof, expected_prof)
167

168

169
def test_reduce_func_None_linewidth_1():
170
    prof = profile_line(
171
        pyth_image,
172
        (1, 2),
173
        (4, 2),
174
        linewidth=1,
175
        order=0,
176
        reduce_func=None,
177
        mode='constant',
178
    )
179
    expected_prof = pyth_image[1:5, 2, np.newaxis]
180
    assert_almost_equal(prof, expected_prof)
181

182

183
def test_reduce_func_None_linewidth_3():
184
    prof = profile_line(
185
        pyth_image,
186
        (1, 2),
187
        (4, 2),
188
        linewidth=3,
189
        order=0,
190
        reduce_func=None,
191
        mode='constant',
192
    )
193
    expected_prof = pyth_image[1:5, 1:4]
194
    assert_almost_equal(prof, expected_prof)
195

196

197
def test_reduce_func_lambda_linewidth_3():
198
    def reduce_func(x):
199
        return x + x**2
200

201
    prof = profile_line(
202
        pyth_image,
203
        (1, 2),
204
        (4, 2),
205
        linewidth=3,
206
        order=0,
207
        reduce_func=reduce_func,
208
        mode='constant',
209
    )
210
    expected_prof = np.apply_along_axis(reduce_func, arr=pyth_image[1:5, 1:4], axis=1)
211
    assert_almost_equal(prof, expected_prof)
212

213

214
def test_reduce_func_sqrt_linewidth_3():
215
    def reduce_func(x):
216
        return x**0.5
217

218
    prof = profile_line(
219
        pyth_image,
220
        (1, 2),
221
        (4, 2),
222
        linewidth=3,
223
        order=0,
224
        reduce_func=reduce_func,
225
        mode='constant',
226
    )
227
    expected_prof = np.apply_along_axis(reduce_func, arr=pyth_image[1:5, 1:4], axis=1)
228
    assert_almost_equal(prof, expected_prof)
229

230

231
def test_reduce_func_sumofsqrt_linewidth_3():
232
    def reduce_func(x):
233
        return np.sum(x**0.5)
234

235
    prof = profile_line(
236
        pyth_image,
237
        (1, 2),
238
        (4, 2),
239
        linewidth=3,
240
        order=0,
241
        reduce_func=reduce_func,
242
        mode='constant',
243
    )
244
    expected_prof = np.apply_along_axis(reduce_func, arr=pyth_image[1:5, 1:4], axis=1)
245
    assert_almost_equal(prof, expected_prof)
246

247

248
def test_oob_coodinates():
249
    offset = 2
250
    idx = pyth_image.shape[0] + offset
251
    prof = profile_line(
252
        pyth_image,
253
        (-offset, 2),
254
        (idx, 2),
255
        linewidth=1,
256
        order=0,
257
        reduce_func=None,
258
        mode='constant',
259
    )
260
    expected_prof = np.vstack(
261
        [np.zeros((offset, 1)), pyth_image[:, 2, np.newaxis], np.zeros((offset + 1, 1))]
262
    )
263
    assert_almost_equal(prof, expected_prof)
264

265

266
def test_bool_array_input():
267
    shape = (200, 200)
268
    center_x, center_y = (140, 150)
269
    radius = 20
270
    x, y = np.meshgrid(range(shape[1]), range(shape[0]))
271
    mask = (y - center_y) ** 2 + (x - center_x) ** 2 < radius**2
272
    src = (center_y, center_x)
273
    phi = 4 * np.pi / 9.0
274
    dy = 31 * np.cos(phi)
275
    dx = 31 * np.sin(phi)
276
    dst = (center_y + dy, center_x + dx)
277

278
    profile_u8 = profile_line(mask.astype(np.uint8), src, dst, mode='reflect')
279
    assert all(profile_u8[:radius] == 1)
280

281
    profile_b = profile_line(mask, src, dst, mode='reflect')
282
    assert all(profile_b[:radius] == 1)
283

284
    assert all(profile_b == profile_u8)
285

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

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

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

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