VoxelGrid体素滤波实现

博客展示了如何用更简洁的代码实现体素滤波,将原本复杂的点云处理算法优化,减少代码行数,提高效率。通过创建三维体素网格并计算每个体素的质心来过滤输入点云。

原理
在这里插入图片描述在这里插入图片描述

在这里插入图片描述
实现

/**
 * @description:			体素滤波
 * @param cloud				输入点云
 * @param cloud_filtered	滤波点云
 * @param leafsize			体素大小
 */
void voxelgrid(pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud, pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud_filtered, float leafsize)
{
	pcl::PointXYZ minPt, maxPt;
	pcl::getMinMax3D(*cloud, minPt, maxPt);
	float lx = maxPt.x - minPt.x;
	float ly = maxPt.y - minPt.y;
	float lz = maxPt.z - minPt.z;
	int nx = lx / leafsize + 1;
	int ny = ly / leafsize + 1;
	int nz = lz / leafsize + 1;

    std::vector<std::pair<int,int>> v;
	for (size_t i = 0; i < cloud->points.size(); i++)
	{
		int ix = (cloud->points[i].x - minPt.x) / leafsize;
		int iy = (cloud->points[i].y - minPt.y) / leafsize;
		int iz = (cloud->points[i].z - minPt.z) / leafsize;
		v.push_back(std::pair<int, int>{ix + iy*nx + iz*nx*ny, i});
	}

    std::sort(v.begin(), v.end(), [](std::pair<int, int> p1, std::pair<int, int>p2) {return p1.first < p2.first; });

	int start = 0;
    std::vector<int> point_id;
	pcl::PointCloud<pcl::PointXYZ>::Ptr ptcloud;
	Eigen::Vector4f centroid;
	for (int i = start; i < v.size() - 1; ++i)
	{
		if (v[i].first != v[i + 1].first)
		{
			for (int id = start; id <= i; ++id)	point_id.push_back(v[i].second);
			ptcloud.reset(new pcl::PointCloud<pcl::PointXYZ>);
			pcl::copyPointCloud(*cloud, point_id, *ptcloud);
			pcl::compute3DCentroid(*ptcloud, centroid);
			cloud_filtered->push_back(pcl::PointXYZ(centroid[0], centroid[1], centroid[2]));
			start = i + 1;
			point_id.clear();
		}
		else if (v[i].first == v[v.size() - 1].first)
		{
			point_id.push_back(v[i].second);
		}
	}
	ptcloud.reset(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::copyPointCloud(*cloud, point_id, *ptcloud);
	pcl::compute3DCentroid(*ptcloud, centroid);
	cloud_filtered->push_back(pcl::PointXYZ(centroid[0], centroid[1], centroid[2]));
}

今天看了下之前写的代码,什么玩意真的那么复杂,明明不到30行就能实现:

/**
 * @description:			体素滤波
 * @param cloud				输入点云
 * @param cloud_filtered	滤波点云
 * @param leafsize			体素大小
 */
void voxelgrid(pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud, pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud_filtered, float leafsize)
{
	pcl::PointXYZ minPt, maxPt;
	pcl::getMinMax3D(*cloud, minPt, maxPt);
	float lx = maxPt.x - minPt.x;
	float ly = maxPt.y - minPt.y;
	float lz = maxPt.z - minPt.z;
	int nx = lx / leafsize + 1;
	int ny = ly / leafsize + 1;
	int nz = lz / leafsize + 1;

	std::vector<pcl::PointCloud<pcl::PointXYZ>> v(nx * ny * nz);
	for (size_t i = 0; i < cloud->points.size(); i++)
	{
		int ix = (cloud->points[i].x - minPt.x) / leafsize;
		int iy = (cloud->points[i].y - minPt.y) / leafsize;
		int iz = (cloud->points[i].z - minPt.z) / leafsize;
		v[ix + iy * nx + iz * nx * ny].push_back(cloud->points[i]);
	}

	for (int i = 0; i < v.size(); ++i)
	{
		if (v[i].size())
		{
			Eigen::Vector4f centroid;
			pcl::compute3DCentroid(v[i], centroid);
			cloud_filtered->push_back(pcl::PointXYZ(centroid.x(), centroid.y(), centroid.z()));
		}
	}
}

代码传送门:https://github.com/taifyang/PCL-algorithm

评论 9
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

给算法爸爸上香

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值