scikit-image
72 строки · 2.2 Кб
1import numpy as np2from skimage.measure import approximate_polygon, subdivide_polygon3from skimage.measure._polygon import _SUBDIVISION_MASKS4
5from skimage._shared import testing6from skimage._shared.testing import assert_array_equal, assert_equal7
8
9square = np.array(10[11[0, 0],12[0, 1],13[0, 2],14[0, 3],15[1, 3],16[2, 3],17[3, 3],18[3, 2],19[3, 1],20[3, 0],21[2, 0],22[1, 0],23[0, 0],24]25)
26
27
28def test_approximate_polygon():29out = approximate_polygon(square, 0.1)30assert_array_equal(out, square[(0, 3, 6, 9, 12), :])31
32out = approximate_polygon(square, 2.2)33assert_array_equal(out, square[(0, 6, 12), :])34
35out = approximate_polygon(square[(0, 1, 3, 4, 5, 6, 7, 9, 11, 12), :], 0.1)36assert_array_equal(out, square[(0, 3, 6, 9, 12), :])37
38out = approximate_polygon(square, -1)39assert_array_equal(out, square)40out = approximate_polygon(square, 0)41assert_array_equal(out, square)42
43
44def test_subdivide_polygon():45new_square1 = square46new_square2 = square[:-1]47new_square3 = square[:-1]48# test iterative subdvision49for _ in range(10):50square1, square2, square3 = new_square1, new_square2, new_square351# test different B-Spline degrees52for degree in range(1, 7):53mask_len = len(_SUBDIVISION_MASKS[degree][0])54# test circular55new_square1 = subdivide_polygon(square1, degree)56assert_array_equal(new_square1[-1], new_square1[0])57assert_equal(new_square1.shape[0], 2 * square1.shape[0] - 1)58# test non-circular59new_square2 = subdivide_polygon(square2, degree)60assert_equal(new_square2.shape[0], 2 * (square2.shape[0] - mask_len + 1))61# test non-circular, preserve_ends62new_square3 = subdivide_polygon(square3, degree, True)63assert_equal(new_square3[0], square3[0])64assert_equal(new_square3[-1], square3[-1])65
66assert_equal(new_square3.shape[0], 2 * (square3.shape[0] - mask_len + 2))67
68# not supported B-Spline degree69with testing.raises(ValueError):70subdivide_polygon(square, 0)71with testing.raises(ValueError):72subdivide_polygon(square, 8)73