-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.py
106 lines (80 loc) · 2.58 KB
/
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
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
import numpy as np
def _make_homogeneous_rep_matrix(R, t):
P = np.zeros((4,4))
P[:3,:3] = R
P[:3, 3] = t.reshape(3)
P[3,3] = 1
return P
#direct linear transform
def DLT(P1, P2, point1, point2):
A = [point1[1]*P1[2,:] - P1[1,:],
P1[0,:] - point1[0]*P1[2,:],
point2[1]*P2[2,:] - P2[1,:],
P2[0,:] - point2[0]*P2[2,:]
]
A = np.array(A).reshape((4,4))
#print('A: ')
#print(A)
B = A.transpose() @ A
from scipy import linalg
U, s, Vh = linalg.svd(B, full_matrices = False)
#print('Triangulated point: ')
#print(Vh[3,0:3]/Vh[3,3])
return Vh[3,0:3]/Vh[3,3]
def read_camera_parameters(camera_id):
inf = open('camera_parameters/c' + str(camera_id) + '.dat', 'r')
cmtx = []
dist = []
line = inf.readline()
for _ in range(3):
line = inf.readline().split()
line = [float(en) for en in line]
cmtx.append(line)
line = inf.readline()
line = inf.readline().split()
line = [float(en) for en in line]
dist.append(line)
return np.array(cmtx), np.array(dist)
def read_rotation_translation(camera_id, savefolder = 'camera_parameters/'):
inf = open(savefolder + 'rot_trans_c'+ str(camera_id) + '.dat', 'r')
inf.readline()
rot = []
trans = []
for _ in range(3):
line = inf.readline().split()
line = [float(en) for en in line]
rot.append(line)
inf.readline()
for _ in range(3):
line = inf.readline().split()
line = [float(en) for en in line]
trans.append(line)
inf.close()
return np.array(rot), np.array(trans)
def _convert_to_homogeneous(pts):
pts = np.array(pts)
if len(pts.shape) > 1:
w = np.ones((pts.shape[0], 1))
return np.concatenate([pts, w], axis = 1)
else:
return np.concatenate([pts, [1]], axis = 0)
def get_projection_matrix(camera_id):
#read camera parameters
cmtx, dist = read_camera_parameters(camera_id)
rvec, tvec = read_rotation_translation(camera_id)
#calculate projection matrix
P = cmtx @ _make_homogeneous_rep_matrix(rvec, tvec)[:3,:]
return P
def write_keypoints_to_disk(filename, kpts):
fout = open(filename, 'w')
for frame_kpts in kpts:
for kpt in frame_kpts:
if len(kpt) == 2:
fout.write(str(kpt[0]) + ' ' + str(kpt[1]) + ' ')
else:
fout.write(str(kpt[0]) + ' ' + str(kpt[1]) + ' ' + str(kpt[2]) + ' ')
fout.write('\n')
fout.close()
if __name__ == '__main__':
P2 = get_projection_matrix(0)
P1 = get_projection_matrix(1)