scikit-image

Форк
0
/
test_polygon.py 
72 строки · 2.2 Кб
1
import numpy as np
2
from skimage.measure import approximate_polygon, subdivide_polygon
3
from skimage.measure._polygon import _SUBDIVISION_MASKS
4

5
from skimage._shared import testing
6
from skimage._shared.testing import assert_array_equal, assert_equal
7

8

9
square = 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

28
def test_approximate_polygon():
29
    out = approximate_polygon(square, 0.1)
30
    assert_array_equal(out, square[(0, 3, 6, 9, 12), :])
31

32
    out = approximate_polygon(square, 2.2)
33
    assert_array_equal(out, square[(0, 6, 12), :])
34

35
    out = approximate_polygon(square[(0, 1, 3, 4, 5, 6, 7, 9, 11, 12), :], 0.1)
36
    assert_array_equal(out, square[(0, 3, 6, 9, 12), :])
37

38
    out = approximate_polygon(square, -1)
39
    assert_array_equal(out, square)
40
    out = approximate_polygon(square, 0)
41
    assert_array_equal(out, square)
42

43

44
def test_subdivide_polygon():
45
    new_square1 = square
46
    new_square2 = square[:-1]
47
    new_square3 = square[:-1]
48
    # test iterative subdvision
49
    for _ in range(10):
50
        square1, square2, square3 = new_square1, new_square2, new_square3
51
        # test different B-Spline degrees
52
        for degree in range(1, 7):
53
            mask_len = len(_SUBDIVISION_MASKS[degree][0])
54
            # test circular
55
            new_square1 = subdivide_polygon(square1, degree)
56
            assert_array_equal(new_square1[-1], new_square1[0])
57
            assert_equal(new_square1.shape[0], 2 * square1.shape[0] - 1)
58
            # test non-circular
59
            new_square2 = subdivide_polygon(square2, degree)
60
            assert_equal(new_square2.shape[0], 2 * (square2.shape[0] - mask_len + 1))
61
            # test non-circular, preserve_ends
62
            new_square3 = subdivide_polygon(square3, degree, True)
63
            assert_equal(new_square3[0], square3[0])
64
            assert_equal(new_square3[-1], square3[-1])
65

66
            assert_equal(new_square3.shape[0], 2 * (square3.shape[0] - mask_len + 2))
67

68
    # not supported B-Spline degree
69
    with testing.raises(ValueError):
70
        subdivide_polygon(square, 0)
71
    with testing.raises(ValueError):
72
        subdivide_polygon(square, 8)
73

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

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

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

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