目录
4 输入输出 I/O
io
库,读写点云数据,从各种传感器获取点云。
4.1 PCD文件格式
鉴于其他格式(ply, obj, stl, x3d等)存在一些问题,PCL主要使用PCD格式。当然也可以使用其他格式。
4.1.1 PCD版本
当前官方版本0.7 (PCD_V7)
4.1.2 文件头header
文件头 (ASCII格式)阐述了点云数据的属性。对于PCD_V7,主要字段如下:
- VERSION : PCD版本 (e.g., .7)
- FIELDS : 每个特征维度名称 (FIELDS x y z rgb)
- SIZE : 每个特征维度的大小 (bytes)
unsigned char/ char : 1 bytes
unsigned short/ short : 2 bytes
unsigned int/ int / float : 4 bytes
double : 8 bytes
- TYPE :: 每个特征维度类型( as a char, I, U, F)
- COUNT : 每个特征维度包含数字个数 (x是1个数,VHF有308个数)
- WIDTH: 宽 (organized), 点数 (unorganized)
- HEIGHT : 高 (organized), 1 (unorganized)
- VIEWPOINT : 视点, translation (tx ty tz) + quaternion (qw, qx, qy, qz)
VIEWPOINT 0 0 0 1 0 0 0
POINTS : 点数
DATA : 数据存储类型 (ascii or binary)
note: 以上顺序不能变。VERSION和DATA是对文件的描述,FIELDS,SIZE,TYPE,COUNT是对点的特征的描述,WIDTH,HEIGHT,VIEWPOINT,POINTS是对点云整体描述
4.1.3 数据存储类型
ASCII:每个点是一行
binary:数据是拷贝自pcl::PointCloud.points array/vector。在linux上,采用mmap/munmap实现快速读写。
4.1.4 PCD文件的优势
- 可以存储和处理有组织点云,对实时的应用很重要(AR,机器人)
- binary mmap/ munmap数据类型是最快的读写方式
- 保存不同类型,灵活高效。无效数据NAN
- 可以保存n-D特征描述子
- 通过控制该文件格式,更适应PCL,最高性能。其他格式的读取需要额外类型转换。
示例:
# .PCD v.7 - Point Cloud Data file format
VERSION .7
FIELDS x y z rgb
SIZE 4 4 4 4
TYPE F F F F
COUNT 1 1 1 1
WIDTH 213
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 213
DATA ascii
0.93773 0.33763 0 4.2108e+06
0.90805 0.35641 0 4.2108e+06
0.81915 0.32 0 4.2108e+06
4.2 从PCD文件中读取点云
头文件:pcl/io/pcd_io.h
函数:pcl::io::loadPCDFile<PointXYZ>(filename, *cloud)
#include <iostream>
// 头文件 pcl/io/pcd_io.h
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
int
main (int argc, char** argv)
{
// 创建PointCloud<pcl::PointXYZ> Boost shared pointer并初始化
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
// 读取点云 pcl::io::loadPCDFile<pcl::PointXYZ>
if (pcl::io::loadPCDFile<pcl::PointXYZ> ("test_pcd.pcd", *cloud) == -1) //* load the file
{
PCL_ERROR ("Couldn't read file test_pcd.pcd \n");
return (-1);
}
std::cout << "Loaded "
<< cloud->width * cloud->height
<< " data points from test_pcd.pcd with the following fields: "
<< std::endl;
for (const