Exemple #1
0
def test_quadrilateral():

    points = np.array([[2, 2], [6, 2], [6, 7], [2, 6]])
    q = Quadrilateral(points)
    q.to_interval()
    q.to_rectangle()
    assert q.shift(1) == Quadrilateral(points + 1)
    assert q.shift([1, 2]) == Quadrilateral(points + np.array([1, 2]))
    assert q.scale(2) == Quadrilateral(points * 2)
    assert q.scale([3, 2]) == Quadrilateral(points * np.array([3, 2]))
    assert q.pad(left=1, top=2, bottom=4) == Quadrilateral(
        np.array([[1, 0], [6, 0], [6, 11], [1, 10]]))
    assert (q.mapped_rectangle_points == np.array([[0, 0], [4, 0], [4, 5],
                                                   [0, 5]])).all()

    points = np.array([[2, 2], [6, 2], [6, 5], [2, 5]])
    q = Quadrilateral(points)
    img = np.random.randint(2, 24, (30, 20)).astype("uint8")
    img[2:5, 2:6] = 0
    assert np.unique(q.crop_image(img)) == np.array([0])

    q = Quadrilateral(np.array([[-2, 0], [0, 2], [2, 0], [0, -2]]))
    assert q.area == 8.0

    q = Quadrilateral([1, 2, 3, 4, 5, 6, 7, 8])
    assert (q.points == np.array([[1, 2], [3, 4], [5, 6], [7, 8]])).all()

    with pytest.raises(ValueError):
        Quadrilateral([1, 2, 3, 4, 5, 6, 7])  # Incompatible list length

    with pytest.raises(ValueError):
        Quadrilateral(np.array([[2, 2], [6, 2],
                                [6, 5]]))  # Incompatible ndarray shape
def test_quadrilateral():

    points = np.array([[2, 2], [6, 2], [6, 7], [2, 6]])
    q = Quadrilateral(points)
    q.to_interval()
    q.to_rectangle()
    assert q.shift(1) == Quadrilateral(points + 1)
    assert q.shift([1, 2]) == Quadrilateral(points + np.array([1, 2]))
    assert q.scale(2) == Quadrilateral(points * 2)
    assert q.scale([3, 2]) == Quadrilateral(points * np.array([3, 2]))
    assert q.pad(left=1, top=2, bottom=4) == Quadrilateral(
        np.array([[1, 0], [6, 0], [6, 11], [1, 10]]))
    assert (q.mapped_rectangle_points == np.array([[0, 0], [4, 0], [4, 5],
                                                   [0, 5]])).all()

    points = np.array([[2, 2], [6, 2], [6, 5], [2, 5]])
    q = Quadrilateral(points)
    img = np.random.randint(2, 24, (30, 20)).astype('uint8')
    img[2:5, 2:6] = 0
    assert np.unique(q.crop_image(img)) == np.array([0])

    q = Quadrilateral(np.array([[-2, 0], [0, 2], [2, 0], [0, -2]]))
    assert q.area == 8.