-
Notifications
You must be signed in to change notification settings - Fork 290
/
triangulation_implement.py
39 lines (32 loc) · 1.15 KB
/
triangulation_implement.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
import numpy as np
import cv2 as cv
def triangulatePoints(P0, P1, pts0, pts1):
Xs = []
for (p, q) in zip(pts0.T, pts1.T):
# Solve 'AX = 0'
A = np.vstack((p[0] * P0[2] - P0[0],
p[1] * P0[2] - P0[1],
q[0] * P1[2] - P1[0],
q[1] * P1[2] - P1[1]))
_, _, Vt = np.linalg.svd(A, full_matrices=True)
Xs.append(Vt[-1])
return np.vstack(Xs).T
if __name__ == '__main__':
f, cx, cy = 1000., 320., 240.
pts0 = np.loadtxt('../data/image_formation0.xyz')[:,:2]
pts1 = np.loadtxt('../data/image_formation1.xyz')[:,:2]
output_file = 'triangulation_implement.xyz'
# Estimate relative pose of two view
F, _ = cv.findFundamentalMat(pts0, pts1, cv.FM_8POINT)
K = np.array([[f, 0, cx], [0, f, cy], [0, 0, 1]])
E = K.T @ F @ K
_, R, t, _ = cv.recoverPose(E, pts0, pts1)
# Reconstruct 3D points (triangulation)
P0 = K @ np.eye(3, 4, dtype=np.float32)
Rt = np.hstack((R, t))
P1 = K @ Rt
X = triangulatePoints(P0, P1, pts0.T, pts1.T)
X /= X[3]
X = X.T
# Write the reconstructed 3D points
np.savetxt(output_file, X)