今天开始着手处理PLY数据,由于之前没有接触过PCL,所以连最简单的数据读取与显示都搞了半天,现在将代码公布出来以供参考。
使用的环境是:vs2015+pcl1.8.1
#include "stdafx.h"
#include <iostream>
#include <string>
#include <pcl/point_types.h>
#include <pcl/io/ply_io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/PCLPointCloud2.h>
#include <pcl/visualization/cloud_viewer.h>
using namespace pcl;
using namespace pcl::io;
using namespace std;
int main() {
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPLYFile<pcl::PointXYZ>("pointCloud.ply", *cloud) == -1) {
PCL_ERROR("Couldnot read file.\n");
system("pause");
return(-1);
}
pcl::visualization::CloudViewer viewer("Cloud Viewer");
viewer.showCloud(cloud);
system("pause");
return(0);
}
值得注意的几点是:
1)cloud智能指针对象在定义的时候记得要初始化;一开始我没有初始化,结果程序报错:
Assertion failed: px!=0 file ..\boost\smart_ptr\shared_ptr.hpp, line 704.
2)pointCloud的格式有很多种,如PointXYZ, PointXYZRGB,...。这里我这使用的是PointXYZ,是因为使用的数据pointcloud.ply中只包含每个点的三维坐标,这里的格式需要根据自己的数据来确定。
pointcloud.ply数据格式如下:
ply
format ascii 1.0
element vertex 43200
property float x
property float y
property float z
end_header
-125 -95 232
-241 -109 456
-247 -169 462
......
......
3)数据显示的时候,一开始的界面是上面一条绿色,下面左边是黑色,右边是红色,我还以为数据出问题了,用鼠标缩放才发现这代表的是坐标轴。通过鼠标左右键与滑轮可以调节数据的缩放、旋转角度等。
来源:CSDN
作者:wowodadai
链接:https://blog.csdn.net/ifenghua135792468/article/details/82898299