在平面上提取凸(凹)多边形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include <iostream>
#include <pcl/ModelCoefficients.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/filters/passthrough.h>
#include <pcl/filters/project_inliers.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/surface/concave_hull.h>


int main(int argc,char** argv) {
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>),
cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>),
cloud_projected(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PCDReader reader;

reader.read("table_scene_mug_stereo_textured.pcd",*cloud);
//建立过滤器消除杂散的NaN
pcl::PassThrough<pcl::PointXYZ> pass;
pass.setInputCloud(cloud);//输入点云
pass.setFilterFieldName("z");//过滤字段为z坐标
pass.setFilterLimits(0,1.1);//设置分割阈值为(0,1.1)
pass.filter(*cloud_filtered);//输出点云
//输出处理过后的点云数据的点的数量
std::cerr<<"PointCloud after filtering has: "
<<cloud_filtered->points.size()<<std::endl;

pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
//创建分割对象
pcl::SACSegmentation<pcl::PointXYZ> seg;
//设置优化系数为true
seg.setOptimizeCoefficients(true);
seg.setModelType(pcl::SACMODEL_PLANE);
seg.setModelType(pcl::SAC_RANSAC);
seg.setDistanceThreshold(0.01);
seg.setInputCloud(cloud_filtered);
seg.segment(*inliers,*coefficients);
std::cerr<<"PointCloud after segmentation has: "<<inliers->indices.size()
<<" inliers."<<std::endl;

pcl::ProjectInliers<pcl::PointXYZ> proj;//点云投影滤波模型
proj.setModelType(pcl::SACMODEL_PLANE);//设置投影模型
proj.setIndices(inliers);
proj.setInputCloud(cloud_filtered);
proj.setModelCoefficients(coefficients);//将估计得到的平面coefficients参数设置为投影平面模型系数
proj.filter(*cloud_projected);//输出点云
std::cerr<<"ProCloud after projection has: "
<<cloud_projected->points.size()<<" data points."<<std::endl;
//存储提取多边形上的点云
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_hull(new pcl::PointCloud<pcl::PointXYZ>);
pcl::ConcaveHull<pcl::PointXYZ> chull;//创建多边形提取对象
chull.setInputCloud(cloud_projected);//设置输入点云为提取后的点云数据
chull.setAlpha(0.1);
chull.reconstruct(*cloud_hull);//创建提取创建凹多边形

std::cerr<<"Concave hull has:"<<cloud_hull->points.size()
<<" data points."<<std::endl;
pcl::PCDWriter writer;
writer.write("table.pcd",*cloud_hull, false);

return 0;
}

输出结果比较

感谢您的阅读。 🙏 关于转载请看这里