Fixed ellipse symmetry and added some tests.

This commit is contained in:
Alexey Umnov
2014-09-17 19:13:36 +04:00
parent b67d46ab91
commit abcd613593
2 changed files with 166 additions and 34 deletions
+139 -21
View File
@@ -298,30 +298,134 @@ def test_circle_perimeter_aa():
assert_array_equal(img, img_)
def test_ellipse():
img = np.zeros((15, 15), 'uint8')
def test_ellipse_trivial():
img = np.zeros((2, 2), 'uint8')
rr, cc = ellipse(0.5, 0.5, 0.5, 0.5)
img[rr, cc] = 1
img_correct = np.array([
[0, 0],
[0, 0]
])
assert_array_equal(img, img_correct)
img = np.zeros((2, 2), 'uint8')
rr, cc = ellipse(0.5, 0.5, 1.1, 1.1)
img[rr, cc] = 1
img_correct = np.array([
[1, 1],
[1, 1],
])
assert_array_equal(img, img_correct)
img = np.zeros((3, 3), 'uint8')
rr, cc = ellipse(1, 1, 0.9, 0.9)
img[rr, cc] = 1
img_correct = np.array([
[0, 0, 0],
[0, 1, 0],
[0, 0, 0],
])
assert_array_equal(img, img_correct)
img = np.zeros((3, 3), 'uint8')
rr, cc = ellipse(1, 1, 1.1, 1.1)
img[rr, cc] = 1
img_correct = np.array([
[0, 1, 0],
[1, 1, 1],
[0, 1, 0],
])
assert_array_equal(img, img_correct)
img = np.zeros((3, 3), 'uint8')
rr, cc = ellipse(1, 1, 1.5, 1.5)
img[rr, cc] = 1
img_correct = np.array([
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
])
assert_array_equal(img, img_correct)
def test_ellipse_generic():
img = np.zeros((4, 4), 'uint8')
rr, cc = ellipse(1.5, 1.5, 1.1, 1.7)
img[rr, cc] = 1
img_ = np.array([
[0, 0, 0, 0],
[1, 1, 1, 1],
[1, 1, 1, 1],
[0, 0, 0, 0],
])
assert_array_equal(img, img_)
img = np.zeros((5, 5), 'uint8')
rr, cc = ellipse(2, 2, 1.7, 1.7)
img[rr, cc] = 1
img_ = np.array([
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0],
])
assert_array_equal(img, img_)
img = np.zeros((10, 10), 'uint8')
rr, cc = ellipse(5, 5, 3, 4)
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, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 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],
])
assert_array_equal(img, img_)
img = np.zeros((10, 10), 'uint8')
rr, cc = ellipse(4.5, 5, 3.5, 4)
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, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 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],
])
assert_array_equal(img, img_)
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,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 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_)
@@ -352,6 +456,20 @@ def test_ellipse_with_shape():
assert_array_equal(img, img_)
def test_ellipse_negative():
rr, cc = ellipse(-3, -3, 1.7, 1.7)
rr_, cc_ = np.nonzero(np.array([
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0],
]))
assert_array_equal(rr, rr_ - 5)
assert_array_equal(cc, cc_ - 5)
def test_ellipse_perimeter_dot_zeroangle():
# dot, angle == 0
img = np.zeros((30, 15), 'uint8')