Skip to content

Instantly share code, notes, and snippets.

@HTLife
Created February 11, 2019 11:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save HTLife/e8f1c4ff1737710e34258ef965b48344 to your computer and use it in GitHub Desktop.
Save HTLife/e8f1c4ff1737710e34258ef965b48344 to your computer and use it in GitHub Desktop.
Convert KITTI velodyne bin to open3d pcd
import numpy as np
import struct
from open3d import *
def convert_kitti_bin_to_pcd(binFilePath):
size_float = 4
list_pcd = []
with open(binFilePath, "rb") as f:
byte = f.read(size_float * 4)
while byte:
x, y, z, intensity = struct.unpack("ffff", byte)
list_pcd.append([x, y, z])
byte = f.read(size_float * 4)
np_pcd = np.asarray(list_pcd)
pcd = PointCloud()
pcd.points = Vector3dVector(np_pcd)
return pcd
@aldakata
Copy link

Hi! I would like to use this piece of code but i get:
NameError Traceback (most recent call last)
in
----> 1 convert_kitti_bin_to_pcd(path)

in convert_kitti_bin_to_pcd(binFilePath)
13 byte = f.read(size_float * 4)
14 np_pcd = np.asarray(list_pcd)
---> 15 pcd = PointCloud()
16 pcd.points = Vector3dVector(np_pcd)
17 return pcd

NameError: name 'PointCloud' is not defined

What import am I missing? Thank you!

@sammilei
Copy link

import numpy as np
import struct
from open3d import *

def convert_kitti_bin_to_pcd(binFilePath):
    size_float = 4
    list_pcd = []
    with open(binFilePath, "rb") as f:
        byte = f.read(size_float * 4)
        while byte:
            x, y, z, intensity = struct.unpack("ffff", byte)
            list_pcd.append([x, y, z])
            byte = f.read(size_float * 4)
    np_pcd = np.asarray(list_pcd)
    pcd = open3d.geometry.PointCloud()
    pcd.points = open3d.utility.Vector3dVector(np_pcd)
    return pcd

# source
bin_file = '1644609772_916356000.bin'

# convert
pcd_ = convert_kitti_bin_to_pcd(bin_file)

# show
print(pcd_)
print(np.asarray(pcd_.points))
open3d.visualization.draw_geometries([pcd_])

# save
open3d.io.write_point_cloud('1644609772_916356000.pcd', pcd_, write_ascii=False, compressed=False, print_progress=False)

@sammilei
Copy link

image (16)

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