forked from ericmjonas/franktrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_util.py
108 lines (87 loc) · 2.79 KB
/
test_util.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import numpy as np
from numpy.testing import *
from nose.tools import *
import util2 as util
import time
def test_geom_converter():
gc = util.GeomConverter((320, 240),
(160, 120),
(-1.0, -1.0))
x, y = gc.real_to_image(0, 0)
assert_almost_equal(x, 120)
assert_almost_equal(y, 160)
x, y = gc.real_to_image(-1, -1)
assert_almost_equal(x, 0)
assert_almost_equal(y, 0)
gc = util.GeomConverter((320, 240),
(160, 120),
(-1.0, -1.5))
x, y = gc.real_to_image(0, 0)
assert_almost_equal(x, 180)
assert_almost_equal(y, 160)
def test_extract_region():
R, C = 4, 6
x = np.arange(R*C)
x.shape = (R, C)
for r in range(R):
for c in range(C):
s = util.extract_region_safe(x, r, c, 0, 17)
assert_equal(s.shape, (1, 1))
assert_equal(s[0, 0], x[r, c])
# upper left
s = util.extract_region_safe(x, 0, 0, 1, 17)
assert_equal(s.shape, (3, 3))
assert_array_equal(s[0, :], 17)
assert_array_equal(s[:, 0], 17)
assert_array_equal(s[1:,1:], x[:2, :2])
# upper right
s = util.extract_region_safe(x, 0, 5, 1, 17)
assert_equal(s.shape, (3, 3))
assert_array_equal(s[0, :], 17)
assert_array_equal(s[:, 2], 17)
assert_array_equal(s[1:,:2], x[:2, 4:])
# lower left
s = util.extract_region_safe(x, 3, 0, 1, 17)
assert_equal(s.shape, (3, 3))
assert_array_equal(s[2, :], 17)
assert_array_equal(s[:, 0], 17)
assert_array_equal(s[:2,1:], x[2:, :2])
# lower right
s = util.extract_region_safe(x, 3, 5, 1, 17)
assert_equal(s.shape, (3, 3))
assert_array_equal(s[2, :], 17)
assert_array_equal(s[:, 2], 17)
assert_array_equal(s[:2,:2], x[2:, 4:])
# now we move the window around for a variety of edge sizes,
# save the middle pixel, and reconstruct the matrix and compare
for p in range(4):
newx= np.zeros_like(x)
for r in range(R):
for c in range(C):
s = util.extract_region_safe(x, r, c, p, 17)
newx[r, c] = s[p, p]
assert_array_equal(newx, x)
def test_render_hat():
N = 100
H = 48
W = 64
x = 30
y = 20
size = 3.0
BORDER = 0.2
slows = []
t1 = time.time()
for i in range(N):
img = util.render_hat_ma(H, W, x, y, size, BORDER)
slows.append(img)
t2 = time.time()
print "slows took", (t2-t1)/N*1e6, "us per render"
fasts = []
t1 = time.time()
for i in range(N):
img = util.render_hat_ma_fast(H, W, x, y, size, BORDER)
fasts.append(img)
t2 = time.time()
print "fasts took", (t2-t1)/N*1e6, "us per render"
for s, f in zip(slows, fasts):
assert_allclose(s, f)