scikit-image
284 строки · 7.6 Кб
1import numpy as np2
3from ..._shared.testing import assert_equal, assert_almost_equal4from ..profile import profile_line5
6image = np.arange(100).reshape((10, 10)).astype(float)7
8
9def test_horizontal_rightward():10prof = profile_line(image, (0, 2), (0, 8), order=0, mode='constant')11expected_prof = np.arange(2, 9)12assert_equal(prof, expected_prof)13
14
15def test_horizontal_leftward():16prof = profile_line(image, (0, 8), (0, 2), order=0, mode='constant')17expected_prof = np.arange(8, 1, -1)18assert_equal(prof, expected_prof)19
20
21def test_vertical_downward():22prof = profile_line(image, (2, 5), (8, 5), order=0, mode='constant')23expected_prof = np.arange(25, 95, 10)24assert_equal(prof, expected_prof)25
26
27def test_vertical_upward():28prof = profile_line(image, (8, 5), (2, 5), order=0, mode='constant')29expected_prof = np.arange(85, 15, -10)30assert_equal(prof, expected_prof)31
32
33def test_45deg_right_downward():34prof = profile_line(image, (2, 2), (8, 8), order=0, mode='constant')35expected_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 of38# length traversing a checkerboard pattern of squares also of unit39# length. Because the line is diagonal, sometimes more than one40# marker will fall on the same checkerboard box.41assert_almost_equal(prof, expected_prof)42
43
44def test_45deg_right_downward_interpolated():45prof = profile_line(image, (2, 2), (8, 8), order=1, mode='constant')46expected_prof = np.linspace(22, 88, 10)47assert_almost_equal(prof, expected_prof)48
49
50def test_45deg_right_upward():51prof = profile_line(image, (8, 2), (2, 8), order=1, mode='constant')52expected_prof = np.arange(82, 27, -6)53assert_almost_equal(prof, expected_prof)54
55
56def test_45deg_left_upward():57prof = profile_line(image, (8, 8), (2, 2), order=1, mode='constant')58expected_prof = np.arange(88, 21, -22.0 / 3)59assert_almost_equal(prof, expected_prof)60
61
62def test_45deg_left_downward():63prof = profile_line(image, (2, 8), (8, 2), order=1, mode='constant')64expected_prof = np.arange(28, 83, 6)65assert_almost_equal(prof, expected_prof)66
67
68def test_pythagorean_triangle_right_downward():69prof = profile_line(image, (1, 1), (7, 9), order=0, mode='constant')70expected_prof = np.array([11, 22, 23, 33, 34, 45, 56, 57, 67, 68, 79])71assert_equal(prof, expected_prof)72
73
74def test_pythagorean_triangle_right_downward_interpolated():75prof = profile_line(image, (1, 1), (7, 9), order=1, mode='constant')76expected_prof = np.linspace(11, 79, 11)77assert_almost_equal(prof, expected_prof)78
79
80pyth_image = np.zeros((6, 7), float)81line = ((1, 2, 2, 3, 3, 4), (1, 2, 3, 3, 4, 5))82below = ((2, 2, 3, 4, 4, 5), (0, 1, 2, 3, 4, 4))83above = ((0, 1, 1, 2, 3, 3), (2, 2, 3, 4, 5, 6))84pyth_image[line] = 1.885pyth_image[below] = 0.686pyth_image[above] = 0.687
88
89def test_pythagorean_triangle_right_downward_linewidth():90prof = profile_line(91pyth_image, (1, 1), (4, 5), linewidth=3, order=0, mode='constant'92)93expected_prof = np.ones(6)94assert_almost_equal(prof, expected_prof)95
96
97def test_pythagorean_triangle_right_upward_linewidth():98prof = profile_line(99pyth_image[::-1, :], (4, 1), (1, 5), linewidth=3, order=0, mode='constant'100)101expected_prof = np.ones(6)102assert_almost_equal(prof, expected_prof)103
104
105def test_pythagorean_triangle_transpose_left_down_linewidth():106prof = profile_line(107pyth_image.T[:, ::-1], (1, 4), (5, 1), linewidth=3, order=0, mode='constant'108)109expected_prof = np.ones(6)110assert_almost_equal(prof, expected_prof)111
112
113def test_reduce_func_mean():114prof = profile_line(115pyth_image,116(0, 1),117(3, 1),118linewidth=3,119order=0,120reduce_func=np.mean,121mode='reflect',122)123expected_prof = pyth_image[:4, :3].mean(1)124assert_almost_equal(prof, expected_prof)125
126
127def test_reduce_func_max():128prof = profile_line(129pyth_image,130(0, 1),131(3, 1),132linewidth=3,133order=0,134reduce_func=np.max,135mode='reflect',136)137expected_prof = pyth_image[:4, :3].max(1)138assert_almost_equal(prof, expected_prof)139
140
141def test_reduce_func_sum():142prof = profile_line(143pyth_image,144(0, 1),145(3, 1),146linewidth=3,147order=0,148reduce_func=np.sum,149mode='reflect',150)151expected_prof = pyth_image[:4, :3].sum(1)152assert_almost_equal(prof, expected_prof)153
154
155def test_reduce_func_mean_linewidth_1():156prof = profile_line(157pyth_image,158(0, 1),159(3, 1),160linewidth=1,161order=0,162reduce_func=np.mean,163mode='constant',164)165expected_prof = pyth_image[:4, 1]166assert_almost_equal(prof, expected_prof)167
168
169def test_reduce_func_None_linewidth_1():170prof = profile_line(171pyth_image,172(1, 2),173(4, 2),174linewidth=1,175order=0,176reduce_func=None,177mode='constant',178)179expected_prof = pyth_image[1:5, 2, np.newaxis]180assert_almost_equal(prof, expected_prof)181
182
183def test_reduce_func_None_linewidth_3():184prof = profile_line(185pyth_image,186(1, 2),187(4, 2),188linewidth=3,189order=0,190reduce_func=None,191mode='constant',192)193expected_prof = pyth_image[1:5, 1:4]194assert_almost_equal(prof, expected_prof)195
196
197def test_reduce_func_lambda_linewidth_3():198def reduce_func(x):199return x + x**2200
201prof = profile_line(202pyth_image,203(1, 2),204(4, 2),205linewidth=3,206order=0,207reduce_func=reduce_func,208mode='constant',209)210expected_prof = np.apply_along_axis(reduce_func, arr=pyth_image[1:5, 1:4], axis=1)211assert_almost_equal(prof, expected_prof)212
213
214def test_reduce_func_sqrt_linewidth_3():215def reduce_func(x):216return x**0.5217
218prof = profile_line(219pyth_image,220(1, 2),221(4, 2),222linewidth=3,223order=0,224reduce_func=reduce_func,225mode='constant',226)227expected_prof = np.apply_along_axis(reduce_func, arr=pyth_image[1:5, 1:4], axis=1)228assert_almost_equal(prof, expected_prof)229
230
231def test_reduce_func_sumofsqrt_linewidth_3():232def reduce_func(x):233return np.sum(x**0.5)234
235prof = profile_line(236pyth_image,237(1, 2),238(4, 2),239linewidth=3,240order=0,241reduce_func=reduce_func,242mode='constant',243)244expected_prof = np.apply_along_axis(reduce_func, arr=pyth_image[1:5, 1:4], axis=1)245assert_almost_equal(prof, expected_prof)246
247
248def test_oob_coodinates():249offset = 2250idx = pyth_image.shape[0] + offset251prof = profile_line(252pyth_image,253(-offset, 2),254(idx, 2),255linewidth=1,256order=0,257reduce_func=None,258mode='constant',259)260expected_prof = np.vstack(261[np.zeros((offset, 1)), pyth_image[:, 2, np.newaxis], np.zeros((offset + 1, 1))]262)263assert_almost_equal(prof, expected_prof)264
265
266def test_bool_array_input():267shape = (200, 200)268center_x, center_y = (140, 150)269radius = 20270x, y = np.meshgrid(range(shape[1]), range(shape[0]))271mask = (y - center_y) ** 2 + (x - center_x) ** 2 < radius**2272src = (center_y, center_x)273phi = 4 * np.pi / 9.0274dy = 31 * np.cos(phi)275dx = 31 * np.sin(phi)276dst = (center_y + dy, center_x + dx)277
278profile_u8 = profile_line(mask.astype(np.uint8), src, dst, mode='reflect')279assert all(profile_u8[:radius] == 1)280
281profile_b = profile_line(mask, src, dst, mode='reflect')282assert all(profile_b[:radius] == 1)283
284assert all(profile_b == profile_u8)285