三维目标检测:(四)实现点云的聚类并输出要分类的目标

点云的聚类算法

https://blog.csdn.net/luolaihua2018/article/details/120184539

欧式聚类流程

点云欧式聚类算法流程
(1)点云读入;
(2)体素化下采样(方便处理);
(3)去离散点;
(4)RANSAC算法检测平面,并剔除平面点云;
(5)欧式聚类;
(6)结果的输出和可视化;

代码(只输出点最多的一个类)

pcl::search::KdTree<PointType>::Ptr tree(new pcl::search::KdTree<PointType>);
        tree->setInputCloud(obj_cloud);//创建点云索引向量,用于存储实际的点云信息
        //用于存储聚类点的容器
        vector<pcl::PointIndices> cluster_indices;
        pcl::EuclideanClusterExtraction<PointType> ec;
        ec.setClusterTolerance(0.02); // 设置近邻搜索的搜索半径为2cm
        ec.setMinClusterSize(1000);//设置一个聚类需要的最少点数目为100
        ec.setMaxClusterSize(10000);//设置一个聚类需要的最大点数目为25000
        ec.setSearchMethod(tree);//设置点云的搜索机制
        ec.setInputCloud(obj_cloud);
        ec.extract(cluster_indices);//从点云中提取聚类,并将点云索引保存在cluster_indices中
		cloud_cluster = this->best_obj(cluster_indices,obj_cloud);
PointCloud<PointType>::Ptr pointcloud_seg::best_obj(const vector<pcl::PointIndices>& cluster_indices,const pcl::PointCloud<PointType>::Ptr &msg)
    {
    int index=cluster_indices.size();

    pcl::PointCloud<PointType>::Ptr cloud_cluster(new pcl::PointCloud<PointType>);
    int clu=0;
    if(index ==0)
    {
        cout<<"There are no object in this pointcloud!"<<endl;
        return msg;
    }
    int maxmum=cluster_indices[0].indices.size();
    for(int i=0;i<index;i++)
    {
       if(cluster_indices[i].indices.size()>maxmum)
       {
           maxmum=cluster_indices[i].indices.size();
           clu = i;
       }
    }
    pcl::PointIndices max=cluster_indices[clu];
    for (vector<int>::const_iterator pit = max.indices.begin(); pit != max.indices.end(); pit++) {
        cloud_cluster->points.push_back(msg->points[*pit]);
        cloud_cluster->header = msg->header;
        cloud_cluster->width = cloud_cluster->points.size();
        cloud_cluster->height = 1;
        cloud_cluster->is_dense = true;
    }
    std::cout << "PointCloud representing the Cluster: " << cloud_cluster->size() << " data points."<< std::endl;
    return cloud_cluster;
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值