forked from niconielsen32/ComputerVision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visual_odometry.py
247 lines (202 loc) · 8.14 KB
/
visual_odometry.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import os
import numpy as np
import cv2
from lib.visualization import plotting
from lib.visualization.video import play_trip
from tqdm import tqdm
class VisualOdometry():
def __init__(self, data_dir):
self.K, self.P = self._load_calib(os.path.join(data_dir, 'calib.txt'))
self.gt_poses = self._load_poses(os.path.join(data_dir,"poses.txt"))
self.images = self._load_images(os.path.join(data_dir,"image_l"))
self.orb = cv2.ORB_create(3000)
FLANN_INDEX_LSH = 6
index_params = dict(algorithm=FLANN_INDEX_LSH, table_number=6, key_size=12, multi_probe_level=1)
search_params = dict(checks=50)
self.flann = cv2.FlannBasedMatcher(indexParams=index_params, searchParams=search_params)
@staticmethod
def _load_calib(filepath):
"""
Loads the calibration of the camera
Parameters
----------
filepath (str): The file path to the camera file
Returns
-------
K (ndarray): Intrinsic parameters
P (ndarray): Projection matrix
"""
with open(filepath, 'r') as f:
params = np.fromstring(f.readline(), dtype=np.float64, sep=' ')
P = np.reshape(params, (3, 4))
K = P[0:3, 0:3]
return K, P
@staticmethod
def _load_poses(filepath):
"""
Loads the GT poses
Parameters
----------
filepath (str): The file path to the poses file
Returns
-------
poses (ndarray): The GT poses
"""
poses = []
with open(filepath, 'r') as f:
for line in f.readlines():
T = np.fromstring(line, dtype=np.float64, sep=' ')
T = T.reshape(3, 4)
T = np.vstack((T, [0, 0, 0, 1]))
poses.append(T)
return poses
@staticmethod
def _load_images(filepath):
"""
Loads the images
Parameters
----------
filepath (str): The file path to image dir
Returns
-------
images (list): grayscale images
"""
image_paths = [os.path.join(filepath, file) for file in sorted(os.listdir(filepath))]
return [cv2.imread(path, cv2.IMREAD_GRAYSCALE) for path in image_paths]
@staticmethod
def _form_transf(R, t):
"""
Makes a transformation matrix from the given rotation matrix and translation vector
Parameters
----------
R (ndarray): The rotation matrix
t (list): The translation vector
Returns
-------
T (ndarray): The transformation matrix
"""
T = np.eye(4, dtype=np.float64)
T[:3, :3] = R
T[:3, 3] = t
return T
def get_matches(self, i):
"""
This function detect and compute keypoints and descriptors from the i-1'th and i'th image using the class orb object
Parameters
----------
i (int): The current frame
Returns
-------
q1 (ndarray): The good keypoints matches position in i-1'th image
q2 (ndarray): The good keypoints matches position in i'th image
"""
# Find the keypoints and descriptors with ORB
kp1, des1 = self.orb.detectAndCompute(self.images[i - 1], None)
kp2, des2 = self.orb.detectAndCompute(self.images[i], None)
# Find matches
matches = self.flann.knnMatch(des1, des2, k=2)
# Find the matches there do not have a to high distance
good = []
try:
for m, n in matches:
if m.distance < 0.8 * n.distance:
good.append(m)
except ValueError:
pass
draw_params = dict(matchColor = -1, # draw matches in green color
singlePointColor = None,
matchesMask = None, # draw only inliers
flags = 2)
img3 = cv2.drawMatches(self.images[i], kp1, self.images[i-1],kp2, good ,None,**draw_params)
cv2.imshow("image", img3)
cv2.waitKey(200)
# Get the image points form the good matches
q1 = np.float32([kp1[m.queryIdx].pt for m in good])
q2 = np.float32([kp2[m.trainIdx].pt for m in good])
return q1, q2
def get_pose(self, q1, q2):
"""
Calculates the transformation matrix
Parameters
----------
q1 (ndarray): The good keypoints matches position in i-1'th image
q2 (ndarray): The good keypoints matches position in i'th image
Returns
-------
transformation_matrix (ndarray): The transformation matrix
"""
# Essential matrix
E, _ = cv2.findEssentialMat(q1, q2, self.K, threshold=1)
# Decompose the Essential matrix into R and t
R, t = self.decomp_essential_mat(E, q1, q2)
# Get transformation matrix
transformation_matrix = self._form_transf(R, np.squeeze(t))
return transformation_matrix
def decomp_essential_mat(self, E, q1, q2):
"""
Decompose the Essential matrix
Parameters
----------
E (ndarray): Essential matrix
q1 (ndarray): The good keypoints matches position in i-1'th image
q2 (ndarray): The good keypoints matches position in i'th image
Returns
-------
right_pair (list): Contains the rotation matrix and translation vector
"""
def sum_z_cal_relative_scale(R, t):
# Get the transformation matrix
T = self._form_transf(R, t)
# Make the projection matrix
P = np.matmul(np.concatenate((self.K, np.zeros((3, 1))), axis=1), T)
# Triangulate the 3D points
hom_Q1 = cv2.triangulatePoints(self.P, P, q1.T, q2.T)
# Also seen from cam 2
hom_Q2 = np.matmul(T, hom_Q1)
# Un-homogenize
uhom_Q1 = hom_Q1[:3, :] / hom_Q1[3, :]
uhom_Q2 = hom_Q2[:3, :] / hom_Q2[3, :]
# Find the number of points there has positive z coordinate in both cameras
sum_of_pos_z_Q1 = sum(uhom_Q1[2, :] > 0)
sum_of_pos_z_Q2 = sum(uhom_Q2[2, :] > 0)
# Form point pairs and calculate the relative scale
relative_scale = np.mean(np.linalg.norm(uhom_Q1.T[:-1] - uhom_Q1.T[1:], axis=-1)/
np.linalg.norm(uhom_Q2.T[:-1] - uhom_Q2.T[1:], axis=-1))
return sum_of_pos_z_Q1 + sum_of_pos_z_Q2, relative_scale
# Decompose the essential matrix
R1, R2, t = cv2.decomposeEssentialMat(E)
t = np.squeeze(t)
# Make a list of the different possible pairs
pairs = [[R1, t], [R1, -t], [R2, t], [R2, -t]]
# Check which solution there is the right one
z_sums = []
relative_scales = []
for R, t in pairs:
z_sum, scale = sum_z_cal_relative_scale(R, t)
z_sums.append(z_sum)
relative_scales.append(scale)
# Select the pair there has the most points with positive z coordinate
right_pair_idx = np.argmax(z_sums)
right_pair = pairs[right_pair_idx]
relative_scale = relative_scales[right_pair_idx]
R1, t = right_pair
t = t * relative_scale
return [R1, t]
def main():
data_dir = "KITTI_sequence_2" # Try KITTI_sequence_2 too
vo = VisualOdometry(data_dir)
play_trip(vo.images) # Comment out to not play the trip
gt_path = []
estimated_path = []
for i, gt_pose in enumerate(tqdm(vo.gt_poses, unit="pose")):
if i == 0:
cur_pose = gt_pose
else:
q1, q2 = vo.get_matches(i)
transf = vo.get_pose(q1, q2)
cur_pose = np.matmul(cur_pose, np.linalg.inv(transf))
gt_path.append((gt_pose[0, 3], gt_pose[2, 3]))
estimated_path.append((cur_pose[0, 3], cur_pose[2, 3]))
plotting.visualize_paths(gt_path, estimated_path, "Visual Odometry", file_out=os.path.basename(data_dir) + ".html")
if __name__ == "__main__":
main()