一、聚类
欧式聚类详解(点云数据处理)_JAT0929的博客-CSDN博客
物以类聚,人以穷分,大概就是这个意思。
二、欧式聚类
using namespace std;
typedef pcl::PointXYZRGB PointT;
int main()
{
std::string filename = "C:\\Users\\Albert\\Desktop\\DATA\\trees.pcd";
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>());
if (pcl::io::loadPCDFile<pcl::PointXYZRGB>(filename, *cloud) == -1) //* 读入PCD格式的文件,如果文件不存在,返回-1
{
PCL_ERROR("Couldn't read file test_pcd.pcd \n"); //文件不存在时,返回错误,终止程序。
return (-1);
}
cout << " 点云大小: " << cloud->size() << endl;
// 欧式聚类
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZRGB>());
pcl::EuclideanClusterExtraction<pcl::PointXYZRGB> ece;// 欧式聚类
ece.setInputCloud(cloud);
ece.setClusterTolerance(0.5); // 设置近邻的搜索半径
ece.setMinClusterSize(1000); // 设置最小的聚类点数
ece.setMaxClusterSize(1000000); // 设置最大的聚类点云点数
ece.setSearchMethod(tree);
vector<pcl::PointIndices> cluster_indices;
ece.extract(cluster_indices); // 输出点云的聚类别
pcl::PCDWriter writer;
int j = 0;
for (vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin(); it != cluster_indices.end();++it)
{
pcl::PointCloud<PointT>::Ptr cloud_cluster(new pcl::PointCloud<PointT>);
for (vector<int>::const_iterator pit = it->indices.begin(); pit != it->indices.end(); pit++)
cloud_cluster->points.push_back(cloud->points[*pit]);
stringstream ss;
ss << "" << j + 1 << ".pcd";
writer.write<pcl::PointXYZRGB>(ss.str(),*cloud_cluster,true);
j++;
}
system("pause");
return 0;
}