How do I convert nuScenes lidar data to .pcd file

Since all lidar data are .pcd.bin, how do I convert lidar files(.pcd.bin) to .pcd format, thanks!
Following are the steps I get from google, not sure if its right…
It would help a lot if someone could point out any error of these steps!

step 1. Load binary point cloud

scan = np.fromfile(file_path, dtype=np.float32)

step 2. Reshape and drop reflection values

points = scan.reshape((-1, 5))[:, :4]

step 3. Convert to Open3D point cloud

o3d_pcd = o3d.geometry.PointCloud(o3d.utility.Vector3dVector(points))

step 4. Save to whatever format you like“pointcloud.pcd”, o3d_pcd)

and I also wonder why the point should transpose at last (points.T) from the answer here

@chiu_kevin doing point.T is simply to reshape the points into the required shape needed for the application in the post you linked above

For your application (to convert from .pcd.bin to .pcd), you could perhaps you can try something like this:

Based on the above stackoverflow answers, here’s a possible code snippet you could try:

import os
import struct

import numpy as np
from nuscenes.nuscenes import NuScenes
from nuscenes.utils.data_classes import LidarPointCloud
import open3d as o3d

nusc = NuScenes(version='v1.0-mini', dataroot='/data/sets/nuscenes', verbose=False)

# Get some random .pcd.bin file from nuScenes.
pcd_bin_file = os.path.join(nusc.dataroot, nusc.get('sample_data', '9d9bf11fb0e144c8b446d54a8a00184f')['filename'])

# Load the .pcd.bin file.
pc = LidarPointCloud.from_file(pcd_bin_file)
bin_pcd = pc.points.T

# Reshape and get only values for x, y and z.
bin_pcd = bin_pcd.reshape((-1, 4))[:, 0:3]

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

# Save to a .pcd file."~/Desktop/test.pcd"), o3d_pcd)

# Read the saved .pcd file from the previous step.
pcd ="~/Desktop/test.pcd"))
out_arr = np.asarray(pcd.points)  

# Load the original point cloud data from nuScenes, and check that the saved .pcd matches the original data.
pc = LidarPointCloud.from_file(pcd_bin_file)
points = pc.points.T
assert np.array_equal(out_arr, points[:, :3])

Thanks a lot!!! You save my day!

1 Like