-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_utils.py
executable file
·68 lines (52 loc) · 1.87 KB
/
my_utils.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
import numpy as np
def posetow2c_matrcs(poses):
tmp = inversestep4(inversestep3(inversestep2(inversestep1(poses))))
N = tmp.shape[0]
ret = []
for i in range(N):
ret.append(tmp[i])
return ret
def getRTfromPose(w2c_mats):
for m in w2c_mats:
R = m[:3, :3]
t = m[:3, 3]
#print(R, t)
def tolist(w2c_mats):
return w2c_mats.tolist()
def inversestep4(c2w_mats):
return np.linalg.inv(c2w_mats)
def inversestep3(newposes):
tmp = newposes.transpose([2, 0, 1]) # 20, 3, 4
N, _, __ = tmp.shape
zeros = np.zeros((N, 1, 4))
zeros[:, 0, 3] = 1
c2w_mats = np.concatenate([tmp, zeros], axis=1)
return c2w_mats
def inversestep2(newposes):
return newposes[:,0:4, :]
def inversestep1(newposes):
poses = np.concatenate([newposes[:, 1:2, :], newposes[:, 0:1, :], -newposes[:, 2:3, :], newposes[:, 3:4, :], newposes[:, 4:5, :]], axis=1)
return poses
def rotmat2qvec(R):
Rxx, Ryx, Rzx, Rxy, Ryy, Rzy, Rxz, Ryz, Rzz = R.flat
K = np.array([
[Rxx - Ryy - Rzz, 0, 0, 0],
[Ryx + Rxy, Ryy - Rxx - Rzz, 0, 0],
[Rzx + Rxz, Rzy + Ryz, Rzz - Rxx - Ryy, 0],
[Ryz - Rzy, Rzx - Rxz, Rxy - Ryx, Rxx + Ryy + Rzz]]) / 3.0
eigvals, eigvecs = np.linalg.eigh(K)
qvec = eigvecs[[3, 0, 1, 2], np.argmax(eigvals)]
if qvec[0] < 0:
qvec *= -1
return qvec
def qvec2rotmat(qvec):
return np.array([
[1 - 2 * qvec[2]**2 - 2 * qvec[3]**2,
2 * qvec[1] * qvec[2] - 2 * qvec[0] * qvec[3],
2 * qvec[3] * qvec[1] + 2 * qvec[0] * qvec[2]],
[2 * qvec[1] * qvec[2] + 2 * qvec[0] * qvec[3],
1 - 2 * qvec[1]**2 - 2 * qvec[3]**2,
2 * qvec[2] * qvec[3] - 2 * qvec[0] * qvec[1]],
[2 * qvec[3] * qvec[1] - 2 * qvec[0] * qvec[2],
2 * qvec[2] * qvec[3] + 2 * qvec[0] * qvec[1],
1 - 2 * qvec[1]**2 - 2 * qvec[2]**2]])