-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconstruction_opencv.py
47 lines (36 loc) · 1.41 KB
/
reconstruction_opencv.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
import cv2
import numpy as np
from visualization import plot_epipolar_lines
def compute_epipolar_geometry(pts0, pts1, K1, K2):
# Compute the best Fundamental matrix using ransac
F, mask = cv2.findFundamentalMat(pts0, pts1, cv2.RANSAC)
pts0 = pts0[mask.ravel() == 1]
pts1 = pts1[mask.ravel() == 1]
# Get the index of the selected points
selected_points = np.where(mask.ravel() == 1)[0]
# Compute the Essential matrix
E, _ = cv2.findEssentialMat(pts0, pts1, K1, method=cv2.RANSAC)
# Compute the Rotation and Translation
_, R, C, _ = cv2.recoverPose(E, pts0, pts1, K1)
return F, E, R, C, selected_points
def reconstruct_scene(pts0, pts1, K1, K2):
"""
Reconstruct the scene from the Fundamental matrix and the cameras calibration matrices
:param pts0: points in image 1
:param pts1: points in image 2
:param K1: camera 1 calibration matrix
:param K2: camera 2 calibration matrix
:param image1: image 1
:param image2: image 2
:return: 3D points
"""
E, _ = cv2.findEssentialMat(pts0, pts1, K1, method=cv2.RANSAC)
# Compute the projection matrices
_, R, C, _ = cv2.recoverPose(E, pts0, pts1, K1)
P1 = K1 @ np.hstack([np.eye(3), np.zeros((3, 1))])
P2 = K2 @ np.hstack([R, C])
# Triangulate the points
points = cv2.triangulatePoints(P1, P2, pts0.T, pts1.T)
points /= points[3]
points = points[:3].T
return points