点云 3D 数据 读取、保存 - pypcd 包

1. 点云 3D 数据

点云 3D 数据 离线状况 通常以 “.pcd”、“.bin” 等格式存储,其中 “.pcd” 文件在 Ubuntu 下可以方便的使用 pcl_viewer 命令查看;而 “.bin” 格式 则可 以最少的 库依赖 完成对点云的读取保存。

2. Python 下 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 的情况,可以在此小节代码基础上举一反三实现。

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

77wpa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值