您的位置:首页 > 其它

PCL中读取pcd点云数据的两种方法

2017-06-27 15:55 1146 查看
        在读取pcd文件之前,首先要确定pcd文件内三维数据存储的点的类型。点的类型有很多PiontXY,PiontXYZ,PiontXYZRGBA,PiontXYZI,比如下面以PiontXYZRGB格式为例。

        PCD文件由文件头和数据域组成,文件头存储了点云数据的字段格式信息,如:

 # .PCD v0.7 - Point Cloud Data file format

 VERSION 0.7
 FIELDS x y z rgb

SIZE 4 4 4 4

TYPE F F F F

COUNT 1 1 1 1

WIDTH 246868

HEIGHT 1

VIEWPOINT 0 0 0 1 0 0 0

POINTS 246868

DATA ascii

         读入数据后,可以遍历点云数据,并访问各个域上的值。 
        显示使用的是PCL的PCLVisualizer。addCoordinateSystem函数可以加入摄像头的坐标系统(其中红绿蓝分别代表x、y、z轴,对应了正右、正下、正前方)。setCameraPosition函数设定窗口的视角,前三个是视点坐标(x,y,z),设置为(0,0,-3.0)说明当前视点是摄像头的后面3米。后三个是视点的向上方向,(0,-1,0)表明以y轴负方向为正上方。
#include
#include
#include  //PCL的PCD格式文件的输入输出头文件
#include  //PCL对各种格式的点的支持头文件
#include  //可视化模块PCLVisualizer头文件
#include //点云查看窗口头文件
using namespace std;

int main(int argc, char** argv)
{
//定义点的类型 PointXYZRGB
typedef pcl::PointXYZRGB PointT;
pcl::PointCloud::Ptr cloud(new pcl::PointCloud);

if (pcl::io::loadPCDFile("pointcloud.pcd", *cloud) == -1){
//* load the file
PCL_ERROR("Couldn't read PCD file \n");
return (-1);
}
printf("Loaded %d data points from PCD\n",
cloud->width * cloud->height);

pcl::visualization::PCLVisualizer viewer("Cloud viewer");
viewer.setCameraPosition(0, 0, -3.0, 0, -1, 0);
viewer.addCoordinateSystem(0.3);

viewer.addPointCloud(cloud);
while (!viewer.wasStopped())
viewer.spinOnce(100);
return (0);
}
//方法二:
//int main(int argc, char** argv)
//{
//	pcl::PointCloud::Ptr cloud(new pcl::PointCloud); // 创建点云(指针)
//
//	if (pcl::io::loadPCDFile("pointcloud.pcd", *cloud) == -1) //* 读入PCD格式的文件,如果文件不存在,返回-1
//	{
//		PCL_ERROR("Couldn't read file test_pcd.pcd \n"); //文件不存在时,返回错误,终止程序。
//		return (-1);
//	}
//	pcl::visualization::CloudViewer viewer("Simple Cloud Viewer");//直接创造一个显示窗口
//	viewer.showCloud(cloud);//再这个窗口显示点云
//	while (!viewer.wasStopped())
//	{
//	}
//	return (0);
//}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PCL PCD读取显示