点云 3D 数据 读取、保存 - pypcd 包
1. 点云 3D 数据
点云 3D 数据 离线状况 通常以 “.pcd”、“.bin” 等格式存储,其中 “.pcd” 文件在 Ubuntu 下可以方便的使用 pcl_viewer 命令查看;而 “.bin” 格式 则可 以最少的 库依赖 完成对点云的读取保存。
2. Python 下 pypcd 库安装
- pypcd 网址:https://github.com/dimatura/pypcd
当前默认支持的是 python 2.* 环境, python 3.* 下的安装需要适配版本,不然使用过程中会报错
2.1 在 Python 2.* 下安装 pypcd
pip install pypcd
2.2 在 Python 3.* 下安装 pypcd
git clone https://github.com/dimatura/pypcd
cd pypcd
git fetch origin pull/9/head:python3
git checkout python3
python3 setup.py install
3. 点云 3D 数据 的读取、保存
3.1 “.pcd” 格式
import numpy as np
from pypcd import pypcd
file_path = "/home/hjw/point_cloud/test.pcd"
# read data
pcd = pypcd.PointCloud.from_path(file_path)
point_cloud = np.zeros((pcd.points, len(pcd.fields)), dtype=np.float32)
for i, field in enumerate(pcd.fields):
point_cloud[:, i] = np.transpose(pcd.pc_data[field])
# x, y, z, intensity
point_cloud = point_cloud[:, 0:4]
# store_path = "/home/hjw/point_cloud/test_save.pcd"
# # save as binary compressed
# pcd.save_pcd(store_path , compression='binary_compressed')
3.2 “.bin” 格式
- 单文件单线程读取
import numpy as np
file_path = "/home/hjw/point_cloud/test.bin"
# read data
point_cloud = np.fromfile(file_path , dtype=np.float32)
point_cloud = point_cloud.reshape((-1, 4))
# x, y, z, intensity
point_cloud = point_cloud[:, 0:4]
# store_path = "/home/hjw/point_cloud/test_save.bin"
# # save data
# point_cloud.tofile(store_path)
- 多线程文件序列读取
import time
import numpy as np
from concurrent.futures import ThreadPoolExecutor
def read_files_thread(files_seq, threads_num=4):
thread_pool = ThreadPoolExecutor(max_workers=threads_num)
def read_file(file_path):
point_cloud = np.fromfile(file_path, dtype=np.float32)
point_cloud = point_cloud.reshape((-1, 4))
return point_cloud
files_val_seq = [i for i in thread_pool.map(read_file, files_seq)]
thread_pool.shutdown()
return files_val_seq
def read_files(files_seq):
def read_file(file_path):
point_cloud = np.fromfile(file_path, dtype=np.float32)
point_cloud = point_cloud.reshape((-1, 4))
return point_cloud
files_val_seq = [read_file(i) for i in files_seq]
return files_val_seq
file_sequence = ['1.bin', '2.bin', '3.bin', '4.bin', '5.bin', '6.bin']
start_time = time.time()
method_1 = read_files_thread(file_sequence, 8)
print("method_1 time consume ", time.time() - start_time)
start_time = time.time()
method_2 = read_files(file_sequence)
print("method_2 time consume ", time.time() - start_time)
3.3 “.npy” 格式
import numpy as np
from pypcd import pypcd
file_path = "/home/hjw/point_cloud/test.npy"
# read data, point cloud shape N×4, (x, y, z, intensity)
point_cloud = np.load('test.npy')
xyzi = np.zeros((point_cloud.shape[0], point_cloud.shape[1]), dtype=np.float32)
xyzi[:, 0] = point_cloud[:, 0]
xyzi[:, 1] = point_cloud[:, 1]
xyzi[:, 2] = point_cloud[:, 2]
xyzi[:, 3] = point_cloud[:, 3]
xyzi = xyzi.view(np.dtype([('x', np.float32), ('y', np.float32), ('z', np.float32), ('intensity', np.float32)])).squeeze()
pcd = pypcd.PointCloud.from_array(xyzi)
store_path = "/home/hjw/point_cloud/test_save.pcd"
# save as binary compressed
pcd.save_pcd(store_path , compression='binary_compressed')
说明:此处的样例数据 test.npy 大小为 N×4,即 (x, y, z, intensity),对应 pcd 文件中的 fields,对于维度 大于4 的情况,可以在此小节代码基础上举一反三实现。