Skip to content

Instantly share code, notes, and snippets.

@RameshKamath
Forked from ronrest/kitti_lidar.py
Last active February 25, 2020 00:01
Show Gist options
  • Save RameshKamath/fd3a0e0ccd6adfd8e40d5b4dc9e52b79 to your computer and use it in GitHub Desktop.
Save RameshKamath/fd3a0e0ccd6adfd8e40d5b4dc9e52b79 to your computer and use it in GitHub Desktop.

Revisions

  1. RameshKamath revised this gist Apr 27, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion kitti_lidar.py
    Original file line number Diff line number Diff line change
    @@ -41,7 +41,7 @@


    else:
    # Plot Using Matplotlib - Much slower than mayavi and visuals are not as goog as mayavi.
    # Plot Using Matplotlib - Much slower than mayavi and visuals are not as good as mayavi.
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    fig = plt.figure()
  2. RameshKamath revised this gist Mar 15, 2018. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion kitti_lidar.py
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,6 @@
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    fig = plt.figure()
    fig.plot(range(10))

    # Raw Data directory information
    basedir = '/home/ramesh/Documents/kitti_data'
  3. RameshKamath revised this gist Mar 15, 2018. 1 changed file with 18 additions and 14 deletions.
    32 changes: 18 additions & 14 deletions kitti_lidar.py
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@
    if VISLIB == "mayavi":
    # Plot using mayavi -Much faster and smoother than matplotlib
    # is built for 3D scientific data visualization and plotting
    import mayavi.mlab
    from mayavi import mlab
    """
    I like to use python3 so i am giving installtion help for python3 (python2 might be similar):
    @@ -44,6 +44,8 @@
    # Plot Using Matplotlib - Much slower than mayavi and visuals are not as goog as mayavi.
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    fig = plt.figure()
    fig.plot(range(10))

    # Raw Data directory information
    basedir = '/home/ramesh/Documents/kitti_data'
    @@ -53,44 +55,46 @@
    # 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)
    frame_range = range(0, 151, 1)

    # Load the data
    dataset = pykitti.raw(basedir, date, drive, frame = frame_range)

    l=True #to loop or not
    n = 1 #keeping track of frames

    n = 1 #keeping track of frames
    # Plot only the ith frame (out of what has been loaded)
    i = 3

    #same as "velo = dataset.velo[i]"
    for velo in dataset.velo:
    if n == i:
    for velo in dataset.velo: #velo=[[x y z reflectance][....][....]....]
    if l:
    i=5 # changing limit of looped frame
    if n == i or l:
    if VISLIB == "mayavi":
    fig = mayavi.mlab.figure(bgcolor=(0, 0, 0), size=(640, 360))
    mayavi.mlab.points3d(
    fig = mlab.figure(bgcolor=(0, 0, 0), size=(640, 360))
    mlab.points3d(
    velo[:, 0], # x
    velo[:, 1], # y
    velo[:, 2], # z
    velo[:, 1], # y
    velo[:, 2], # z (height)
    velo[:, 2], # Height data used for shading
    #velo[:, 3], # reflectance values
    mode="point", # How to render each point {'point', 'sphere' , 'cube' }
    colormap='spectral', # 'bone', 'copper',
    colormap='spectral', #'bone', 'copper','spectral','hsv','hot','CMRmap','Blues'

    #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()
    mlab.show()
    if i==n:
    break
    n=n+1
    else:
    # NOTE: Only 1 out of every 100 points are plotted using the matplotlib
    # version to prevent crashing the computer
    skip = 50 # 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
  4. RameshKamath revised this gist Mar 15, 2018. 1 changed file with 71 additions and 48 deletions.
    119 changes: 71 additions & 48 deletions kitti_lidar.py
    Original file line number Diff line number Diff line change
    @@ -14,17 +14,39 @@
    - 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"
    VISLIB = "mayavi"
    #VISLIB = "matplotlib"

    if VISLIB == "mayavi":
    # Plot using mayavi -Much faster and smoother than matplotlib
    # is built for 3D scientific data visualization and plotting
    import mayavi.mlab
    """
    I like to use python3 so i am giving installtion help for python3 (python2 might be similar):
    pip3 install mayavi
    sudo apt-get install build-essential git cmake libqt4-dev libphonon-dev python3-dev libxml2-dev libxslt1-dev qtmobility-dev libqtwebkit-dev
    sudo apt-get install python3-pyqt4 (mayavi needs pyqt4 to work)
    """



    else:
    # Plot Using Matplotlib - Much slower than mayavi and visuals are not as goog as mayavi.
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D

    # Raw Data directory information
    basedir = '/home/dodo_brain/kitti_data/'
    basedir = '/home/ramesh/Documents/kitti_data'
    date = '2011_09_26'
    drive = '0005'

    @@ -34,52 +56,53 @@
    frame_range = range(150, 151, 1)

    # Load the data
    dataset = pykitti.raw(basedir, date, drive, frame_range)
    dataset = pykitti.raw(basedir, date, drive, frame = frame_range)

    # Load Lidar Data
    dataset.load_velo() # Each scan is a Nx4 array of [x,y,z,reflectance]

    n = 1 #keeping track of frames
    # 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()
    i = 3

    #same as "velo = dataset.velo[i]"
    for velo in dataset.velo:
    if n == i:
    if VISLIB == "mayavi":
    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()
    if i==n:
    break
    n=n+1
    else:
    # NOTE: Only 1 out of every 100 points are plotted using the matplotlib
    # version to prevent crashing the computer
    skip = 50 # 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()

    if i==n:
    break
    n=n+1
    else:
    n=n+1
  5. @ronrest ronrest revised this gist Mar 24, 2017. 1 changed file with 19 additions and 11 deletions.
    30 changes: 19 additions & 11 deletions kitti_lidar.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    """
    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:
    @@ -22,9 +24,9 @@
    VISLIB = "matplotlib"

    # Raw Data directory information
    basedir = '/home/dodo_brain/data/kitti_data'
    date = '2011_09_26' # Date of the rawdata trial
    drive = '0005' # Drive number of the raw data trial
    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
    @@ -45,17 +47,22 @@
    # 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
    mode="cube",
    color=(0, 1, 0),
    scale_factor=0.4
    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[velo_range, 3] # reflectance data

    # 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
    @@ -75,3 +82,4 @@
    cmap='gray')
    ax.set_title('Lidar scan (subsampled)')
    plt.show()

  6. @ronrest ronrest created this gist Mar 23, 2017.
    77 changes: 77 additions & 0 deletions kitti_lidar.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    """
    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/data/kitti_data'
    date = '2011_09_26' # Date of the rawdata trial
    drive = '0005' # Drive number of the raw data trial

    # 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

    mayavi.mlab.points3d(
    velo[:, 0], # x
    velo[:, 1], # y
    velo[:, 2], # z
    mode="cube",
    color=(0, 1, 0),
    scale_factor=0.4
    )
    # velo[velo_range, 3] # reflectance data

    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()