forked from mattloper/opendr
/
test_camera.py
executable file
·98 lines (67 loc) · 2.88 KB
/
test_camera.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
#!/usr/bin/env python
# encoding: utf-8
"""
Author(s): Matthew Loper
See LICENCE.txt for licensing and contact information.
"""
import unittest
import numpy as np
from camera import *
import chumpy as ch
class TestCamera(unittest.TestCase):
def get_cam_params(self):
v_raw = np.sin(np.arange(900)).reshape((-1,3))
v_raw[:, 2] -= 2
rt = ch.zeros(3)
t = ch.zeros(3)
f = ch.array([500,500])
c = ch.array([320,240])
k = ch.zeros(5)
cam_params = {'v': ch.Ch(v_raw), 'rt': rt, 't': t, 'f': f, 'c': c, 'k': k}
return cam_params
def test_project_points_without_derivatives(self):
from util_tests import get_earthmesh
cam_params = self.get_cam_params()
mesh = get_earthmesh(trans=np.array([0,0,5]), rotation = np.array([0,0,0]))
pp = ProjectPoints(f=cam_params['f'], rt=cam_params['rt'], t=cam_params['t'], k=cam_params['k'], c=cam_params['c'])
pp.v = mesh.v
np.testing.assert_array_equal(pp.r, pp.r_and_derivatives[0].squeeze())
def test_project_points(self):
self.project_points(ProjectPoints)
def test_project_points_3d(self):
self.project_points(ProjectPoints3D)
def project_points(self, cls):
cam_params = self.get_cam_params()
for key, value in cam_params.items():
eps = (np.random.random(value.r.size)-.5) * 1e-5
pp_dist = cls(**cam_params)
old_val = pp_dist.r.copy()
old_dr = pp_dist.dr_wrt(value).dot(eps)
tmp = cam_params[key].r.copy()
tmp += eps.reshape(tmp.shape)
cam_params[key] = ch.Ch(tmp)
diff = ((cls(**cam_params).r - old_val))
raw_dr_diff = np.abs(old_dr.flatten() - diff.flatten())
med_diff = np.median(raw_dr_diff)
max_diff = np.max(raw_dr_diff)
#pct_diff = (100. * max_diff / np.mean(np.abs(old_val.flatten())))
# print 'testing for %s' % (key,)
# print 'empirical' + str(diff.flatten()[:5])
# print 'predicted' + str(old_dr[:5])
# print 'med diff: %.2e' % (med_diff,)
# print 'max diff: %.2e' % (max_diff,)
#print 'pct diff: %.2e%%' % (pct_diff,)
self.assertLess(med_diff, 1e-8)
self.assertLess(max_diff, 5e-8)
pp_dist = cls(**cam_params)
# Test to make sure that depends_on is working
for name in ('rt', 't', 'f', 'c', 'k'):
aa = pp_dist.camera_mtx
setattr(pp_dist, name, getattr(pp_dist, name).r + 1)
bb = pp_dist.camera_mtx
if name in ('f', 'c'):
self.assertTrue(aa is not bb)
else:
self.assertTrue(aa is bb)
if __name__ == '__main__':
unittest.main()