-
Notifications
You must be signed in to change notification settings - Fork 1
/
kitti_caliberation.py
76 lines (65 loc) · 2.81 KB
/
kitti_caliberation.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
import os
import cv2
import numpy as np
class KittiCalibration:
"""
Perform different types of calibration between camera & LIDAR
image = Projection * Camera3D_after_rectification
image = Projection * R_Rectification * Camera3D_reference
"""
def __init__(self, calib_path, from_video=False):
self.calib_path = calib_path
self.calib_matrix = {}
if from_video:
self.calib_matrix = self.parse_calib_from_video(calib_path)
self.calib_path = os.path.join(calib_path, "modified_calib_file.txt")
print('#################', self.calib_path)
else:
self.calib_matrix = self.parse_calib_files(calib_path)
self.P0 = self.calib_matrix["P0"]
self.P1 = self.calib_matrix["P1"]
# Projection Matrix (Intrensic) .. from camera 3d (after rectification) to image coord.
self.P2 = self.calib_matrix["P2"].reshape(3, 4)
self.P3 = self.calib_matrix["P3"]
# rectification rotation matrix 3x3
self.R0_rect = self.calib_matrix["R0_rect"].reshape(3,3)
# Extrensic Transilation-Rotation Matrix from LIDAR to Cam ref(before rectification)
self.Tr_velo_to_cam = self.calib_matrix["Tr_velo_to_cam"].reshape(3,4)
def parse_calib_files(self, calib_path):
assert self.calib_path is not None
mat_ = {}
with open(os.path.join(calib_path), 'r') as calib_file:
for line in calib_file:
line = line.split()
# Avoiding empty line exception
if len(line) == 0:
continue
# The only non-float values in these files are dates, which
# we don't care about anyway
try:
mat_[line[0][:-1]] = np.array(line[1:], dtype=np.float32)
except ValueError:
continue
return mat_
def parse_calib_from_video(self, calib_path):
""" Read calibration for camera 2 from video calib files.
there are calib_cam_to_cam and calib_velo_to_cam under the calib_root_dir
"""
assert calib_path is not None
mat_ = {}
cam2cam = self.parse_calib_files(
os.path.join(calib_path, "calib_cam_to_cam.txt")
)
velo2cam = self.parse_calib_files(
os.path.join(calib_path, "calib_velo_to_cam.txt")
)
mat_["P0"] = cam2cam["P_rect_00"]
mat_["P1"] = cam2cam["P_rect_01"]
mat_["P2"] = cam2cam["P_rect_02"]
mat_["P3"] = cam2cam["P_rect_03"]
mat_["R0_rect"] = cam2cam["R_rect_00"]
Tr_velo_to_cam = np.zeros((3, 4))
Tr_velo_to_cam[0:3, 0:3] = np.reshape(velo2cam["R"], [3, 3])
Tr_velo_to_cam[:, 3] = velo2cam["T"]
mat_["Tr_velo_to_cam"] = Tr_velo_to_cam
return mat_