欧式聚类分割
pcl::EuclideanClusterExtraction是基于欧式距离提取集群的方法,仅依据距离,将小于距离阈值的点云作为一个集群。
具体的实现方法大致是:
(1) 找到空间中某点p10,由kdTree找到离他最近的n个点,判断这n个点到p的距离;
(2) 将距离小于阈值r的点p12、p13、p14…放在类Q里;
(3) 在 Q\p10 里找到一点p12,重复1;
(4) 在 Q\p10、p12 找到一点,重复1,找到p22、p23、p24…全部放进Q里;
(5) 当 Q 再也不能有新点加入了,则完成搜索了。
//****欧式聚类分割****//
#include
#include
#include
#include
#include
using namespace std;
int
main (int argc, char** argv)
{
// 读取点云数据
pcl::PCDReader reader;
pcl::PointCloud
reader.read ("test.pcd", *cloud);
pcl::search::KdTree
tree->setInputCloud(cloud);
std::vector
pcl::EuclideanClusterExtraction
ec.setClusterTolerance(3); //设置近邻搜索的搜索半径
ec.setMinClusterSize(5000); //设置最小聚类尺寸
ec.setMaxClusterSize(100000);
ec.setSearchMethod(tree);
ec.setInputCloud(cloud);
ec.extract(cluster_indices);
std::vector
for (std::vector
{
pcl::PointCloud
for (std::vector
cloud_cluster->points.push_back(cloud->points[*pit]);
cloud_cluster->width = cloud_cluster->points.size ();
cloud_cluster->height = 1;
cloud_cluster->is_dense = true;
Eucluextra.push_back(cloud_cluster);
}
//可视化
pcl::visualization::PCLVisualizer viewer("PCLVisualizer");
viewer.initCameraParameters();
int v1(0);
viewer.createViewPort(0.0, 0.0, 0.5, 1.0, v1);
viewer.setBackgroundColor(128.0 / 255.0, 138.0 / 255.0, 135.0 / 255.0, v1);
viewer.addText("Cloud before segmenting", 10, 10, "v1 test", v1);
viewer.addPointCloud
int v2(0);
viewer.createViewPort(0.5, 0.0, 1.0, 1.0, v2);
viewer.setBackgroundColor(128.0 / 255.0, 138.0 / 255.0, 135.0 / 255.0, v2);
viewer.addText("Cloud after segmenting", 10, 10, "v2 test", v2);
for (int i = 0; i < Eucluextra.size(); i++)
{
CString cstr;
cstr.Format(_T("cloud_segmented%d"), i);
cstr += _T(".pcd");
string str_filename = CStringA(cstr);
//显示分割得到的各片点云
pcl::visualization::PointCloudColorHandlerCustom
viewer.addPointCloud(Eucluextra[i], color, str_filename, v2);
}
while (!viewer.wasStopped())
{
viewer.spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
return (0);
}
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
68
69
70
71
72
73
————————————————
版权声明:本文为CSDN博主「饿得太久吃不下」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/fei_12138/article/details/109718785