一、介绍
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 <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/passthrough.h>
#include <pcl/visualization/cloud_viewer.h>
int main()
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
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<pcl::PointXYZ>::Ptr filter(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PassThrough<pcl::PointXYZ> 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;
}
2、VoxelGrid filter
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>
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<pcl::PCLPointCloud2> 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、StatisticalOutlierRemoval
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/filters/statistical_outlier_removal.h>
int main()
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
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<pcl::PointXYZ>::Ptr filter(new pcl::PointCloud<pcl::PointXYZ>);
pcl::StatisticalOutlierRemoval<pcl::PointXYZ> 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;
}
4、Projecting points
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/project_inliers.h>
#include <pcl/visualization/cloud_viewer.h>
int main()
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
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<pcl::PointXYZ>::Ptr filter(new pcl::PointCloud<pcl::PointXYZ>);
pcl::ProjectInliers<pcl::PointXYZ> 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;
}
三、参考
Introduction — Point Cloud Library 0.0 documentation