forked from argoverse/argoverse-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_dilate_utils_unit.py
75 lines (62 loc) · 2.1 KB
/
test_dilate_utils_unit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# <Copyright 2019, Argo AI, LLC. Released under the MIT license.>
"""Unit tests for dilation_utils.py."""
import numpy as np
from argoverse.utils.dilation_utils import dilate_by_l2
def test_dilate_by_l2_no_change() -> None:
"""Test dilate_by_l2() and expect no change.
Do not modify the following 8-bit image (0 px dilation)
0 0 0 0 0
0 x x x 0
0 x x x 0
0 x x x 0
0 0 0 0 0
"""
img = np.zeros((5, 5), dtype=np.uint8)
img[1:4, 1:4] = 1
dilated_img = dilate_by_l2(img.copy(), dilation_thresh=0.0)
assert np.allclose(img, dilated_img)
def test_dilate_by_l2_2x2square_1px() -> None:
"""Test dilate_by_l2() with a 1 pixel dilation.
Dilate the following 8-bit image w/ 1 px border
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 x x 0 0
0 0 x x 0 0 -> 0 x x x x 0
0 0 x x 0 0 0 x x x x 0
0 0 0 0 0 0 0 0 x x 0 0
0 0 0 0 0 0 0 0 0 0 0 0
"""
img = np.zeros((6, 6), dtype=np.uint8)
img[2:4, 2:4] = 1
dilated_img = dilate_by_l2(img, dilation_thresh=1.0)
print(dilated_img)
# ground truth
dilated_img_gt = np.array(
[
[0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0],
],
dtype=np.uint8,
)
assert np.allclose(dilated_img, dilated_img_gt)
def test_dilate_by_l2_horizline_1point42px() -> None:
"""Test dilate_by_l2() with a non-integer dilation that should get cut off.
Dilate the following 8-bit image w/ 1.42 px border,
allowing us to access diagonal neighbors:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 x x x x
0 0 x x 0 -> 0 x x x x
0 0 0 0 0 0 x x x x
0 0 0 0 0 0 0 0 0 0
"""
img = np.zeros((5, 5), dtype=np.uint8)
img[2, 2:4] = 1
dilated_img = dilate_by_l2(img, dilation_thresh=1.42)
# ground truth
dilated_img_gt = np.array(
[[0, 0, 0, 0, 0], [0, 1, 1, 1, 1], [0, 1, 1, 1, 1], [0, 1, 1, 1, 1], [0, 0, 0, 0, 0]], dtype=np.uint8
)
assert np.allclose(dilated_img, dilated_img_gt)