一、PassThrough 滤波
#include
#include
#include
#include
#include
#include
int
main(int argc, char** argv)
{
pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
pcl::PointCloud::Ptr cloud_filtered(new pcl::PointCloud);
// 填入点云数据
pcl::PCDReader reader;
// 把路径改为自己存放文件的路径
reader.read("table_scene_lms400.pcd", *cloud);
std::cerr << "Cloud before filtering: " << std::endl;
std::cerr << *cloud << std::endl;
pcl::visualization::PCLVisualizer viewer("滤波");
int v1(0); //设置左右窗口
int v2(1);
viewer.createViewPort(0.0, 0.0, 0.5, 1, v1);
viewer.setBackgroundColor(0, 0, 0, v1);
viewer.createViewPort(0.5, 0.0, 1, 1, v2);
viewer.setBackgroundColor(0.5, 0.5, 0.5, v2);
pcl::visualization::PointCloudColorHandlerCustom cloud_out_green(cloud, 20, 180, 20); // 显示绿色点云
viewer.addPointCloud(cloud, cloud_out_green, "cloud_out1", v1);
pcl::PassThrough pass; //创建滤波器 pass
pass.setInputCloud(cloud);
pass.setFilterFieldName("y");
pass.setFilterLimits(0.4, 0.9);
pass.setFilterFieldName("x");
pass.setFilterLimits(-0.8, 0.65);
//pass.setFilterLimitsNegative (true);
pass.filter(*cloud_filtered);
std::cerr << "Cloud after filtering: " << std::endl;
std::cerr << *cloud_filtered << std::endl;
/*pcl::PCDWriter writer;*/
//writer.write ("table_scene_lms400_inliers.pcd", *cloud_filtered, false);
//sor.setNegative (true);
//sor.filter (*cloud_filtered);
//writer.write ("table_scene_lms400_outliers.pcd", *cloud_filtered, false);
pcl::visualization::PointCloudColorHandlerCustom cloud_out_orage(cloud_filtered, 250, 128, 10); //显示橘色点云
viewer.addPointCloud(cloud_filtered, cloud_out_orage, "cloud_out2", v2);
//viewer.setSize(960, 780);
while (!viewer.wasStopped())
{
viewer.spinOnce();
}
return 0;
}
二、StatisticalOutlierRemoval
#include
#include
#include
#include
#include
#include
int
main (int argc, char** argv)
{
pcl::PointCloud::Ptr cloud (new pcl::PointCloud);
pcl::PointCloud::Ptr cloud_filtered (new pcl::PointCloud);
// 填入点云数据
pcl::PCDReader reader;
// 把路径改为自己存放文件的路径
reader.read ("table_scene_lms400.pcd", *cloud);
std::cerr << "Cloud before filtering: " << std::endl;
std::cerr << *cloud << std::endl;
pcl::visualization::PCLVisualizer viewer("滤波 StatisticalOutlierRemoval ");
int v1(0); //设置左右窗口
int v2(1);
viewer.createViewPort(0.0, 0.0, 0.5, 1, v1);
viewer.setBackgroundColor(0, 0, 0, v1);
viewer.createViewPort(0.5, 0.0, 1, 1, v2);
viewer.setBackgroundColor(0.5, 0.5, 0.5, v2);
pcl::visualization::PointCloudColorHandlerCustom cloud_out_green(cloud, 20, 180, 20); // 显示绿色点云
viewer.addPointCloud(cloud, cloud_out_green, "cloud_out1", v1);
//创建滤波器对象
pcl::StatisticalOutlierRemoval sor;
sor.setInputCloud (cloud);
sor.setMeanK (50);
sor.setStddevMulThresh (1.0);
sor.filter (*cloud_filtered);
std::cerr << "Cloud after filtering: " << std::endl;
std::cerr << *cloud_filtered << std::endl;
pcl::PCDWriter writer;
//writer.write ("table_scene_lms400_inliers.pcd", *cloud_filtered, false);
//sor.setNegative (true);
//sor.filter (*cloud_filtered);
//writer.write ("table_scene_lms400_outliers.pcd", *cloud_filtered, false);
pcl::visualization::PointCloudColorHandlerCustom cloud_out_orage(cloud_filtered, 250, 128, 10); //显示橘色点云
viewer.addPointCloud(cloud_filtered, cloud_out_orage, "cloud_out2", v2);
viewer.setSize(960, 780);
while (!viewer.wasStopped())
{
viewer.spinOnce();
}
return 0;
}