-
Notifications
You must be signed in to change notification settings - Fork 4
/
kitti_registration.py
138 lines (116 loc) · 6.35 KB
/
kitti_registration.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
import argparse
import copy
import csv
import matplotlib.pyplot as plt
import numpy as np
import os
from mpl_toolkits.mplot3d import Axes3D
import open3d as o3d
def bbox2rt(bbox):
center = (bbox.min(0) + bbox.max(0)) / 2
bbox -= center
yaw = np.arctan2(bbox[3, 1] - bbox[0, 1], bbox[3, 0] - bbox[0, 0])
rotation = np.array([[np.cos(yaw), -np.sin(yaw), 0],
[np.sin(yaw), np.cos(yaw), 0],
[0, 0, 1]])
return rotation, center
def register(source, target, args):
residual = o3d.registration.TransformationEstimationPointToPoint()
criteria = o3d.registration.ICPConvergenceCriteria(max_iteration=args.max_iter)
# Align the centroids of the point clouds
source_points = np.array(source.points)
target_points = np.array(target.points)
source_center = np.mean(source_points, axis=0)
target_center = np.mean(target_points, axis=0)
source = o3d.geometry.PointCloud()
source.points = o3d.utility.Vector3dVector(source_points - source_center)
target = o3d.geometry.PointCloud()
target.points = o3d.utility.Vector3dVector(target_points - target_center)
result = o3d.registration.registration_icp(source, target, args.max_dist, np.eye(4), residual, criteria)
source_trans = copy.deepcopy(source)
source_trans.transform(result.transformation)
R = result.transformation[:3, :3]
t = result.transformation[:3, 3] + target_center - np.dot(source_center, R.T)
return R, t, np.array(source_trans.points), np.array(target.points)
def rotation_error(R1, R2):
cos = (np.trace(np.dot(R1, R2.T)) - 1) / 2
cos = np.maximum(np.minimum(cos, 1), -1)
return 180 * np.arccos(cos) / np.pi
def translation_error(t1, t2):
return np.sqrt(np.sum((t1 - t2) ** 2))
def plot_pcd_pair(ax, pcd1, pcd2, title, cmaps, size, xlim=(-1.5, 1.5), ylim=(-1.5, 1.5), zlim=(-1, 2)):
ax.scatter(pcd1[:, 0], pcd1[:, 1], pcd1[:, 2], c=pcd1[:, 0], s=size, cmap=cmaps[0], vmin=-5, vmax=1.5)
ax.scatter(pcd2[:, 0], pcd2[:, 1], pcd2[:, 2], c=pcd2[:, 0], s=size, cmap=cmaps[1], vmin=-5, vmax=1.5)
ax.set_title(title)
ax.set_axis_off()
ax.set_xlim(xlim)
ax.set_ylim(ylim)
ax.set_zlim(zlim)
def track(args):
os.makedirs(os.path.join(args.saving_path, 'visu', 'kitti', 'registration', 'plots'), exist_ok=True)
csv_file = open(os.path.join(args.saving_path, 'visu', 'kitti', 'registration', 'error.csv'), 'w')
writer = csv.writer(csv_file)
writer.writerow(['id', 'r_err_part', 't_err_part', 'r_err_comp', 't_err_comp'])
n = 0
total_r_err_part = 0
total_t_err_part = 0
total_r_err_comp = 0
total_t_err_comp = 0
for filename in os.listdir(os.path.join(args.dataset_path, 'tracklets')):
tracklet_id = filename.split('.')[0]
with open(os.path.join(args.dataset_path, 'tracklets', filename)) as file:
car_ids = file.read().splitlines()
prev_frame = int(car_ids[0].split('_')[1])
prev_R, prev_t = bbox2rt(np.loadtxt(os.path.join(args.dataset_path, 'bboxes', '%s.txt' % car_ids[0])))
prev_partial = o3d.io.read_point_cloud(os.path.join(args.dataset_path, 'cars', '%s.pcd' % car_ids[0]))
prev_complete = o3d.io.read_point_cloud(
os.path.join(args.saving_path, 'visu', 'kitti', 'completions', '%s.pcd' % car_ids[0]))
for i in range(args.interval, len(car_ids), args.interval):
n += 1
frame = int(car_ids[i].split('_')[1])
instance_id = '%s_frame_%d_to_%d' % (tracklet_id, prev_frame, frame)
R, t = bbox2rt(np.loadtxt(os.path.join(args.dataset_path, 'bboxes', '%s.txt' % car_ids[i])))
R_gt = np.dot(R, prev_R.T)
t_gt = t - np.dot(prev_t, R_gt.T)
partial = o3d.io.read_point_cloud(os.path.join(args.dataset_path, 'cars', '%s.pcd' % car_ids[i]))
R_part, t_part, partial_trans, partial_target = register(prev_partial, partial, args)
r_err_part = rotation_error(R_part, R_gt)
t_err_part = translation_error(t_part, t_gt)
total_r_err_part += r_err_part
total_t_err_part += t_err_part
complete = o3d.io.read_point_cloud(
os.path.join(args.saving_path, 'visu', 'kitti', 'completions', '%s.pcd' % car_ids[i]))
R_comp, t_comp, complete_trans, complete_target = register(prev_complete, complete, args)
r_err_comp = rotation_error(R_comp, R_gt)
t_err_comp = translation_error(t_comp, t_gt)
total_r_err_comp += r_err_comp
total_t_err_comp += t_err_comp
writer.writerow([instance_id, r_err_part, t_err_part, r_err_comp, t_err_comp])
if n % args.plot_freq == 0:
fig = plt.figure(figsize=(8, 4))
ax = fig.add_subplot(121, projection='3d')
plot_pcd_pair(ax, partial_trans, partial_target,
'Rotation error %.4f\nTranslation error %.4f' % (r_err_part, t_err_part),
['Reds', 'Blues'], size=5)
ax = fig.add_subplot(122, projection='3d')
plot_pcd_pair(ax, complete_trans, complete_target,
'Rotation error %.4f\nTranslation error %.4f' % (r_err_comp, t_err_comp),
['Reds', 'Blues'], size=0.5)
plt.subplots_adjust(left=0, right=1, bottom=0, top=0.95, wspace=0)
fig.savefig(
os.path.join(args.saving_path, 'visu', 'kitti', 'registration', 'plots', '%s.png' % instance_id))
plt.close(fig)
print('Using original pcd: average rotation error %.4f average translation error %.4f' %
(total_r_err_part / n, total_t_err_part / n))
print('Using completed pcd: average roration error %.4f average translation error %.4f' %
(total_r_err_comp / n, total_t_err_comp / n))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--dataset_path')
parser.add_argument('--saving_path', help="model_log_file_path")
parser.add_argument('--interval', type=int, default=1, help='number of frames to skip')
parser.add_argument('--max_iter', type=int, default=100, help='max iteration for ICP')
parser.add_argument('--max_dist', type=float, default=0.05, help='matching threshold for ICP')
parser.add_argument('--plot_freq', type=int, default=100)
args = parser.parse_args()
track(args)