点云裁剪--PassThrough点云裁剪

1.版本要求

版本: >PCL1.0

2.简介

点云裁剪是点云目标检测不可缺少的预处理环节,截取目标所在区域点云对于目标检测的可靠性和效率都有着不可取代的作用。PassThrough是PCL开源库中简单好用的点云裁剪手段。

3.数据

本例中使用的点云数据(test.pcd)请见百度网盘分享。
链接:https://pan.baidu.com/s/1QiK_-pRSTYXk6lZHx36pgg
提取码:ykzl

4.代码

#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/filters/passthrough.h>
#include <string>
#include <sstream>

int main(int argc, char**argv)
{
	//读入测试点云test.pcd
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_PassThrough_trim(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::io::loadPCDFile("test.pcd", *cloud);
	
	//创建x轴裁剪对象
	pcl::PassThrough<pcl::PointXYZ> passX;
	passX.setInputCloud(cloud);
	passX.setFilterFieldName("x");
	passX.setFilterLimits(-8, 8);  //裁剪保留区域 -8m-8m
	passX.filter(*cloud_PassThrough_trim);

	//创建y轴裁剪对象
	pcl::PassThrough<pcl::PointXYZ> passY;
	passY.setInputCloud(cloud_PassThrough_trim);
	passY.setFilterFieldName("y");
	passY.setFilterLimits(-8, 8);  //裁剪保留区域 -8m-8m
	passY.filter(*cloud_PassThrough_trim);

	//创建z轴裁剪对象
	pcl::PassThrough<pcl::PointXYZ> passZ;
	passZ.setInputCloud(cloud_PassThrough_trim);
	passZ.setFilterFieldName("z");
	passZ.setFilterLimits(0, 10);  //裁剪保留区域 0m-10m
	passZ.filter(*cloud_PassThrough_trim);

	//输出降裁剪前的点云数量和裁剪后的点云数量
	std::cout << "points quantity before Pass Through trim:" << cloud->size() << std::endl
		<< "points quantity after Pass Through trim:" << cloud_PassThrough_trim->size() << std::endl;

	//对比显示裁剪前和裁剪后点云
	pcl::visualization::PCLVisualizer viewer("Cloud Viewer");
	int v1(0);  //创建左窗口显示裁剪前点云
	viewer.createViewPort(0, 0, 0.5, 1.0, v1);  //左右窗口大小划分,1:1
	viewer.setBackgroundColor(0, 0, 0, v1);
	viewer.addText("Original Cloud", 2, 2, "Original Cloud", v1);
	pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> rgb1(cloud, "z");
	viewer.addPointCloud<pcl::PointXYZ>(cloud, rgb1, "original cloud", v1);
	viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "original cloud", v1);
	viewer.addCoordinateSystem(1.0, "original cloud", v1);
	int v2(1);  //创建右窗口显示裁剪后点云
	viewer.createViewPort(0.5, 0, 1.0, 1.0, v2);  //左右窗口大小划分,1:1
	viewer.setBackgroundColor(0, 0, 0, v2);
	viewer.addText("Trimed Cloud", 2, 2, "Trimed Cloud", v2);
	pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> rgb2(cloud_PassThrough_trim, "z");
	viewer.addPointCloud<pcl::PointXYZ>(cloud_PassThrough_trim, rgb2, "trimed cloud", v2);
	viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "trimed cloud", v2);
	viewer.addCoordinateSystem(1.0, "trimed cloud", v2);

	viewer.spin(); //循环不断显示点云

	return 0;
}

5.效果

左图为裁剪前的点云显示,右图为裁剪后的点云显示。
点云裁剪--PassThrough点云裁剪

上一篇:点云检测--欧式聚类Euclidean Cluster


下一篇:点云特征--计算点云法向量normals