Skip to content

Instantly share code, notes, and snippets.

@ronrest
Last active August 14, 2023 03:12
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save ronrest/08abaf42930473f9e3e2dbadad5c92fb to your computer and use it in GitHub Desktop.
Save ronrest/08abaf42930473f9e3e2dbadad5c92fb to your computer and use it in GitHub Desktop.
Visualize Lidar Data in Kitti Data
"""
VISUALISE THE LIDAR DATA FROM THE KITTI DATASET
Based on the sample code from
https://github.com/utiasSTARS/pykitti/blob/master/demos/demo_raw.py
And:
http://stackoverflow.com/a/37863912
Contains two methods of visualizing lidar data interactively.
- Matplotlib - very slow, and likely to crash, so only 1 out of every 100
points are plotted.
- Also, data looks VERY distorted due to auto scaling along
each axis. (this could potentially be edited)
- Mayavi - Much faster, and looks nicer.
- Preserves actual scale along each axes so items look
recognizable
"""
import pykitti # install using pip install pykitti
import os
import numpy as np
# Chose which visualization library to use: "mayavi" or "matplotlib"
# VISLIB = "mayavi"
VISLIB = "matplotlib"
# Raw Data directory information
basedir = '/home/dodo_brain/kitti_data/'
date = '2011_09_26'
drive = '0005'
# Optionally, specify the frame range to load
# since we are only visualizing one frame, we will restrict what we load
# Set to None to use all the data
frame_range = range(150, 151, 1)
# Load the data
dataset = pykitti.raw(basedir, date, drive, frame_range)
# Load Lidar Data
dataset.load_velo() # Each scan is a Nx4 array of [x,y,z,reflectance]
# Plot only the ith frame (out of what has been loaded)
i = 0
velo = dataset.velo[i]
if VISLIB == "mayavi":
# Plot using mayavi -Much faster and smoother than matplotlib
import mayavi.mlab
fig = mayavi.mlab.figure(bgcolor=(0, 0, 0), size=(640, 360))
mayavi.mlab.points3d(
velo[:, 0], # x
velo[:, 1], # y
velo[:, 2], # z
velo[:, 2], # Height data used for shading
mode="point", # How to render each point {'point', 'sphere' , 'cube' }
colormap='spectral', # 'bone', 'copper',
#color=(0, 1, 0), # Used a fixed (r,g,b) color instead of colormap
scale_factor=100, # scale of the points
line_width=10, # Scale of the line, if any
figure=fig,
)
# velo[:, 3], # reflectance values
mayavi.mlab.show()
else:
# Plot Using Matplotlib - Much slower than mayavi.
# NOTE: Only 1 out of every 100 points are plotted using the matplotlib
# version to prevent crashing the computer
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
skip = 100 # plot one in every `skip` points
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
velo_range = range(0, velo.shape[0], skip) # skip points to prevent crash
ax.scatter(velo[velo_range, 0], # x
velo[velo_range, 1], # y
velo[velo_range, 2], # z
c=velo[velo_range, 3], # reflectance
cmap='gray')
ax.set_title('Lidar scan (subsampled)')
plt.show()
@JeremyCKT
Copy link

When I run this, the error occurs:
File "kitti_lidar.py", line 37, in
dataset = pykitti.raw(basedir, date, drive, frame_range)
TypeError: init() takes 4 positional arguments but 5 were given
How do I fix it?

@MSiam
Copy link

MSiam commented Jan 9, 2018

I also had the same error pykitti has changed at least the version which I have and u probably have as well.
Use this will work:
dataset = pykitti.raw(basedir, date, drive)

Also no function for load_velo() so remove line 40.
And dataset.velo is a generator now u can iterate on it:
for velo in dataset.velo:
do whatever u want with the velo pts...

@liemwellys
Copy link

liemwellys commented Jul 17, 2018

Hi, How do you set up the folder for dataset downloaded from KITTI website? Because when I try to load the velodyne point using method dataset.get_velo(0), it has error:

Traceback (most recent call last):
File "D:/Documents/Python Projects/kitti velodyne/kitti5.py", line 26, in
print(data.get_cam0(0))
File "C:\Users\Welly\Anaconda3\envs\py36\lib\site-packages\pykitti\raw.py", line 48, in get_cam0
return utils.load_image(self.cam0_files[idx], mode='L')
IndexError: list index out of range

my configuration is like this:
basedir = 'data'
date = '2011_09_26'
drive = '0005'

@lawrencekiba
Copy link

I have the same issue as @liemwellys, and while using dataset.velo[i] like in the original code, I got the error on the generator object not being suscriptable.

@e2sh83
Copy link

e2sh83 commented Jan 4, 2019

Change like below and work fine for me.

dataset = pykitti.raw(basedir, date, drive, frames=frame_range)

velo = dataset.get_velo(0) # Each scan is a Nx4 array of [x,y,z,reflectance]

@VijeetBenni
Copy link

What is the range for reflectance value in kitti dataset?
I mean what the range of values it varies between?

@lngpai
Copy link

lngpai commented May 28, 2020

Change like below and work fine for me.

dataset = pykitti.raw(basedir, date, drive, frames=frame_range)

velo = dataset.get_velo(0) # Each scan is a Nx4 array of [x,y,z,reflectance]
Even with the above change, I get the eror as follows.

File "kitti_lidar.py", line 41, in
velo = dataset.get_velo(0) # Each scan is a Nx4 array of [x,y,z,reflectance]
File "/home/lng/.local/lib/python3.5/site-packages/pykitti/raw.py", line 107, in get_velo
return utils.load_velo_scan(self.velo_files[idx])
IndexError: list index out of range

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment