【激光雷达点云障碍物检测】cluster3d.cpp、cluster3d.h

自己理解的注释。

cluster3d.h

#ifndef PLAYBACK_CLUSTER3D_H
#define PLAYBACK_CLUSTER3D_H
#include <pcl/common/common.h>
#include <chrono>
#include <string>
#include "kdtree3d.h"

namespace lidar_obstacle_detection {
	// shorthand for point cloud pointer
	template<typename PointT>
	using PtCdtr = typename pcl::PointCloud<PointT>::Ptr;
	template<typename PointT>
	class ClusterPts 
	{
	private:
		int num_points;
		float distanceTol;
		int minClusterSize;
		int maxClusterSize;
		std::vector<bool> processed;
		std::vector<PtCdtr<PointT>> clusters;
	public:
		//构造函数
		ClusterPts(int nPts, float cTol, int minSize, int maxSize) : num_points(nPts), distanceTol(cTol),minClusterSize(minSize), maxClusterSize(maxSize) 
		{
			processed.assign(num_points, false);
		}
		~ClusterPts();
		void clusterHelper(int ind, PtCdtr<PointT> cloud, std::vector<int> &cluster, KdTree *tree);
		std::vector<PtCdtr<PointT>> EuclidCluster(PtCdtr<PointT> cloud);
	};
}
#endif //PLAYBACK_CLUSTER3D_H

 cluster3d.cpp

#include "cluster3d.h"
using namespace lidar_obstacle_detection;
template<typename PointT>
ClusterPts<PointT>::~ClusterPts() {}
//根据最近邻搜索的阈值,找到了类数,每一类包含了属于该类的点			ok
template<typename PointT>
void ClusterPts<PointT>::clusterHelper(int ind, PtCdtr<PointT> cloud, std::vector<int> &cluster, KdTree *tree) 
{
	//std::vector<bool> processed;
	processed[ind] = true;
	cluster.push_back(ind);
	//ec.setClusterTolerance(clusterTolerance);  设置近邻搜索半径
	std::vector<int> nearest_point = tree->search(cloud->points[ind], distanceTol); 

	for (int nearest_id : nearest_point) 
	{
		if (!processed[nearest_id]) 
		{
			clusterHelper(nearest_id, cloud, cluster, tree);
		}
	}
}

//重写欧式聚类算法		ok
template<typename PointT>
std::vector<PtCdtr<PointT>> ClusterPts<PointT>::EuclidCluster(PtCdtr<PointT> cloud) 
{
	KdTree *tree = new KdTree;  //创建重写kdtree的对象
	//对cloud创建kdtree
	for (int ind = 0; ind < num_points; ind++) 
	{
		tree->insert(cloud->points[ind], ind);
	}
	//std::vector<bool> processed;
	for (int ind = 0; ind < num_points; ind++) 
	{
		if (processed[ind]) 
		{
			ind++;
			continue;
		}
		std::vector<int> cluster_ind; //每一类包含点的索引
		PtCdtr<PointT> cloudCluster(new pcl::PointCloud<PointT>);
		clusterHelper(ind, cloud, cluster_ind, tree);

		int cluster_size = cluster_ind.size();  //总类数
		if (cluster_size >= minClusterSize && cluster_size <= maxClusterSize) 
		{
			for (int i = 0; i < cluster_size; i++) 
			{
				cloudCluster->points.push_back(cloud->points[cluster_ind[i]]);
			}
			cloudCluster->width = cloudCluster->points.size();
			cloudCluster->height = 1;
			clusters.push_back(cloudCluster);
		}
	}
	return clusters;
}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要实现激光雷达点云pcd障碍物识别并用open3d可视化,可以按照以下步骤进行: 1. 安装open3d和numpy库: ```python pip install open3d numpy ``` 2. 读取pcd文件: ```python import open3d as o3d pcd = o3d.io.read_point_cloud("example.pcd") ``` 3. 对点云进行下采样,以加快处理速度: ```python downpcd = pcd.voxel_down_sample(voxel_size=0.05) ``` 4. 对下采样后的点云进行平滑处理,以去除噪声: ```python downpcd.estimate_normals() downpcd.paint_uniform_color([1, 0.706, 0]) o3d.visualization.draw_geometries([downpcd]) ``` 5. 对处理后的点云进行聚类,找出障碍物: ```python with o3d.utility.VerbosityContextManager( o3d.utility.VerbosityLevel.Debug) as cm: labels = np.array(downpcd.cluster_dbscan(eps=0.5, min_points=10, print_progress=True)) max_label = labels.max() print(f"point cloud has {max_label + 1} clusters") colors = plt.get_cmap("tab20")(labels / (max_label if max_label > 0 else 1)) colors[labels < 0] = 0 downpcd.colors = o3d.utility.Vector3dVector(colors[:, :3]) o3d.visualization.draw_geometries([downpcd]) ``` 6. 可以将障碍物点云保存成独立的pcd文件: ```python for i in range(max_label + 1): indices = np.where(labels == i)[0] cloud_cluster = downpcd.select_down_sample(indices) o3d.io.write_point_cloud(f"cluster{i}.pcd", cloud_cluster) ``` 7. 最后,可以用open3d可视化显示障碍物识别结果: ```python o3d.visualization.draw_geometries([downpcd]) ``` 完整代码如下: ```python import numpy as np import open3d as o3d import matplotlib.pyplot as plt # 读取pcd文件 pcd = o3d.io.read_point_cloud("example.pcd") # 对点云进行下采样 downpcd = pcd.voxel_down_sample(voxel_size=0.05) # 对下采样后的点云进行平滑处理 downpcd.estimate_normals() downpcd.paint_uniform_color([1, 0.706, 0]) o3d.visualization.draw_geometries([downpcd]) # 对处理后的点云进行聚类,找出障碍物 with o3d.utility.VerbosityContextManager( o3d.utility.VerbosityLevel.Debug) as cm: labels = np.array(downpcd.cluster_dbscan(eps=0.5, min_points=10, print_progress=True)) max_label = labels.max() print(f"point cloud has {max_label + 1} clusters") colors = plt.get_cmap("tab20")(labels / (max_label if max_label > 0 else 1)) colors[labels < 0] = 0 downpcd.colors = o3d.utility.Vector3dVector(colors[:, :3]) o3d.visualization.draw_geometries([downpcd]) # 将障碍物点云保存成独立的pcd文件 for i in range(max_label + 1): indices = np.where(labels == i)[0] cloud_cluster = downpcd.select_down_sample(indices) o3d.io.write_point_cloud(f"cluster{i}.pcd", cloud_cluster) # 用open3d可视化显示障碍物识别结果 o3d.visualization.draw_geometries([downpcd]) ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值