Skip to content

Instantly share code, notes, and snippets.

@HViktorTsoi
Created February 27, 2025 14:12
Show Gist options
  • Save HViktorTsoi/0086206fc3708abd93e0b06bacfe9dac to your computer and use it in GitHub Desktop.
Save HViktorTsoi/0086206fc3708abd93e0b06bacfe9dac to your computer and use it in GitHub Desktop.
Write Nx4 numpy arrary point cloud (x, y, z, intensity) to PCD
def write_pcd_binary(points, filename):
"""
将Nx4的点云数据保存为PCD二进制格式
points: Nx4的numpy array,每行为(x,y,z,intensity)
filename: 保存的文件名
"""
n_points = len(points)
# PCD文件头
header = f"""# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z intensity
SIZE 4 4 4 4
TYPE F F F F
COUNT 1 1 1 1
WIDTH {n_points}
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS {n_points}
DATA binary
"""
# 写入文件
with open(filename, 'wb') as f:
# 写入ASCII头
f.write(header.encode())
# 直接将整个numpy array写入为二进制
points = points.astype(np.float32)
f.write(points.tobytes())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment