-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonoVideoOdometry.py
106 lines (79 loc) · 4.25 KB
/
MonoVideoOdometry.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
import cv2
class MonoVideoOdometery(object):
def __init__(self,
mtx,
first_frame,
second_frame,
abs_distance,
lk_params=dict(winSize = (21,21), criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 30, 0.01)),
detector=cv2.FastFeatureDetector_create(threshold=25, nonmaxSuppression=True)):
'''
Arguments:
img_file_path {str} -- File path that leads to image sequences
pose_file_path {str} -- File path that leads to true poses from image sequence
Keyword Arguments:
focal_length {float} -- Focal length of camera used in image sequence (default: {718.8560})
pp {tuple} -- Principal point of camera in image sequence (default: {(607.1928, 185.2157)})
lk_params {dict} -- Parameters for Lucas Kanade optical flow (default: {dict(winSize = (21,21), criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 30, 0.01))})
detector {cv2.FeatureDetector} -- Most types of OpenCV feature detectors (default: {cv2.FastFeatureDetector_create(threshold=25, nonmaxSuppression=True)})
Raises:
ValueError -- Raised when file either file paths are not correct, or img_file_path is not configured correctly
'''
self.detector = detector
self.lk_params = lk_params
self.mtx = mtx
self.old_frame = first_frame
self.R = np.zeros(shape=(3, 3))
self.t = np.zeros(shape=(3, 3))
self.init = True
self.n_features = 0
self.step(second_frame, abs_distance)
def _detect(self, img):
'''Used to detect features and parse into useable format
Arguments:
img {np.ndarray} -- Image for which to detect keypoints on
Returns:
np.array -- A sequence of points in (x, y) coordinate format
denoting location of detected keypoint
'''
p0 = self.detector.detect(img)
return np.array([x.pt for x in p0], dtype=np.float32).reshape(-1, 1, 2)
def step(self, current_frame, abs_distance):
'''
Used to perform visual odometery. If features fall out of frame
such that there are less than 2000 features remaining, a new feature
detection is triggered.
'''
if self.n_features < 2000:
self.p0 = self._detect(self.old_frame)
# Calculate optical flow between frames, st holds status
# of points from frame to frame
self.p1, st, err = cv2.calcOpticalFlowPyrLK(self.old_frame, current_frame, self.p0, None, **self.lk_params)
# Save the good points from the optical flow
self.good_old = self.p0[st == 1]
self.good_new = self.p1[st == 1]
# If the frame is one of first two, we need to initalize
# our t and R vectors so behavior is different
if self.init:
E, _ = cv2.findEssentialMat(self.good_new, self.good_old, self.mtx, method=cv2.RANSAC, prob=0.999, threshold=1.0, mask=None)
_, self.R, self.t, _ = cv2.recoverPose(E, self.good_old, self.good_new, cameraMatrix=self.mtx, R=self.R, t=self.t, mask=None)
self.init = False
else:
E, _ = cv2.findEssentialMat(self.good_new, self.good_old, self.mtx, method=cv2.RANSAC, prob=0.999, threshold=1.0, mask=None)
_, R, t, _ = cv2.recoverPose(E, self.good_old, self.good_new, cameraMatrix=self.mtx, R=self.R.copy(), t=self.t.copy(), mask=None)
if (abs_distance > 0.1 and abs(t[2][0]) > abs(t[0][0]) and abs(t[2][0]) > abs(t[1][0])):
self.t = self.t + abs_distance * self.R.dot(t)
self.R = R.dot(self.R)
# Save the total number of good features
self.n_features = self.good_new.shape[0]
self.old_frame = current_frame
return self.get_mono_coordinates()
def get_mono_coordinates(self):
# We multiply by the diagonal matrix to fix our vector
# onto same coordinate axis as true values
diag = np.array([[-1, 0, 0],
[0, -1, 0],
[0, 0, -1]])
adj_coord = np.matmul(diag, self.t)
return adj_coord.flatten()