点云滤波介绍

一、介绍

1、Filtering a PointCloud using a PassThrough filter

2、Downsampling a PointCloud using a VoxelGrid filter

3、Removing sparse outliers using StatisticalOutlierRemoval

4、Projecting points using a parametric model

数据集:链接:https://pan.baidu.com/s/1jR7k5iI-acVyyehKcYbetA?pwd=mr16
     提取码:mr16

二、代码

1、PassThrough filter

#include 
#include 
#include 
#include 
#include 

int main()
{
    pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	if (-1 == pcl::io::loadPCDFile("rabbit.pcd", *cloud))
	{
		std::cout << "read pcd file error!" << std::endl;
	}
	std::cout << "cloud1 size = " << cloud->points.size() << std::endl;

	pcl::PointCloud::Ptr filter(new pcl::PointCloud);
	pcl::PassThrough pass;
	pass.setInputCloud(cloud);
	pass.setFilterFieldName("x");
	pass.setFilterLimits(-10.0f, 0.0f);
	pass.filter(*filter);
	std::cout << "filter size = " << filter->points.size() << std::endl;

	pcl::visualization::CloudViewer viewer("Cloud Viewer");
	viewer.showCloud(filter);
	while (!viewer.wasStopped())
	{
	}

	return 0;
}

点云滤波介绍_第1张图片

点云滤波介绍_第2张图片 

 

2、VoxelGrid filter

#include 
#include 
#include 
#include 

int main()
{
    pcl::PCLPointCloud2::Ptr cloud(new pcl::PCLPointCloud2());
	pcl::PCDReader reader;
    reader.read("rabbit.pcd", *cloud);
	std::cout << "cloud size = " << cloud->data.size() << std::endl;

	pcl::PCLPointCloud2::Ptr filter(new pcl::PCLPointCloud2());
	pcl::VoxelGrid sor;
	sor.setInputCloud(cloud);
	sor.setLeafSize(0.5f, 0.5f, 0.5f);
	sor.filter(*filter);
	std::cout << "filter size = " << filter->data.size() << std::endl;

	pcl::PCDWriter writer;
	writer.write("rabbit_down.pcd", *filter, Eigen::Vector4f::Zero(), Eigen::Quaternionf::Identity(), false);

	return 0;
}

点云滤波介绍_第3张图片

点云滤波介绍_第4张图片 

 

3、StatisticalOutlierRemoval

#include 
#include 
#include 
#include 
#include 

int main()
{
    pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	if (-1 == pcl::io::loadPCDFile("filtered1.pcd", *cloud))
	{
		std::cout << "read pcd file error!" << std::endl;
	}
	std::cout << "cloud1 size = " << cloud->points.size() << std::endl;

	pcl::PointCloud::Ptr filter(new pcl::PointCloud);
	pcl::StatisticalOutlierRemoval sor;
	sor.setInputCloud(cloud);
	sor.setMeanK(80);
	sor.setStddevMulThresh(1.0);
	sor.filter(*filter);
	std::cout << "filter size = " << filter->points.size() << std::endl;

	pcl::visualization::CloudViewer viewer("Cloud Viewer");
	viewer.showCloud(filter);
	while (!viewer.wasStopped())
	{
	}

	return 0;
}

点云滤波介绍_第5张图片

点云滤波介绍_第6张图片  

4、Projecting points

#include 
#include 
#include 
#include 
#include 

int main()
{
    pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	if (-1 == pcl::io::loadPCDFile("filtered1.pcd", *cloud))
	{
		std::cout << "read pcd file error!" << std::endl;
	}
	std::cout << "cloud1 size = " << cloud->points.size() << std::endl;

	
	// z=0 plane
	pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());
	coefficients->values.resize(4);
	coefficients->values[0] = coefficients->values[1] = 0;
	coefficients->values[2] = 1.0;
	coefficients->values[3] = 0;

    pcl::PointCloud::Ptr filter(new pcl::PointCloud);
	pcl::ProjectInliers proj;
	proj.setModelType(pcl::SACMODEL_PLANE);
	proj.setInputCloud(cloud);
	proj.setModelCoefficients(coefficients);
	proj.filter(*filter);
	std::cout << "filter size = " << filter->points.size() << std::endl;

	pcl::visualization::CloudViewer viewer("Cloud Viewer");
	viewer.showCloud(filter);
	while (!viewer.wasStopped())
	{
	}

	return 0;
}

点云滤波介绍_第7张图片

点云滤波介绍_第8张图片 

 

三、参考

Introduction — Point Cloud Library 0.0 documentation

你可能感兴趣的:(点云,计算机视觉,PCL)