-
Notifications
You must be signed in to change notification settings - Fork 8
/
relative_pose_tests.py
127 lines (101 loc) · 4.5 KB
/
relative_pose_tests.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import pytheia as pt
import numpy as np
from noise_utils import add_noise_to_point
from scipy.spatial.transform import Rotation as R
def perspective_division(points):
return np.divide(points.T, points[:, 2])[0:2, :].T
def generate_image_points(points3d,
projection_noise_std,
expected_rotation,
expected_translation):
image_points1 = perspective_division(points3d)
rotated_points_3d = expected_rotation @ points3d.T + \
np.expand_dims(expected_translation, 1)
image_points2 = perspective_division(rotated_points_3d.T)
image_points1 = add_noise_to_point(image_points1, projection_noise_std)
image_points2 = add_noise_to_point(image_points2, projection_noise_std)
return image_points1, image_points2
def test_8ptFundamentalMatrix(pts0, pts1):
success, F = pt.sfm.NormalizedEightPointFundamentalMatrix(pts0, pts1)
assert success
def test_5ptEssentialMatrix(image_pts_1, image_pts_2, t_gt, max_error_deg):
success, Es = pt.sfm.FivePointRelativePose(
image_pts_1[:5, :], image_pts_2[:5, :])
normalized_corrs = []
for i in range(image_pts_1.shape[0]):
normalized_corrs.append(pt.matching.FeatureCorrespondence(
pt.sfm.Feature(image_pts_1[i, :]), pt.sfm.Feature(image_pts_2[i, :])))
norm_t_gt = t_gt / np.linalg.norm(t_gt)
# find closest to GT
min_t_dist = 100000
sol_idx = 0
for sol in range(len(Es)):
pose_res = pt.sfm.GetBestPoseFromEssentialMatrix(
Es[sol], normalized_corrs)
translation_est = -pose_res[1] @ pose_res[2]
translation_est /= np.linalg.norm(translation_est)
t_dist = np.arccos(np.dot(translation_est, norm_t_gt))
if t_dist < min_t_dist:
min_t_dist = t_dist
sol_idx = sol
pose_res = pt.sfm.GetBestPoseFromEssentialMatrix(
Es[sol_idx], normalized_corrs)
translation_est = -pose_res[1] @ pose_res[2]
t_angle = np.arccos(np.dot(translation_est, norm_t_gt)) * 180. / np.pi
r_dist = np.linalg.norm(R.from_matrix(pose_res[1].T @ R_gt).as_rotvec())
assert success
assert t_angle < max_error_deg
assert r_dist < max_error_deg
def test_EstimateRelativeOrientation(image_pts_1, image_pts_2, R_gt, t_gt, max_error_deg):
params = pt.solvers.RansacParameters()
params.error_thresh = 1e-4
params.max_iterations = 20
params.min_iterations = 1
# create normalized correspondences
normalized_corrs = []
for i in range(image_pts_1.shape[0]):
normalized_corrs.append(pt.matching.FeatureCorrespondence(
pt.sfm.Feature(image_pts_1[i, :]), pt.sfm.Feature(image_pts_2[i, :])))
success, rel_ori, ransac_sum = pt.sfm.EstimateRelativePose(
params, pt.sfm.RansacType(0), normalized_corrs)
r_dist = np.linalg.norm(R.from_matrix(
rel_ori.rotation.T @ R_gt).as_rotvec())
print(r_dist)
assert success
assert r_dist < max_error_deg
if __name__ == "__main__":
points_3d = np.array([[-1.0, 3.0, 3.0],
[-1.0, 1.0, 3.0],
[5.0, 2.0, 1.0],
[-1.0, 1.0, 2.0],
[2.0, 1.0, 3.0],
[-1.0, -3.0, 2.0],
[1.0, -2.0, 1.0],
[-2.0, 2.0, 3.0]], dtype=np.float32)
R_gt = R.from_rotvec(13.0 * np.pi / 180
* np.array([0., 0., 1.])).as_matrix()
t_gt = np.array([1.0, 0.5, 0.2])
noise = 0.0 / 512.
# without noise
image_points_1, image_points_2 = generate_image_points(
points_3d, noise, R_gt, t_gt)
# check fundamtenal matrix estimation
test_8ptFundamentalMatrix(image_points_1, image_points_2)
# check essential matrix estimation
test_5ptEssentialMatrix(
image_points_1, image_points_2, t_gt, max_error_deg=1e-4)
# test relative orientation function
test_EstimateRelativeOrientation(
image_points_1, image_points_2, R_gt, t_gt, max_error_deg=1e-4)
# with noise
noise = 1.0 / 512.
image_points_1, image_points_2 = generate_image_points(
points_3d, noise, R_gt, t_gt)
# check fundamtenal matrix estimation
test_8ptFundamentalMatrix(image_points_1, image_points_2)
# check essential matrix estimation
test_5ptEssentialMatrix(
image_points_1, image_points_2, t_gt, max_error_deg=5.)
# test relative orientation function
test_EstimateRelativeOrientation(
image_points_1, image_points_2, R_gt, t_gt, max_error_deg=5.)