forked from merlinwu/end_to_end_visual_odometry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess_lidar.py
64 lines (49 loc) · 2.5 KB
/
preprocess_lidar.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
# this script is to convert stored x y z reflectance lidar scans into images, and store them in a pickle for use when training
import pykitti
import numpy as np
from scipy.stats import binned_statistic
import pickle
base_dir = "/media/bapskiko/SpinDrive/kitti/dataset/"
sequences = ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10"]
# channel, height, width
img_shape = [64, 1152]
img_channels = [2]
enc_angles = np.linspace(-np.pi, np.pi, num=(img_shape[1] + 1), endpoint=False)
for seq in sequences:
data = pykitti.odometry(base_dir, seq)
length = [len(data.poses)]
images = np.zeros(length + img_channels + img_shape, dtype=np.float32)
scan_idx = 0
#first need to convert each xyz
for scan in data.velo:
theta = np.arctan2(scan[:, 1], scan[:, 0])
xy = np.sqrt(np.square(scan[:, 0]) + np.square(scan[:, 1]))
az = np.arctan2(scan[:, 2], xy)
velo_start = np.min(az)
velo_end = np.max(az)
spacing = (velo_end - velo_start) / 63
az = np.rint((az - velo_start)/spacing).astype(np.int16)
dist = np.sqrt(np.square(xy) + np.square(scan[:,2]))
for i in range(0, 64):
if len(theta[az == i]) == 0:
images[scan_idx, 0, 63 - i, :] = np.max(dist)
images[scan_idx, 1, 63 - i, :] = 0
else:
strip_mean = binned_statistic(theta[az == i], [dist[az == i], scan[az == i, 3]], statistic='mean', bins=enc_angles)
mask = np.isnan(strip_mean.statistic[0])
for j in range(0, 2):
images[scan_idx, j, 63-i, mask] = np.interp(np.flatnonzero(mask), np.flatnonzero(~mask), strip_mean.statistic[j, ~mask])
images[scan_idx, j, 63-i, ~mask] = strip_mean.statistic[j, ~mask]
if scan_idx % 100 == 0:
print("Loading sequence %s %.1f%% " % (seq, (scan_idx / len(data.poses)) * 100))
scan_idx = scan_idx + 1
# save sequence to a pickle
range_out = open("/home/bapskiko/git/end_to_end_visual_odometry/pickles/" + str(seq) + "_range.pik", "wb")
int_out = open("/home/bapskiko/git/end_to_end_visual_odometry/pickles/" + str(seq) + "_intensity.pik", "wb")
for i in range(0, len(data.poses)):
pickle.dump(images[i, 0, :, :].astype(np.float16), range_out)
pickle.dump((images[i, 1, :, :] * 255.0).astype(np.uint8), int_out)
if i % 100 == 0:
print("Saving sequence %s %.1f%% " % (seq, (i / len(data.poses)) * 100))
range_out.close()
int_out.close()