1.首先包含的对应的ModelCoefficients.h以及filter中向平面投影的project_inlier.h
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/filters/project_inliers.h>
2.增加可视化显示的代码
int user_data;
void
viewerOneOff(pcl::visualization::PCLVisualizer& viewer)
{
viewer.setBackgroundColor(1.0, 0.5, 1.0);
pcl::PointXYZ o;
o.x = 1.0;
o.y = 0;
o.z = 0;
viewer.addSphere(o, 0.25, "sphere", 0);
std::cout << "i only run once" << std::endl;
}
void
viewerPsycho(pcl::visualization::PCLVisualizer& viewer)
{
static unsigned count = 0;
std::stringstream ss;
ss << "Once per viewer loop: " << count++;
viewer.removeShape("text", 0);
viewer.addText(ss.str(), 200, 300, "text", 0);
//FIXME: possible race condition here:
user_data++;
}
3.创建点云对象指针并初始化,输出到屏幕
/2.初始化该对象
cloud->width = 5;//对于未组织的点云的相当于points个数
cloud->height = 1; //对未组织的点云指定为1
cloud->points.resize (cloud->width * cloud->height); //修剪或追加值初始化的元素
for (size_t i = 0; i < cloud->points.size (); ++i)
{
cloud->points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
cloud->points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
cloud->points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);
}
// 3.cerr 输出对象放置刷屏
std::cerr << "Cloud before projection: " << std::endl;
for (size_t i = 0; i < cloud->points.size (); ++i)
std::cerr << " " << cloud->points[i].x << " "
<< cloud->points[i].y << " "
<< cloud->points[i].z << std::endl;
//投影前点
`Cloud before projection:
1.28125 577.094 197.938
828.125 599.031 491.375
358.688 917.438 842.563
764.5 178.281 879.531
727.531 525.844 311.281
4.设置ModelCoefficients值。在这种情况下,我们使用一个平面模型,其中ax + by + cz + d = 0,其中a = b = d = 0,c = 1,或者换句话说,XY平面
// 4.创建一个系数为X=Y=0,Z=1的平面
pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ());
coefficients->values.resize (4);
coefficients->values[0] = coefficients->values[1] = 0;
coefficients->values[2] = 1.0;
coefficients->values[3] = 0;
5.通过该滤波将所有的点投影到创建的平面上,并输出结果
** 注意这里在使用的时候再创建滤波后对象不规范,应该放在程序开始的时候**
//5.创建滤波后对象,并通过滤波投影,并显示结果
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_projected(new
pcl::PointCloud<pcl::PointXYZ>);
// 创建滤波器对象
pcl::ProjectInliers<pcl::PointXYZ> proj;
proj.setModelType (pcl::SACMODEL_PLANE);
proj.setInputCloud (cloud);
proj.setModelCoefficients (coefficients);
proj.filter (*cloud_projected);
//可视化显示
pcl::visualization::CloudViewer viewer("Cloud Viewer");
//showCloud函数是同步的,在此处等待直到渲染显示为止
viewer.showCloud(cloud);
//该注册函数在可视化时只调用一次
viewer.runOnVisualizationThreadOnce(viewerOneOff);
//该注册函数在渲染输出时每次都调用
viewer.runOnVisualizationThread(viewerPsycho);
while (!viewer.wasStopped())
{
//在此处可以添加其他处理
user_data++;
}
std::cerr << "Cloud after projection: " << std::endl;
for (size_t i = 0; i < cloud_projected->points.size (); ++i)
std::cerr << " " << cloud_projected->points[i].x << " "
<< cloud_projected->points[i].y << " "
<< cloud_projected->points[i].z << std::endl;
return (0);
//投影后点
Cloud before projection:
1.28125 577.094 197.938
828.125 599.031 491.375
358.688 917.438 842.563
764.5 178.281 879.531
727.531 525.844 311.281
Cloud after projection:
1.28125 577.094 0
828.125 599.031 0
358.688 917.438 0
764.5 178.281 0
727.531 525.844 0
6.参考网址
pcl官网例程
all-in_one 中的有api 以及例子,但是具体理论说明还是参考官网吧!
...PCL-1.8.1-AllInOne-msvc2017-win64(1)sharedocpcl-1.8 utorialssources中 例子要比pcl入门精通要全