目录
- 平面分割——planar segmentation(随机采样一致性)
- 圆柱体模型分割——cylinder segmentation(随机采样一致性)
- 聚类提取——cluster extraction
- 区域增长分割——region growing segmentation
- 基于颜色的区域生长——region growing rgb segmentation
平面分割——planar segmentation
点云操作中,平面的分割是经常遇到的问题,下面的例子就是如何利用PCL库提拟合出的参数,之后就可以过滤掉在平面附近的点云。pcl::SACSegmentation<pcl::PointXYZ> seg;
#include <iostream>
#include <pcl/ModelCoefficients.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
int
main (int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZ> cloud;
//填充点云数据
cloud.width = 15;
cloud.height = 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 = 1.0;
}
//设置几个局外点
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 (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<pcl::PointXYZ> seg;
//可选设置
seg.setOptimizeCoefficients (true);
//必须设置
seg.setModelType (pcl::SACMODEL_PLANE);//设置模型类型,检测平面
seg.setMethodType (pcl::SAC_RANSAC); //设置方法【聚类或随机样本一致性】
seg.setDistanceThreshold (0.01);
seg.setInputCloud (cloud.makeShared ());
seg.segment (*inliers, *coefficients);//分割操作
if (inliers->indices.size () == 0)
{
PCL_ERROR ("Could not estimate a planar model for the given dataset.");
return (-1);
}
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 (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);
}
实验结果:
********************************************************************************
pcl中coefficients的理解:
https://www.cnblogs.com/chenbokai/p/7597294.html
pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ());
coefficients->values.resize(4);
coefficients->values[0] = 0;
coefficients->values[1] = 0;
coefficients->values[2] = 1.0;
coefficients->values[3] = 0;
然后利用ProjectInliers类将setModelCoefficients将coefficents穿入,然后cloud中points[i].z变成0,即为去除地面。
values[0] = 1.0 时,points中x为0;
values[1] = 1.0 时,points中y为0;
values[2] = 1.0时,points中z为0;
values[3] = 1.0时,points均为 -nan;
************************************************************************************************************************
https://blog.csdn.net/lch_vison/article/details/80806464
PCL中对象和共享指针的转化pcl::PointIndices::Ptr和pcl::PointIndices、PointCloud::Ptr和PointCloud
二、圆柱体模型分割——cylinder segmentation
https://blog.csdn.net/zfjBIT/article/details/95054109
类 SACSegmentationFromNormals< PointT, PointNT >是利用采样一致性方法进行点云分割的类,与其父类 SACSegmentation 不同之处在于其在算法实现时采用了法线信息,即该类在进行运算输出之前需要设定法线信息 。
程序流程:
1、直通滤波器,过滤掉远于1.5米的数据点;
2、估计每个点的表面法线;
3、分割出平面模型(演示数据集中表示桌面)并保存到磁盘中;
4、分割圆出柱体模型(演示数据集中表示圆杯)并保存到磁盘中;
5、可视化分割结果。
#include <pcl/ModelCoefficients.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/passthrough.h>
#include <pcl/features/normal_3d.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>
typedef pcl::PointXYZ PointT;
int
main (int argc, char** argv)
{
// All the objects needed
pcl::PCDReader reader;
pcl::PassThrough<PointT> pass;
pcl::NormalEstimation<PointT, pcl::Normal> ne;
pcl::SACSegmentationFromNormals<PointT, pcl::Normal> seg;
pcl::PCDWriter writer;
pcl::ExtractIndices<PointT> extract;
pcl::ExtractIndices<pcl::Normal> extract_normals;
pcl::search::KdTree<PointT>::Ptr tree (new pcl::search::KdTree<PointT> ());
// Datasets
pcl::PointCloud<PointT>::Ptr cloud (new pcl::PointCloud<PointT>);
pcl::PointCloud<PointT>::Ptr cloud_filtered (new pcl::PointCloud<PointT>);
pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal>);
pcl::PointCloud<PointT>::Ptr cloud_filtered2 (new pcl::PointCloud<PointT>);
pcl::PointCloud<pcl::Normal>::Ptr cloud_normals2 (new pcl::PointCloud<pcl::Normal>);
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;
// Build a passthrough filter to remove spurious NaNs
pass.setInputCloud (cloud);
pass.setFilterFieldName ("z");
pass.setFilterLimits (0, 1.5);
pass.filter (*cloud_filtered);
std::cerr << "PointCloud after filtering has: " << cloud_filtered->points.size () << " data points." << std::endl;
// Estimate point normals
ne.setSearchMethod (tree);
ne.setInputCloud (cloud_filtered);
ne.setKSearch (50);
ne.compute (*cloud_normals);
// Create the segmentation object for the planar model and set all the parameters
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);
// Obtain the plane inliers and coefficients
seg.segment (*inliers_plane, *coefficients_plane);
std::cerr << "Plane coefficients: " << *coefficients_plane << std::endl;
// Extract the planar inliers from the input cloud
extract.setInputCloud (cloud_filtered);
extract.setIndices (inliers_plane);
extract.setNegative (false);
// Write the planar inliers to disk
pcl::PointCloud<PointT>::Ptr cloud_plane (new pcl::PointCloud<PointT> ());
extract.filter (*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);
// Remove the planar inliers, extract the rest
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);
// Create the segmentation object for cylinder segmentation and set all the parameters
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<PointT>::Ptr cloud_cylinder (new pcl::PointCloud<PointT> ());
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);
}
// 可视化部分
pcl::visualization::PCLVisualizer v0("segmention"); // 我们将要使用的颜色
float bckgr_gray_level = 0.0; // 黑色
float txt_gray_lvl = 1.0 - bckgr_gray_level; // 设置初始点云为白色
pcl::visualization::PointCloudColorHandlerCustom<PointT> cloud_in_color_h(cloud, (int)255 * txt_gray_lvl, (int)255 * txt_gray_lvl,(int)255 * txt_gray_lvl);//赋予显示点云的颜色
v0.addPointCloud(cloud, cloud_in_color_h, "cloud"); // 可视化部分
pcl::visualization::PCLVisualizer viewer("segmention"); // 设置cloud_plane点云为红色
pcl::visualization::PointCloudColorHandlerCustom<PointT> cloud_tr_color_h(cloud_plane, 0, 0, 255);
viewer.addPointCloud(cloud_plane, cloud_tr_color_h, "cloud_plane"); // 设置cloud_cylinder点云为绿色
pcl::visualization::PointCloudColorHandlerCustom<PointT> cloud_icp_color_h(cloud_cylinder, 0, 255, 0);
viewer.addPointCloud(cloud_cylinder, cloud_icp_color_h, "cloud_cylinder");
启动可视化
//v0.addCoordinateSystem(0.0);
//v0.initCameraParameters();
//viewer.addCoordinateSystem(0.0);
//viewer.initCameraParameters();
//等待直到可视化窗口关闭。
while (!viewer.wasStopped())
{
v0.spinOnce(100);
viewer.spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
return (0);
}
实验结果:
原始点云(包括平面桌子,杯子,其他物体)
提取的点云:蓝色为平面 桌子,绿色为圆柱 杯子。
三、聚类提取——cluster extraction
pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
参考:https://blog.csdn.net/JAT0929/article/details/104258461
https://blog.csdn.net/AdamShan/article/details/83015570
基于欧式距离的分割和基于区域生长的分割本质上都是用区分邻里关系远近来完成的。由于点云数据提供了更高维度的数据,故有很多信息可以提取获得。欧几里得算法使用邻居之间距离作为判定标准,而区域生长算法则利用了法线,曲率,颜色等信息来判断点云是否应该聚成一类。
下面是欧式距离分割的具体算法伪代码:
- 在空间找一点p1,用kdTree找到离他最近的n个点,判断这n个点到p的距离,如果距离小于某个阈值r,则点p1,p2,p3,放入簇Q中
- 然后在Q里p2点处找n个点,判断距离,重复1
- 在 Q\p1,p2 找到一点,重复1,找到p12,p13,p14….全部放进Q里
- 当 Q 再也不能有新点加入了,则完成搜索了
本代码流程:
(1)点云读入;
(2)体素化下采样(方便处理);
(3)去离散点;
(4)RANSAC算法检测平面,并剔除平面点云;
(5)欧式聚类;
(6)结果的输出和可视化;
#include <pcl/ModelCoefficients.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/features/normal_3d.h>
#include <pcl/kdtree/kdtree.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/visualization/pcl_visualizer.h>
int
main (int argc, char** argv)
{
// Read in the cloud data
pcl::PCDReader reader;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>), cloud_f (new pcl::PointCloud<pcl::PointXYZ>);
reader.read ("table_scene_lms400.pcd", *cloud);
std::cout << "PointCloud before filtering has: " << cloud->points.size () << " data points." << std::endl; //*
// Create the filtering object: downsample the dataset using a leaf size of 1cm
pcl::VoxelGrid<pcl::PointXYZ> vg;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
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; //*
// Create the segmentation object for the planar model and set all the parameters
pcl::SACSegmentation<pcl::PointXYZ> seg;
pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_plane (new pcl::PointCloud<pcl::PointXYZ> ());
pcl::PCDWriter writer;
seg.setOptimizeCoefficients (true);
seg.setModelType (pcl::SACMODEL_PLANE);
seg.setMethodType (pcl::SAC_RANSAC);
seg.setMaxIterations (100);
seg.setDistanceThreshold (0.02); //设置判断是否为模型内点的距离阈值
pcl::visualization::PCLVisualizer viewer("segmention");
viewer.setBackgroundColor(1.0, 1.0, 1.0);
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;
}
// Extract the planar inliers from the input cloud
pcl::ExtractIndices<pcl::PointXYZ> extract;
extract.setInputCloud (cloud_filtered);
extract.setIndices (inliers);
extract.setNegative (false);
// Write the planar inliers to disk
extract.filter (*cloud_plane);
std::cout << "PointCloud representing the planar component: " << cloud_plane->points.size () << " data points." << std::endl;
std::stringstream s1;
s1 << "cloud_plane" << i;
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_tr_color_h(cloud_plane, 0, 0, 255);
viewer.addPointCloud(cloud_plane, cloud_tr_color_h, s1.str()); // 设置cloud_cylinder点云为绿色
// Remove the planar inliers, extract the rest
extract.setNegative (true);
extract.filter (*cloud_f);
cloud_filtered = cloud_f;
i++;
}
// Creating the KdTree object for the search method of the extraction
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>);
tree->setInputCloud (cloud_filtered);
std::vector<pcl::PointIndices> cluster_indices;
pcl::EuclideanClusterExtraction<pcl::PointXYZ> 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);//从点云中提取聚类,并将点云索引保存在cluster_indices中
/*为了从点云索引向量中分割出每个聚类,必须迭代访问点云索引,每次创建一个新的点云数据集,并且将所有当前聚类的点写入到点云数据集中。*/
//迭代访问点云索引cluster_indices,直到分割出所有聚类
int j = 0;
for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZ>);
for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); 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::stringstream s2;
s2 << "cloud_cluster" << j;
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_tr_color_h(cloud_cluster, 20+(j*50), 10+ (j * 50), 0 );
viewer.addPointCloud(cloud_cluster, cloud_tr_color_h, s2.str());
std::cout << "PointCloud representing the Cluster: " << cloud_cluster->points.size () << " data points." << std::endl;
std::stringstream ss;
ss << "cloud_cluster_" << j << ".pcd";
writer.write<pcl::PointXYZ> (ss.str (), *cloud_cluster, false); //*
j++;
}
while (!viewer.wasStopped())
{
viewer.spinOnce();
}
return (0);
}
实验结果:
(此处的可视化,是将不同的部分,依次用不同的颜色显示到一起)
可以看出分割出两个平面,剩余部分根据聚类,聚类出5部分,用不同颜色标出。
https://blog.csdn.net/qq_40335930/article/details/91982640 后来看到这个道友的可视化。我觉得可以参考。用的随机产生颜色。
四、区域增长分割——region growing segmentation
之前了解到的 基于区域的图像分割,原理是选取一个种子点,然后一点点吧周围相似的点连接到一起组成一个区域。
pcl::RegionGrowing类
参考:https://blog.csdn.net/aishuirenjia/article/details/80239562
算法核心:该算法是基于点法线之间角度的比较,企图将满足平滑约束的相邻点合并在一起,以一簇点集的形式输出。每簇点集被认为是属于相同平面。
工作原理:首先需要明白,区域增长是从有最小曲率值(curvature value)的点开始的。因此,我们必须计算出所有曲率值,并对它们进行排序。这是因为曲率最小的点位于平坦区域,而从最平坦的区域增长可以减少区域的总数。现在我们来具体描述这个过程:
1.点云中有未标记点,按照点的曲率值对点进行排序,找到最小曲率值点,并把它添加到种子点集;
2.对于每个种子点,算法都会发现周边的所有近邻点。
1)计算每个近邻点与当前种子点的法线角度差(reg.setSmoothnessThreshold),如果差值小于设置的阈值,则该近邻点被重点考虑,进行第二步测试;2)该近邻点通过了法线角度差检验,如果它的曲率小于我们设定的阈值(reg.setCurvatureThreshold),这个点就被添加到种子点集,即属于当前平面。
3.通过两次检验的点,被从原始点云去除。
4.设置最小点簇的点数min(reg.setMinClusterSize),最大点簇为max(reg.setMaxClusterSize)。
5.重复1-3步,算法会生成点数在min和max的所有平面,并对不同平面标记不同颜色加以区分。
6.直到算法在剩余点中生成的点簇不能满足min,算法停止工作。
#include <iostream>
#include <vector>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/search/search.h>
#include <pcl/search/kdtree.h>
#include <pcl/features/normal_3d.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/filters/passthrough.h>
#include <pcl/segmentation/region_growing.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>
#include <pcl/console/time.h>
#include <windows.h>
#include <stdio.h>
#include <psapi.h>
void PrintMemoryInfo( )
{
HANDLE hProcess;
PROCESS_MEMORY_COUNTERS pmc;
hProcess=GetCurrentProcess();
printf( "\nProcess ID: %u\n", hProcess );
// Print information about the memory usage of the process.
//输出进程使用的内存信息
if (NULL == hProcess)
return;
if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
{
printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
printf( "\tPeakWorkingSetSize: 0x%08X\n",
pmc.PeakWorkingSetSize );
printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n",
pmc.QuotaPeakPagedPoolUsage );
printf( "\tQuotaPagedPoolUsage: 0x%08X\n",
pmc.QuotaPagedPoolUsage );
printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n",
pmc.QuotaPeakNonPagedPoolUsage );
printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n",
pmc.QuotaNonPagedPoolUsage );
printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
printf( "\tPeakPagefileUsage: 0x%08X\n",
pmc.PeakPagefileUsage );
}
CloseHandle( hProcess );
}
using namespace pcl::console;
int
main (int argc, char** argv)
{
if(argc<2)
{
std::cout<<".exe xx.pcd -kn 50 -bc 0 -fc 10.0 -nc 0 -st 30 -ct 0.05"<<endl;
return 0;
}//如果输入参数小于1个,输出提示
time_t start,end,diff[5],option;
start = time(0);
int KN_normal=50; //设置默认输入参数
bool Bool_Cuting=false;//设置默认输入参数
float far_cuting=10,near_cuting=0,SmoothnessThreshold=30.0,CurvatureThreshold=0.05;//设置默认输入参数
parse_argument (argc, argv, "-kn", KN_normal);
parse_argument (argc, argv, "-bc", Bool_Cuting);
parse_argument (argc, argv, "-fc", far_cuting);
parse_argument (argc, argv, "-nc", near_cuting);
parse_argument (argc, argv, "-st", SmoothnessThreshold);
parse_argument (argc, argv, "-ct", CurvatureThreshold);//设置输入参数方式
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
if ( pcl::io::loadPCDFile <pcl::PointXYZ> (argv[1], *cloud) == -1)
{
std::cout << "Cloud reading failed." << std::endl;
return (-1);
}// 加载输入点云数据
end = time(0);
diff[0] = difftime (end, start);
PCL_INFO ("\Loading pcd file takes(seconds): %d\n", diff[0]);
//Noraml estimation step(1 parameter)
pcl::search::Search<pcl::PointXYZ>::Ptr tree = boost::shared_ptr<pcl::search::Search<pcl::PointXYZ> > (new pcl::search::KdTree<pcl::PointXYZ>);//创建一个指向kd树搜索对象的共享指针
pcl::PointCloud <pcl::Normal>::Ptr normals (new pcl::PointCloud <pcl::Normal>);
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> normal_estimator;//创建法线估计对象
normal_estimator.setSearchMethod (tree);//设置搜索方法
normal_estimator.setInputCloud (cloud);//设置法线估计对象输入点集
normal_estimator.setKSearch (KN_normal);// 设置用于法向量估计的k近邻数目
normal_estimator.compute (*normals);//计算并输出法向量
end = time(0);
diff[1] = difftime (end, start)-diff[0];
PCL_INFO ("\Estimating normal takes(seconds): %d\n", diff[1]);
//optional step: cutting the part are far from the orignal point in Z direction.2 parameters
pcl::IndicesPtr indices (new std::vector <int>);//创建一组索引
if(Bool_Cuting)//判断是否需要直通滤波
{
pcl::PassThrough<pcl::PointXYZ> pass;//设置直通滤波器对象
pass.setInputCloud (cloud);//设置输入点云
pass.setFilterFieldName ("z");//设置指定过滤的维度
pass.setFilterLimits (near_cuting, far_cuting);//设置指定纬度过滤的范围
pass.filter (*indices);//执行滤波,保存滤波结果在上述索引中
}
// 区域生长算法的5个参数
pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg;//创建区域生长分割对象
reg.setMinClusterSize (50);//设置一个聚类需要的最小点数
reg.setMaxClusterSize (1000000);//设置一个聚类需要的最大点数
reg.setSearchMethod (tree);//设置搜索方法
reg.setNumberOfNeighbours (30);//设置搜索的临近点数目
reg.setInputCloud (cloud);//设置输入点云
if(Bool_Cuting)reg.setIndices (indices);//通过输入参数设置,确定是否输入点云索引
reg.setInputNormals (normals);//设置输入点云的法向量
reg.setSmoothnessThreshold (SmoothnessThreshold / 180.0 * M_PI);//设置平滑阈值
reg.setCurvatureThreshold (CurvatureThreshold);//设置曲率阈值
std::vector <pcl::PointIndices> clusters;
reg.extract (clusters);//获取聚类的结果,分割结果保存在点云索引的向量中。
end = time(0);
diff[2] = difftime (end, start)-diff[0]-diff[1];
PCL_INFO ("\Region growing takes(seconds): %d\n", diff[2]);
std::cout << "Number of clusters is equal to " << clusters.size () << std::endl;//输出聚类的数量
std::cout << "First cluster has " << clusters[0].indices.size () << " points." << 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;
*/
PrintMemoryInfo();
pcl::PointCloud <pcl::PointXYZRGB>::Ptr colored_cloud = reg.getColoredCloud ();
pcl::visualization::CloudViewer viewer ("点云库PCL学习教程第二版-区域增长分割方法");
viewer.showCloud(colored_cloud);
while (!viewer.wasStopped ())
{
}//进行可视化
return (0);
}
实验结果:
cow.pcd
pig.pcd
original.pcd
五、基于颜色的区域生长——region growing rgb segmentation
pcl::RegionGrowingRGB
基于颜色的区域生长分割原理上和基于曲率、法线的分割方法是一致的。只不过比较目标换成了颜色,去掉了点云规模上 限的限制。可以认为,同一个颜色且挨得近,是一类的可能性很大,不需要上限来限制。所以这种方式比较适合用于室内场景分割。尤其是复杂室内场景,颜色分割可以轻松的将连续的场景点云变成不同的物体。哪怕是高低不平的地面,没法用采样一致分割器抽掉,颜色分割算法同样能完成分割任务。
算法步骤:
(1)分割,当前种子点和领域点之间色差小于色差阀值的视为一个聚类;
(2)合并,聚类之间的色差小于色差阀值和并为一个聚类,且当前聚类中点的数量小于聚类点数量的与最近的聚类合并在一起;
#include <iostream>
#include <vector>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/search/search.h>
#include <pcl/search/kdtree.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/filters/passthrough.h>
#include <pcl/segmentation/region_growing_rgb.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>
#include <pcl/console/time.h>
#include <pcl/features/normal_3d.h>
using namespace pcl::console;
int
main (int argc, char** argv)
{
if(argc<2)
{
std::cout<<".exe xx.pcd -b_n 0 -kn 50 -st_n 30 -ct_n 0.05 -bc 0 -fc 10 -nc 0 -dt 10 -ct 6 -rt 5 -mt 600" <<endl;
return 0;
}
time_t start,end,diff[5],option;
start = time(0);
bool Bool_Cuting=false,b_n=false;
int MinClusterSize=600,KN_normal=50;
float far_cuting=10,near_cuting=0,DistanceThreshold=10.0,ColorThreshold=6,RegionColorThreshold=5,SmoothnessThreshold=30.0,CurvatureThreshold=0.05;
parse_argument (argc, argv, "-b_n", b_n);
parse_argument (argc, argv, "-kn", KN_normal);
parse_argument (argc, argv, "-st_n", SmoothnessThreshold);
parse_argument (argc, argv, "-ct_n", CurvatureThreshold);
parse_argument (argc, argv, "-bc", Bool_Cuting);
parse_argument (argc, argv, "-fc", far_cuting);
parse_argument (argc, argv, "-nc", near_cuting);
parse_argument (argc, argv, "-dt", DistanceThreshold);
parse_argument (argc, argv, "-ct", ColorThreshold);
parse_argument (argc, argv, "-rt", RegionColorThreshold);
parse_argument (argc, argv, "-mt", MinClusterSize);
//frist step load the point cloud
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
if ( pcl::io::loadPCDFile <pcl::PointXYZRGB> (argv[1], *cloud) == -1)
{
std::cout << "Cloud reading failed." << std::endl;
return (-1);
}
end = time(0);
diff[0] = difftime (end, start);
PCL_INFO ("\Loading pcd file takes(seconds): %d\n", diff[0]);
pcl::search::Search <pcl::PointXYZRGB>::Ptr tree = boost::shared_ptr<pcl::search::Search<pcl::PointXYZRGB> > (new pcl::search::KdTree<pcl::PointXYZRGB>);
//Noraml estimation step(1 parameter)
pcl::search::Search<pcl::PointXYZRGB>::Ptr tree1 = boost::shared_ptr<pcl::search::Search<pcl::PointXYZRGB> > (new pcl::search::KdTree<pcl::PointXYZRGB>);
pcl::PointCloud <pcl::Normal>::Ptr normals (new pcl::PointCloud <pcl::Normal>);
pcl::NormalEstimation<pcl::PointXYZRGB, pcl::Normal> normal_estimator;
normal_estimator.setSearchMethod (tree);
normal_estimator.setInputCloud (cloud);
normal_estimator.setKSearch (KN_normal);
normal_estimator.compute (*normals);
end = time(0);
diff[1] = difftime (end, start)-diff[0];
PCL_INFO ("\Estimating normal takes(seconds): %d\n", diff[1]);
//Optional step: cutting away the clutter scene too far away from camera
pcl::IndicesPtr indices (new std::vector <int>);
if(Bool_Cuting)//是否通过z轴范围对待处理数据进行裁剪
{
pcl::PassThrough<pcl::PointXYZRGB> pass;
pass.setInputCloud (cloud);
pass.setFilterFieldName ("z");
pass.setFilterLimits (near_cuting, far_cuting);
pass.filter (*indices);
}
// Region growing RGB
pcl::RegionGrowingRGB<pcl::PointXYZRGB> reg;
reg.setInputCloud (cloud);
if(Bool_Cuting)reg.setIndices (indices);
reg.setSearchMethod (tree);
reg.setDistanceThreshold (DistanceThreshold);//点与点归类时欧式距离阈值
reg.setPointColorThreshold (ColorThreshold);//点与点归类时颜色阈值
reg.setRegionColorThreshold (RegionColorThreshold);//点与点归类时颜色阈值
reg.setMinClusterSize (MinClusterSize);
if(b_n)
{
reg.setSmoothModeFlag(true);
reg.setCurvatureTestFlag(true);
reg.setInputNormals (normals);
reg.setSmoothnessThreshold (SmoothnessThreshold / 180.0 * M_PI);
reg.setCurvatureThreshold (CurvatureThreshold);
}
std::vector <pcl::PointIndices> clusters;
reg.extract (clusters);
end = time(0);
diff[2] = difftime (end, start)-diff[0]-diff[1];
PCL_INFO ("\RGB region growing takes(seconds): %d\n", diff[2]);
pcl::PointCloud <pcl::PointXYZRGB>::Ptr colored_cloud = reg.getColoredCloud ();
pcl::visualization::CloudViewer viewer ("点云库PCL学习教程第二版实例-基于颜色的区域生长算法实现点云分割");
viewer.showCloud (colored_cloud);
while (!viewer.wasStopped ())
{
boost::this_thread::sleep (boost::posix_time::microseconds (100));
}
return (0);
}
实验结果:
two_human.pcd(显然,点云数据需要包含rgb)