点云分割适合处理由多个独立空间区域组成的点云,将点云分割为不同的簇,然后可以对其进行独立处理。图中说明了平面模型分割和圆柱模型分割的结果。
基于随机采样一致性的分割
1.从一个样本集S中,随机抽取n个样本,拟合出一个模型,n是能够初始化模型的最小样本数。
2.用1中得到的模型去测试所有的其它数据,如果某个点与模型的误差小于某个阈值,则该点适用于这个模型,认为它也是局内点。
3.如果模型内的局内点达到一定个数,那么估计的模型就足够合理。
4.用所有假设的局内点去重新执行1,2,估计模型,因为它仅仅被初始的假设局内点估计过。
5.最后,通过估计局内点与模型的错误率来评估模型。
这个过程被重复执行固定的次数,每次产生的模型要么因为局内点太少而被舍弃,要么因为比现有的模型更好而被选用。
#include
#include
#include
#include
#include //随机参数估计方法头文件
#include //模型定义头文件
#include //基于采样一致性分割的类的头文件
int main(int argc, char** argv)
{
pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
// 填充点云数据,宽度为15,高度为1,无序点云
cloud->width = 15;
cloud->height = 1;
cloud->points.resize(cloud->width * cloud->height);
// 生成数据,随机填充点云的x,y坐标,但是都处于z=1的平面上
for (std::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 = 1.0;
}
// 设置几个局外点,让它们的z值不同
cloud->points[0].z = 2.0;
cloud->points[3].z = -2.0;
cloud->points[6].z = 4.0;
std::cerr << "Point cloud data: " << cloud->points.size() << " points" << std::endl;
for (std::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;
pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);//创建分割时所需的模型系数对象
pcl::PointIndices::Ptr inliers(new pcl::PointIndices);//创建储存内点的点索引集合对象
pcl::SACSegmentation seg;// 创建分割对象
seg.setOptimizeCoefficients(true);// 设置模型系数需要优化
// Mandatory
seg.setModelType(pcl::SACMODEL_PLANE);//分割的模型类型
seg.setMethodType(pcl::SAC_RANSAC);//随机参数的估计方法
seg.setDistanceThreshold(0.01);//阈值距离
seg.setInputCloud(cloud);
seg.segment(*inliers, *coefficients);//输出
if (inliers->indices.size() == 0)
{
PCL_ERROR("Could not estimate a planar model for the given dataset.");
return (-1);
}
//打印出估计的平面模型参数,以ax+by+cz+d=0的形式
std::cerr << "Model coefficients: " << coefficients->values[0] << " "<< coefficients->values[1] << " "<< coefficients->values[2] << " "<< coefficients->values[3] << std::endl;
std::cerr << "Model inliers: " << inliers->indices.size() << std::endl;
for (std::size_t i = 0; i < inliers->indices.size(); ++i)
std::cerr << inliers->indices[i] << " " << cloud->points[inliers->indices[i]].x << " "
<< cloud->points[inliers->indices[i]].y << " "
<< cloud->points[inliers->indices[i]].z << std::endl;
return (0);
}
基于随机采样一致性,从带有噪声的点云中,分割出一个圆柱体模型,处理流程为:
1.过滤掉远于1.5m的数据点
2.估计每个点的表面法线
3.分割出平面模型,储存
4.分割出圆柱体模型
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef pcl::PointXYZ PointT;
using namespace pcl;
int main(int argc, char** argv)
{
// 定义所有需要的对象
pcl::PCDReader reader;//点云文件读取对象
pcl::PassThrough pass;//直通滤波对象
pcl::NormalEstimation ne;//法线估计对象
pcl::SACSegmentationFromNormals seg;//分割对象
pcl::PCDWriter writer;//点云文件写入对象
pcl::ExtractIndices extract;//点提取对象
pcl::ExtractIndices extract_normals;//点提取对象
pcl::search::KdTree::Ptr tree(new pcl::search::KdTree());
// Datasets
pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
pcl::PointCloud::Ptr cloud_filtered(new pcl::PointCloud);
pcl::PointCloud::Ptr cloud_normals(new pcl::PointCloud);
pcl::PointCloud::Ptr cloud_filtered2(new pcl::PointCloud);
pcl::PointCloud::Ptr cloud_normals2(new pcl::PointCloud);
pcl::ModelCoefficients::Ptr coefficients_plane(new pcl::ModelCoefficients), coefficients_cylinder(new pcl::ModelCoefficients);
pcl::PointIndices::Ptr inliers_plane(new pcl::PointIndices), inliers_cylinder(new pcl::PointIndices);
// Read in the cloud data
reader.read("table_scene_mug_stereo_textured.pcd", *cloud);
std::cerr << "PointCloud has: " << cloud->points.size() << " data points." << std::endl;
// 过滤点
pass.setInputCloud(cloud);
pass.setFilterFieldName("z");//使用z字段
pass.setFilterLimits(0, 1.5);//z不在0-1.5的点过滤
pass.filter(*cloud_filtered);//剩余点储存
std::cerr << "PointCloud after filtering has: " << cloud_filtered->points.size() << " data points." << std::endl;
// 计算点法线
ne.setSearchMethod(tree);
ne.setInputCloud(cloud_filtered);
ne.setKSearch(50);
ne.compute(*cloud_normals);
// 进行平面分割的参数设置
seg.setOptimizeCoefficients(true);
seg.setModelType(pcl::SACMODEL_NORMAL_PLANE);
seg.setNormalDistanceWeight(0.1);
seg.setMethodType(pcl::SAC_RANSAC);
seg.setMaxIterations(100);
seg.setDistanceThreshold(0.03);
seg.setInputCloud(cloud_filtered);
seg.setInputNormals(cloud_normals);
// 获得平面轮廓索引和平面方程的系数
seg.segment(*inliers_plane, *coefficients_plane);
std::cerr << "Plane coefficients: " << *coefficients_plane << std::endl;
// 把平面从输入点云中提取出来
extract.setInputCloud(cloud_filtered);
extract.setIndices(inliers_plane);//把输出的平面索引,作为提取平面的依据
extract.setNegative(false);//提取内点
// Write the planar inliers to disk
pcl::PointCloud::Ptr cloud_plane(new pcl::PointCloud());
extract.filter(*cloud_plane);//通过索引把平面点云提取出来,保存到cloud_plane
std::cerr << "PointCloud representing the planar component: " << cloud_plane->points.size() << " data points." << std::endl;
writer.write("table_scene_mug_stereo_textured_plane.pcd", *cloud_plane, false);
// 把平面点云移除,提取剩下的点云
extract.setNegative(true);
extract.filter(*cloud_filtered2);//剩下的点云
extract_normals.setNegative(true);
extract_normals.setInputCloud(cloud_normals);//法线点云数据
extract_normals.setIndices(inliers_plane);
extract_normals.filter(*cloud_normals2);//提取剩余的法线点云
// 创建圆柱体分割对象
seg.setOptimizeCoefficients(true);//进行优化
seg.setModelType(pcl::SACMODEL_CYLINDER);//分割模型为圆柱体
seg.setMethodType(pcl::SAC_RANSAC);//基于随机采样一致性
seg.setNormalDistanceWeight(0.1);//表面法线权重系数
seg.setMaxIterations(10000);//最大迭代次数
seg.setDistanceThreshold(0.05);//内点到模型的允许阈值
seg.setRadiusLimits(0, 0.1);//估计出的圆柱模型的半径范围
seg.setInputCloud(cloud_filtered2);
seg.setInputNormals(cloud_normals2);
// Obtain the cylinder inliers and coefficients
seg.segment(*inliers_cylinder, *coefficients_cylinder);
std::cerr << "Cylinder coefficients: " << *coefficients_cylinder << std::endl;
// Write the cylinder inliers to disk
extract.setInputCloud(cloud_filtered2);
extract.setIndices(inliers_cylinder);
extract.setNegative(false);
pcl::PointCloud::Ptr cloud_cylinder(new pcl::PointCloud());
extract.filter(*cloud_cylinder);
if (cloud_cylinder->points.empty())
std::cerr << "Can't find the cylindrical component." << std::endl;
else
{
std::cerr << "PointCloud representing the cylindrical component: " << cloud_cylinder->points.size() << " data points." << std::endl;
writer.write("table_scene_mug_stereo_textured_cylinder.pcd", *cloud_cylinder, false);
}
return (0);
}
欧式聚类分割的算法步骤:
1.点云中的某点p10,通过kdTree找到离他最近的n个点,判断这n个点到p的距离。将距离小于阈值r的点p12,p13,p14…放在类Q里
2.在 Q(p10) 里找到一点p12,重复1
3.在 Q(p10,p12) 找到一点,重复1,找到p22,p23,p24…全部放进Q里
4.当 Q 再也不能有新点加入了,则完成搜索了
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char** argv)
{
// Read in the cloud data
pcl::PCDReader reader;
pcl::PointCloud::Ptr cloud(new pcl::PointCloud), cloud_f(new pcl::PointCloud);
reader.read("table_scene_lms400.pcd", *cloud);
std::cout << "PointCloud before filtering has: " << cloud->points.size() << " data points." << std::endl; //*
// 使用1cm体素进行下采样
pcl::VoxelGrid vg;
pcl::PointCloud::Ptr cloud_filtered(new pcl::PointCloud);
vg.setInputCloud(cloud);
vg.setLeafSize(0.01f, 0.01f, 0.01f);
vg.filter(*cloud_filtered);
std::cout << "PointCloud after filtering has: " << cloud_filtered->points.size() << " data points." << std::endl; //*
// 进行平面分割
pcl::SACSegmentation seg;
pcl::PointIndices::Ptr inliers(new pcl::PointIndices);//平面点云索引信息
pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);//平面方程的系数
pcl::PointCloud::Ptr cloud_plane(new pcl::PointCloud());//平面点云
pcl::PCDWriter writer;
seg.setOptimizeCoefficients(true);
seg.setModelType(pcl::SACMODEL_PLANE);
seg.setMethodType(pcl::SAC_RANSAC);
seg.setMaxIterations(100);
seg.setDistanceThreshold(0.02);
int i = 0, nr_points = (int)cloud_filtered->points.size();
while (cloud_filtered->points.size() > 0.3 * nr_points)
{
// Segment the largest planar component from the remaining cloud
seg.setInputCloud(cloud_filtered);
seg.segment(*inliers, *coefficients);
if (inliers->indices.size() == 0)
{
std::cout << "Could not estimate a planar model for the given dataset." << std::endl;
break;
}
// 通过索引提取平面点云
pcl::ExtractIndices extract;
extract.setInputCloud(cloud_filtered);
extract.setIndices(inliers);
extract.setNegative(false);
// Get the points associated with the planar surface
extract.filter(*cloud_plane);
std::cout << "PointCloud representing the planar component: " << cloud_plane->points.size() << " data points." << std::endl;
// 移去平面局内点,提取剩余点云
extract.setNegative(true);
extract.filter(*cloud_f);
*cloud_filtered = *cloud_f;
}
// 创建kd数对象
pcl::search::KdTree::Ptr tree(new pcl::search::KdTree);
tree->setInputCloud(cloud_filtered);
std::vector cluster_indices;//cluster_indices是一个向量,保存每个检测到的聚类,cluster_indices[0]即为第一个聚类包含的点集的所有索引
pcl::EuclideanClusterExtraction ec;//创建欧式聚类对象
ec.setClusterTolerance(0.02); // 近邻搜索的搜索半径为2cm
ec.setMinClusterSize(100);//一个聚类需要的最少点数目为100
ec.setMaxClusterSize(25000);//一个聚类需要的最大点数目为25000
ec.setSearchMethod(tree);
ec.setInputCloud(cloud_filtered);
ec.extract(cluster_indices);
int j = 0;
for (std::vector::const_iterator it = cluster_indices.begin(); it != cluster_indices.end(); ++it)//循环访问每个索引,把每个聚类的点分别写入到点云中
{
pcl::PointCloud::Ptr cloud_cluster(new pcl::PointCloud);
for (std::vector::const_iterator pit = it->indices.begin(); pit != it->indices.end(); ++pit)//it代表聚类索引,pit代表聚类里每个点的索引
cloud_cluster->points.push_back(cloud_filtered->points[*pit]); //*
cloud_cluster->width = cloud_cluster->points.size();
cloud_cluster->height = 1;
cloud_cluster->is_dense = true;
std::cout << "PointCloud representing the Cluster: " << cloud_cluster->points.size() << " data points." << std::endl;
std::stringstream ss;
ss << "cloud_cluster_" << j << ".pcd";
writer.write(ss.str(), *cloud_cluster, false); //*
j++;
}
return (0);
}
算法:
1.按点的曲率值进行排序, 从曲率最小的点开始生长(称为初始种子点)。曲率最小的点位于平坦区域中,一般点云中平面区域较大,从最平坦的区域开始增长可以减少分割区域的总数;
2.创建一个空的种子点序列和空的聚类数组,将初始种子点添加到种子点序列中。对于初始种子点,寻找其邻域点;
3.计算邻域点的法线和当前种子点的法线之间的角度。 如果角度小于阈值,则将当前点添加到当前区域;
4.计算邻域点的曲率值。 如果曲率小于阈值,则将此点添加到种子序列中;
5.在进行种子点邻域判断时,从种子序列中除去当前种子,继续1-4步骤;
6.如果种子序列变空,则意味着算法已完成了一个区域的生长;
7.从头开始重复该过程,选择曲率更大的值作为初始种子点。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char** argv)
{
pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
if (pcl::io::loadPCDFile ("cow.pcd", *cloud) == -1)
{
std::cout << "Cloud reading failed." << std::endl;
return (-1);
}
pcl::search::Search::Ptr tree(new pcl::search::KdTree);
pcl::PointCloud ::Ptr normals(new pcl::PointCloud );
pcl::NormalEstimation normal_estimator;//创建法线估计对象
normal_estimator.setSearchMethod(tree);//设置搜索方法
normal_estimator.setInputCloud(cloud);
normal_estimator.setKSearch(50);//50个邻近点
normal_estimator.compute(*normals);//计算并输出法向量
pcl::IndicesPtr indices(new std::vector );
pcl::PassThrough pass;
pass.setInputCloud(cloud);
pass.setFilterFieldName("z");
pass.setFilterLimits(0.0, 1.0);
pass.filter(*indices);
pcl::RegionGrowing reg;//对:RegionGrowing类进行实例化,该模板有两个参数,一个点云的类型,一个法线的类型
reg.setMinClusterSize(50);//一个聚类的最少点数
reg.setMaxClusterSize(1000000);//一个聚类的最大点数
reg.setSearchMethod(tree);
reg.setNumberOfNeighbours(30);//搜索邻近点的数目
reg.setInputCloud(cloud);
//reg.setIndices (indices);
reg.setInputNormals(normals);
reg.setSmoothnessThreshold(3.0 / 180.0 * M_PI);//两个法线的角度偏差阈值
reg.setCurvatureThreshold(1.0);//两点的曲率偏差阈值
std::vector clusters;
reg.extract(clusters);
std::cout << "Number of clusters is equal to " << clusters.size() << std::endl;
std::cout << "First cluster has " << clusters[0].indices.size() << " points." << std::endl;
std::cout << "These are the indices of the points of the initial" <<
std::endl << "cloud that belong to the first cluster:" << std::endl;
int counter = 0;
while (counter < clusters[0].indices.size())
{
std::cout << clusters[0].indices[counter] << ", ";
counter++;
if (counter % 10 == 0)
std::cout << std::endl;
}
std::cout << std::endl;
pcl::PointCloud ::Ptr colored_cloud = reg.getColoredCloud();
pcl::visualization::CloudViewer viewer("Cluster viewer");
viewer.showCloud(colored_cloud);
while (!viewer.wasStopped())
{
}
return (0);
}
主要区别:
1.它使用颜色而不是法线;
2.它将合并算法用于过度分割和欠分割控制。分割之后,将平均颜色差异很小的两个相邻聚类合并在一起。计算每个群集包含的点数, 如果此数字小于用户定义的值,则当前群集将与最近的相邻群集合并。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std::chrono_literals;
int main(int argc, char** argv)
{
pcl::search::Search ::Ptr tree(new pcl::search::KdTree);//创建一个指向kd树搜索对象的共享指针
pcl::PointCloud ::Ptr cloud(new pcl::PointCloud );
if (pcl::io::loadPCDFile ("region_growing_rgb_tutorial.pcd", *cloud) == -1)
{
std::cout << "Cloud reading failed." << std::endl;
return (-1);
}
//进行直通滤波处理,提取滤波后的点集,保存到索引中
pcl::IndicesPtr indices(new std::vector );//创建一组索引
pcl::PassThrough pass;//设置直通滤波器对象
pass.setInputCloud(cloud);
pass.setFilterFieldName("z");
pass.setFilterLimits(0.0, 1.0);
pass.filter(*indices);
pcl::RegionGrowingRGB reg;
reg.setInputCloud(cloud);
reg.setIndices(indices);//设置输入点云的索引
reg.setSearchMethod(tree);
reg.setDistanceThreshold(10);//设置距离阈值,判断点云两点是否相邻
reg.setPointColorThreshold(6);//两点的颜色差别阈值
reg.setRegionColorThreshold(5);//两个聚类的平均颜色的差别阈值
reg.setMinClusterSize(600);
std::vector clusters;
reg.extract(clusters);
pcl::PointCloud ::Ptr colored_cloud = reg.getColoredCloud();
pcl::visualization::CloudViewer viewer("Cluster viewer");
viewer.showCloud(colored_cloud);
while (!viewer.wasStopped())
{
std::this_thread::sleep_for(100us);
}
return (0);
}
算法的解释见: PCL—点云分割(最小图分割).
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char** argv)
{
pcl::PointCloud ::Ptr cloud(new pcl::PointCloud );
if (pcl::io::loadPCDFile ("min_cut_segmentation_tutorial.pcd", *cloud) == -1)
{
std::cout << "Cloud reading failed." << std::endl;
return (-1);
}
//对点云进行滤波处理,提取滤波后的点集,保存到索引中
pcl::IndicesPtr indices(new std::vector );//索引
pcl::PassThrough pass;//直通滤波器对象
pass.setInputCloud(cloud);
pass.setFilterFieldName("z");//维度
pass.setFilterLimits(0.0, 1.0);//范围
pass.filter(*indices);//结果保存到索引中
pcl::MinCutSegmentation seg;//创建最小割对象
seg.setInputCloud(cloud);//输入点云
seg.setIndices(indices);//点云索引
pcl::PointCloud::Ptr foreground_points(new pcl::PointCloud());
pcl::PointXYZ point;
point.x = 68.97;
point.y = -18.55;
point.z = 0.57;
foreground_points->points.push_back(point);
seg.setForegroundPoints(foreground_points);//输入前景点云的中心点,该中心点为用户预先假定的目标物的中心点坐标
seg.setSigma(0.25);//平滑成本的sigma值
seg.setRadius(3.0433856);//可以粗略认为是前景点云的水平半径,小于该半径的点被认为是前景点云
seg.setNumberOfNeighbours(14);//近邻点的数目
seg.setSourceWeight(0.8);//前景惩罚的权重
std::vector clusters;
seg.extract(clusters);//提取分割结果保存到点云索引的向量中
std::cout << "Maximum flow is " << seg.getMaxFlow() << std::endl;
pcl::PointCloud ::Ptr colored_cloud = seg.getColoredCloud();
pcl::visualization::CloudViewer viewer("Cluster viewer");
viewer.showCloud(colored_cloud);
while (!viewer.wasStopped())
{
}
return (0);
}