PCL:PassThrough ❤️ 直通滤波

文章目录

1 原理

针对点云某一维度,去掉指定范围内(或外)的点。

2 代码实现

#include <pcl/io/pcd_io.h>
#include <pcl/filters/passthrough.h>
#include <pcl/visualization/cloud_viewer.h>

using namespace std;

int main()
{
	//----------------------------------------- 加载点云 ----------------------------------------
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);	//待滤波点云
	if (pcl::io::loadPCDFile("desk.pcd", *cloud) < 0)
	{
		PCL_ERROR("点云文件不存在!\n");
		system("pause");
		return -1;
	}
	cout << "->加载点云个数:" << cloud->points.size() << endl;
	//==========================================================================================

	//----------------------------------------- 直通滤波 ----------------------------------------
	cout << "->正在进行直通滤波..." << endl;
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);	//滤波后点云
	pcl::PassThrough<pcl::PointXYZ> pt;	// 创建滤波器对象
	pt.setInputCloud(cloud);			//设置输入点云
	pt.setFilterFieldName("x");			//设置滤波所需字段z
	pt.setFilterLimits(-0.1, 1);		//设置Z字段过滤范围
	pt.setFilterLimitsNegative(true);	//默认false,保留范围内的点云;true,保存范围外的点云
	pt.filter(*cloud_filtered);			//执行滤波,并将滤波后点云保存到cloud_filtered中
	//==========================================================================================

	//-------------------------------------- 可视化(可选) -------------------------------------
	pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("滤波前后对比"));

	/*-----原始点云-----*/
	int v1(0);
	viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1); //设置第一个视口在X轴、Y轴的最小值、最大值,取值在0-1之间
	viewer->setBackgroundColor(0, 0, 0, v1); //设置背景颜色,0-1,默认黑色(0,0,0)
	viewer->addText("befor_filtered", 10, 10, "v1_text", v1);
	viewer->addPointCloud<pcl::PointXYZ>(cloud, "befor_filtered_cloud", v1);
	viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "befor_filtered_cloud", v1);
	viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1, 0, 0, "befor_filtered_cloud", v1);

	/*-----滤波后点云-----*/
	int v2(0);
	viewer->createViewPort(0.5, 0.0, 1.0, 1.0, v2);
	viewer->setBackgroundColor(0.3, 0.3, 0.3, v2);
	viewer->addText("after_filtered", 10, 10, "v2_text", v2);
	viewer->addPointCloud<pcl::PointXYZ>(cloud_filtered, "after_filtered_cloud", v2);
	viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "after_filtered_cloud", v2);
	viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0, 1, 0, "after_filtered_cloud", v2);

	while (!viewer->wasStopped())
	{
		viewer->spinOnce(100);
		boost::this_thread::sleep(boost::posix_time::microseconds(100000));
	}
	//==========================================================================================

	return 0;
}

3 结果展示

PCL:PassThrough ❤️ 直通滤波


相关链接:

PCL点云数据处理基础❤️❤️❤️目录

上一篇:PCL学习笔记(20)——remove_outliers


下一篇:Consumer的应用