-
Notifications
You must be signed in to change notification settings - Fork 13
/
lidar_to_depth.py
executable file
·53 lines (38 loc) · 1.68 KB
/
lidar_to_depth.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
import numpy as np
import os
import glob
import cv2
import pykitti
# Export Lidar Data to Depth Maps
# Change this to the directory where you store KITTI data
basedir = '/PATH/TO/YOUR/KITTIVO/DATA'
for sequence in ['00','01','02','03','04','05','06','07','08','09','10']:
# Specify the dataset to load
print('Sequence ' + sequence)
# Load the data. Optionally, specify the frame range to load.
# dataset = pykitti.odometry(basedir, sequence)
dataset = pykitti.odometry(basedir, sequence)
# for cam2_image in dataset.cam2:
for i in range(len(dataset)):
color = np.array(dataset.get_cam2(i))
img_width = color.shape[1]
img_height = color.shape[0]
depth = np.zeros([img_height,img_width])
velo = dataset.get_velo(i)
velo[:,-1] = 1
temp = dataset.calib.P_rect_20.dot(dataset.calib.T_cam0_velo)
results = temp.dot(velo.T)
uv = results[:2,:]/results[-1,:]
z = results[-1,:]
valid = (uv[0,:] > 0) & (np.round(uv[0,:]) < img_width) & (uv[1,:] > 0) &(np.round(uv[1,:]) < img_height) &(z>0)&(z<1000)
valid_index = np.round(uv[:,valid]).astype('uint32')
depth[valid_index[1],valid_index[0]] = z[valid]
file_name = dataset.velo_files[i]
out_name = file_name.replace('sequences','RealDepth').replace('bin','png')
depth_to_write = depth.copy() * 256
depth_to_write[depth_to_write<0] = 0
depth_to_write[depth_to_write>65535] = 0
depth_to_write = depth_to_write.astype('uint16')
if not os.path.exists(os.path.dirname(out_name)):
os.makedirs(os.path.dirname(out_name))
cv2.imwrite(out_name,depth_to_write)