点云学习----PCL点云滤波2-体素滤波

体素滤波:

        个人理解就是把整个点云数据按照某个设定的大小,将其分割成一个个小的立方块儿,在每个立方块儿中选取一个有代表性的点(可以是立方块儿中所有点的重心点也可以是中心点,或者是距离中心最近的点),过滤掉其他点,这样可以保持点云的整体轮廓和形状。

以下代码分别采用重心(VoxelGrid)、中心(ApproximateVoxelGrid)以及距离中心最近的点(用kd树进行计算)分别滤波并展示:

这里说明一下:

        1.前两个过滤后的点可能不是原始点云中的点,第三个一定是原始点云数据中的点;

        2.VoxelGrid滤波中可联合使用setLeafSize函数和setMinimumPointsNumberPerVoxel函数实现每个体素内的最小点数的约束

#include <iostream>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/approximate_voxel_grid.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>
#include <liblas/liblas.hpp>
using namespace std;
//Demo5 体素滤波
int main(int argc, char** argv)
{
	//读取点云
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud <pcl::PointXYZ>);
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered1(new pcl::PointCloud<pcl::PointXYZ>);
	//pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered2(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::PCDReader reader;
	reader.read("D:/bunny.pcd", *cloud);
	cout << "Raw point :" << cloud->width * cloud->height << endl;

	//体素滤波	 取最小体素内的重心
	pcl::VoxelGrid<pcl::PointXYZ> vg;
	vg.setInputCloud(cloud);
	//设置最小体素边长
	vg.setLeafSize(0.01f, 0.01f, 0.01f);

	//setLeafSize参数的另一种初始化方式
	//Eigen::Vector4f leaf_size{ 0.01,0.01,0.01,0 };
	//vg.setLeafSize(leaf_size);
	//vg.setMinimumPointsNumberPerVoxel(10);      // 设置每一个体素内需要包含的最小点个数

	vg.filter(*cloud_filtered);
	cout << "Filtered point :" << cloud_filtered->width * cloud_filtered->height << endl;

	//体素滤波 取中心
	pcl::ApproximateVoxelGrid<pcl::PointXYZ> avf;
	avf.setInputCloud(cloud);
	avf.setLeafSize(0.01, 0.01, 0.01);
	avf.filter(*cloud_filtered1);
	cout << "Filtered1 point :" << cloud_filtered1->width * cloud_filtered1->height << endl;

	//体素滤波 取最小体素内距离中心最近的实际点
	//K近邻搜索
	pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;
	kdtree.setInputCloud(cloud);
	//
	pcl::PointIndicesPtr inds = std::shared_ptr<pcl::PointIndices>(new pcl::PointIndices());
	for (size_t i = 0; i < cloud_filtered1->points.size(); i++)
	{
		pcl::PointXYZ searchpoint;
		searchpoint.x = cloud_filtered1->points[i].x;
		searchpoint.y = cloud_filtered1->points[i].y;
		searchpoint.z = cloud_filtered1->points[i].z;

		int k = 1;
		vector<int> pointIdxNKNSearch(k);//k个元素的vector
		vector<float> pointNKNSquaredDistance(k);
		if (kdtree.nearestKSearch(searchpoint, k, pointIdxNKNSearch, pointNKNSquaredDistance) > 0)
		{
			inds->indices.push_back(pointIdxNKNSearch[0]);
		}

	}
	pcl::PointCloud<pcl::PointXYZ>::Ptr final_filtered(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::copyPointCloud(*cloud, inds->indices, *final_filtered);
	cout << "Filtered3 point :" << final_filtered->width * final_filtered->height << endl;

	//结果可视化
	boost::shared_ptr<pcl::visualization::PCLVisualizer> Viewer(new pcl::visualization::PCLVisualizer("show"));

	int v1(0);
	int v2(0);
	int v3(0);
	int v4(0);
	Viewer->createViewPort(0.0, 0.0, 0.5, 0.5, v1);
	Viewer->setBackgroundColor(0, 0, 0, v1);
	Viewer->addText("Raw point clouds", 10, 10, "v1_text", v1);
	Viewer->addPointCloud<pcl::PointXYZ>(cloud, "Raw cloud", v1);
	Viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1, 0, 0, "Raw cloud", v1);

	Viewer->createViewPort(0.5, 0.0, 1.0, 0.5, v2);
	Viewer->setBackgroundColor(0, 0, 0, v2);
	Viewer->addText("Filtered point clouds", 10, 10, "v2_text", v2);
	Viewer->addPointCloud<pcl::PointXYZ>(cloud_filtered, "Filtered cloud", v2);
	Viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0, 1, 0, "Filtered cloud", v2);

	Viewer->createViewPort(0.0, 0.5, 0.5, 1.0, v3);
	Viewer->setBackgroundColor(0, 0, 0, v3);
	Viewer->addText("Filtered1 point clouds", 10, 10, "v3_text", v3);
	Viewer->addPointCloud<pcl::PointXYZ>(cloud_filtered1, "Filtered1 cloud", v3);
	Viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0, 0, 1, "Filtered1 cloud", v3);

	Viewer->createViewPort(0.5, 0.5, 1.0, 1.0, v4);
	Viewer->setBackgroundColor(0, 0, 0, v4);
	Viewer->addText("Filtered1 point clouds", 10, 10, "v4_text", v4);
	Viewer->addPointCloud<pcl::PointXYZ>(final_filtered, "Filtered2 cloud", v4);
	Viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1, 0, 1, "Filtered2 cloud", v4);

	Viewer->setWindowName("VoxelGrid");

	while (!Viewer->wasStopped())
	{
		Viewer->spinOnce(100);
		boost::this_thread::sleep(boost::posix_time::microseconds(100000));
		
	}
	return 0;
}

结果展示:

 具体算法原理及详细介绍可移步参考文献:

PCL 改进体素滤波_点云侠的博客-CSDN博客

PCL体素滤波器_点云侠的博客-CSDN博客_pcl 体素滤波

点云体素下采样 ❤️(体素质心 | 体素中心)_孙 悟 空的博客-CSDN博客_体素中心

PCL 最小点数约束的VoxelGrid体素滤波_点云侠的博客-CSDN博客

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值