-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathransac.py
34 lines (26 loc) · 1.05 KB
/
ransac.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
import numpy as np
# import pdb
from dlt import DLT_method, find_projection_error, decompose_projection
def ransac_estimation(image_points, world_points, iter=500):
"""Use the RANSAC algorithm with DLT to find best projection matrix"""
n_points = image_points.shape[0]
best_P = None
min_error = np.inf
for i in range(iter):
print("Iteration number {}".format(i))
indices = np.random.permutation(n_points)
X, Y = image_points[indices[0:6]], world_points[indices[0:6]]
X_test, Y_test = image_points[indices[6::]], world_points[indices[6::]]
projection_matrix = DLT_method(X, Y)
error = find_projection_error(projection_matrix, X_test, Y_test)
if error < min_error:
min_error = error
best_P = projection_matrix
return best_P
if __name__ == "__main__":
from points import image_points, world_points
projection_matrix = ransac_estimation(image_points, world_points)
K, R, C = decompose_projection(projection_matrix)
print(K)
print(R)
print(C)