版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32867925/article/details/82807455
PCL中所列有两个RANSAC与LMedS两种采样方法,原理在这里不做详细介绍了。但是涉及到如何将分割的对象单独保存出来,以及使用不同颜色进行显示,官网上介绍比较少。现给出一个案例,进行讲解,以RANSAC探测平面为例,其他的以及LMedS不再做详细介绍。
//RANSAC探测多个平面
#include<pcl\point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/vtk_io.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/sample_consensus/method_types.h> //随机参数估计方法头文件
#include <pcl/sample_consensus/model_types.h> //模型定义头文件
#include <pcl/segmentation/sac_segmentation.h> //基于采样一致性分割的类的头文件
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/filters/extract_indices.h>
#include<stdlib.h>
#include<time.h>
#include<sstream>
using namespace std;
int GetRandomNumber()
{
int RandomNumber;
RandomNumber = rand() % (256) + 0;//0到255之间选择颜色
//生成其他范围的数字:RandomNumber = rand() % (b-a+1) + a;
return RandomNumber;
}
void main()
{
//第一步:定义输入的原始数据及滤波后的点,以及分割获得的点、平面系数coefficients、存储内点的索引集合对象inliers、用于显示的窗口
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr planar_segment(new pcl::PointCloud<pcl::PointXYZ>);//创建分割对象
pcl::io::loadPCDFile("E:\\program_study\\C++\\pcd_data\\table_scene_lms400.pcd", *cloud);
//pcl::io::loadPCDFile("E:\\program_study\\C++\\pcd_data\\cubic.pcd", *cloud);
pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);//模型系数
pcl::PointIndices::Ptr inliers(new pcl::PointIndices);//索引列表
pcl::SACSegmentation<pcl::PointXYZ> seg;//分割对象
pcl::visualization::PCLVisualizer viewer("planar segment");
viewer.setBackgroundColor(0, 0, 0);
pcl::ExtractIndices<pcl::PointXYZ> extract;//提取器
int n_piece=2;//需要探测的面的个数
//第二步:将原始点加载进入
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 255, 0, 0);//第二个平面颜色设置成蓝色 single_color_02
viewer.addPointCloud<pcl::PointXYZ>(cloud, single_color, "sample");//将single_color在viewer中进行显示
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample");
//第三步:使用RANSAC获取点数最多的面
for (int i = 0; i < n_piece; i++)
{
seg.setOptimizeCoefficients(true); //使用内部点重新估算模型参数
seg.setModelType(pcl::SACMODEL_PLANE); //设置模型类型
seg.setMethodType(pcl::SAC_RANSAC); //设置随机采样一致性方法类型
seg.setDistanceThreshold(0.01); //设定距离阀值,距离阀值决定了点被认为是局内点是必须满足的条件
seg.setInputCloud(cloud);
seg.segment(*inliers, *coefficients);
extract.setInputCloud(cloud);
extract.setIndices(inliers);
extract.setNegative(false);
//提取探测出来的平面
extract.filter(*planar_segment);
//planar_segment为该次探测出来的面片,可以单独进行保存,此处省略
//剔除探测出的平面,在剩余点中继续探测平面
extract.setNegative(true);
extract.filter(*cloud);
int R = GetRandomNumber();
int G = GetRandomNumber();
int B = GetRandomNumber();
stringstream ss;
ss << i + 1;
string str;
ss >> str;
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(planar_segment, R, G, B);//第二个平面颜色设置成蓝色 single_color_02
viewer.addPointCloud<pcl::PointXYZ>(planar_segment, single_color, str);//将single_color在viewer中进行显示
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3,str);
}
while (!viewer.wasStopped())
{
viewer.spinOnce(1);
}
}
配上提取效果:
提取两个平面的效果图:
提取三个平面效果图
附上代码下载链接: