'Conversion of binary lidar data (.bin) to point cloud data (.pcd) format

I have a lidar data collected with Velodyne-128 in .bin format. I need to convert it into pcd format. I use NVIDIA Driveworks for data manipulation but there is no tool to convert lidar binary data into pcd.

Thus, is there a method to convert binary lidar data into pcd format?



Solution 1:[1]

Here is a snippet to converting a lidar data (in .bin format) into .pcd format

with open ("lidar_velodyne64.bin", "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 = o3d.geometry.PointCloud()
v3d = o3d.utility.Vector3dVector
pcd.points = v3d(np_pcd)

According to the example here, you can save it to file it as below:

import open3d as o3d
o3d.io.write_point_cloud("copy_of_fragment.pcd", pcd)

Solution 2:[2]

Although all the other solutions are probably correct, I was looking for a simple numpy-only version. Here it is:

import numpy as np
import open3d as o3d

# Load binary point cloud
bin_pcd = np.fromfile("lidar_velodyne64.bin", dtype=np.float32)

# Reshape and drop reflection values
points = bin_pcd.reshape((-1, 4))[:, 0:3]

# Convert to Open3D point cloud
o3d_pcd = o3d.geometry.PointCloud(o3d.utility.Vector3dVector(points))

# Save to whatever format you like
o3d.io.write_point_cloud("pointcloud.pcd", o3d_pcd)

Solution 3:[3]

I found a code from github (reference is given below):

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 = geometry.PointCloud()
    pcd.points = utility.Vector3dVector(np_pcd)
    return pcd

Reference:https://gist.githubusercontent.com/HTLife/e8f1c4ff1737710e34258ef965b48344/raw/76e15821e7cd45cac672dbb1f14d577dc9be5ff8/convert_kitti_bin_to_pcd.py

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 rayon
Solution 3 Community