mirror of
https://github.com/wassname/scikit-image.git
synced 2026-06-29 01:43:45 +08:00
183 lines
4.8 KiB
Python
183 lines
4.8 KiB
Python
from numpy.testing import assert_array_equal
|
|
import numpy as np
|
|
|
|
from skimage.draw import line, polygon, circle, ellipse
|
|
|
|
|
|
def test_line_horizontal():
|
|
img = np.zeros((10, 10))
|
|
|
|
rr, cc = line(0, 0, 0, 9)
|
|
img[rr, cc] = 1
|
|
|
|
img_ = np.zeros((10, 10))
|
|
img_[0, :] = 1
|
|
|
|
assert_array_equal(img, img_)
|
|
|
|
|
|
def test_line_vertical():
|
|
img = np.zeros((10, 10))
|
|
|
|
rr, cc = line(0, 0, 9, 0)
|
|
img[rr, cc] = 1
|
|
|
|
img_ = np.zeros((10, 10))
|
|
img_[:, 0] = 1
|
|
|
|
assert_array_equal(img, img_)
|
|
|
|
|
|
def test_line_reverse():
|
|
img = np.zeros((10, 10))
|
|
|
|
rr, cc = line(0, 9, 0, 0)
|
|
img[rr, cc] = 1
|
|
|
|
img_ = np.zeros((10, 10))
|
|
img_[0, :] = 1
|
|
|
|
assert_array_equal(img, img_)
|
|
|
|
|
|
def test_line_diag():
|
|
img = np.zeros((5, 5))
|
|
|
|
rr, cc = line(0, 0, 4, 4)
|
|
img[rr, cc] = 1
|
|
|
|
img_ = np.eye(5)
|
|
|
|
assert_array_equal(img, img_)
|
|
|
|
|
|
def test_polygon_rectangle():
|
|
img = np.zeros((10, 10), 'uint8')
|
|
poly = np.array(((1, 1), (4, 1), (4, 4), (1, 4), (1, 1)))
|
|
|
|
rr, cc = polygon(poly[:, 0], poly[:, 1])
|
|
img[rr, cc] = 1
|
|
|
|
img_ = np.zeros((10, 10))
|
|
img_[1:4, 1:4] = 1
|
|
|
|
assert_array_equal(img, img_)
|
|
|
|
|
|
def test_polygon_rectangle_angular():
|
|
img = np.zeros((10, 10), 'uint8')
|
|
poly = np.array(((0, 3), (4, 7), (7, 4), (3, 0), (0, 3)))
|
|
|
|
rr, cc = polygon(poly[:, 0], poly[:, 1])
|
|
img[rr, cc] = 1
|
|
|
|
img_ = np.array(
|
|
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
|
|
[0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
|
|
[1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
|
|
[0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
|
|
[0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
|
|
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
|
|
)
|
|
|
|
assert_array_equal(img, img_)
|
|
|
|
|
|
def test_polygon_parallelogram():
|
|
img = np.zeros((10, 10), 'uint8')
|
|
poly = np.array(((1, 1), (5, 1), (7, 6), (3, 6), (1, 1)))
|
|
|
|
rr, cc = polygon(poly[:, 0], poly[:, 1])
|
|
img[rr, cc] = 1
|
|
|
|
img_ = np.array(
|
|
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
|
|
[0, 1, 1, 1, 1, 1, 0, 0, 0, 0],
|
|
[0, 1, 1, 1, 1, 1, 0, 0, 0, 0],
|
|
[0, 1, 1, 1, 1, 1, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
|
|
)
|
|
|
|
assert_array_equal(img, img_)
|
|
|
|
|
|
def test_polygon_exceed():
|
|
img = np.zeros((10, 10), 'uint8')
|
|
poly = np.array(((1, -1), (100, -1), (100, 100), (1, 100), (1, 1)))
|
|
|
|
rr, cc = polygon(poly[:, 0], poly[:, 1], img.shape)
|
|
img[rr, cc] = 1
|
|
|
|
img_ = np.zeros((10, 10))
|
|
img_[1:, :] = 1
|
|
|
|
assert_array_equal(img, img_)
|
|
|
|
|
|
def test_circle():
|
|
img = np.zeros((15, 15), 'uint8')
|
|
|
|
rr, cc = circle(7, 7, 6)
|
|
img[rr, cc] = 1
|
|
|
|
img_ = np.array(
|
|
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
|
|
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
|
|
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
|
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
|
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
|
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
|
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
|
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
|
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
|
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
|
|
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
|
|
)
|
|
|
|
assert_array_equal(img, img_)
|
|
|
|
|
|
def test_ellipse():
|
|
img = np.zeros((15, 15), 'uint8')
|
|
|
|
rr, cc = ellipse(7, 7, 3, 7)
|
|
img[rr, cc] = 1
|
|
|
|
img_ = np.array(
|
|
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
|
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
|
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
|
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
|
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
|
|
)
|
|
|
|
assert_array_equal(img, img_)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
from numpy.testing import run_module_suite
|
|
run_module_suite()
|