forked from bluerythem/pcn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kitti_registration.py
140 lines (117 loc) · 6.26 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
139
140
# Author: Wentao Yuan ([email protected]) 05/31/2018
import argparse
import copy
import csv
import matplotlib.pyplot as plt
import numpy as np
import os
from mpl_toolkits.mplot3d import Axes3D
from open3d import *
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 = TransformationEstimationPointToPoint()
criteria = 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 = PointCloud()
source.points = Vector3dVector(source_points - source_center)
target = PointCloud()
target.points = Vector3dVector(target_points - target_center)
result = 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.results_dir, 'plots'), exist_ok=True)
csv_file = open(os.path.join(args.results_dir, '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(args.tracklet_dir):
tracklet_id = filename.split('.')[0]
with open(os.path.join(args.tracklet_dir, 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.bbox_dir, '%s.txt' % car_ids[0])))
prev_partial = read_point_cloud(os.path.join(args.partial_dir, '%s.pcd' % car_ids[0]))
prev_complete = read_point_cloud(os.path.join(args.complete_dir, '%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.bbox_dir, '%s.txt' % car_ids[i])))
R_gt = np.dot(R, prev_R.T)
t_gt = t - np.dot(prev_t, R_gt.T)
partial = read_point_cloud(os.path.join(args.partial_dir, '%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 = read_point_cloud(os.path.join(args.complete_dir, '%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.results_dir, 'plots', '%s.png' % instance_id))
plt.close(fig)
print('Using original pcd: average roration 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('--partial_dir', default='data/kitti/cars')
parser.add_argument('--complete_dir', default='data/results/kitti/pcn_emd/completions')
parser.add_argument('--bbox_dir', default='data/kitti/bboxes')
parser.add_argument('--tracklet_dir', default='data/kitti/tracklets')
parser.add_argument('--results_dir', default='data/results/kitti_registration')
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)