StatisticalOutlierRemoval:离群点移除
生活随笔
收集整理的這篇文章主要介紹了
StatisticalOutlierRemoval:离群点移除
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
官方代碼解析
首先解析一波官方代碼:
pcl::PCDReaderreader;//定義讀取對象 reader.read<pcl::PointXYZ>("table_scene_lms400.pcd",*cloud);//讀取點云文件然后,創建了一個pcl::StatisticalOutlierRemoval濾波器,將對每個點分析的臨近點個數設為50,并將標準差倍數設為1,這意味著如果一個點的距離超出平均距離一個標準差以上,則該點被標記為離群點,并將被移除。計算后的輸出結果儲存在cloud_filtered中。
pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;// 創建濾波器對象 sor.setInputCloud(cloud); //設置呆濾波的點云 sor.setMeanK(50); //設置在進行統計時考慮查詢點鄰近點數 sor.setStddevMulThresh(1.0); //設置判斷是否為離群點的閾值 sor.filter(*cloud_filtered); //執行濾波處理保存內點到cloud_filtered剩下的數據(內部點)將被存入磁盤,以供其他使用,例如可視化等。
pcl::PCDWriterwriter; writer.write<pcl::PointXYZ>("table_scene_lms400_inliers.pcd",*cloud_filtered,false);然后,使用同樣的參數再次調用該濾波器,但是利用函數setNegative設置使輸出取外點,以獲取離群點數據(也就是原本濾除掉的點)。
sor.setNegative(true); sor.filter(*cloud_filtered);并將數據寫回到磁盤。
writer.write<pcl::PointXYZ>("table_scene_lms400_outliers.pcd",*cloud_filtered,false);實戰數據:
#include <iostream> #include <vector> #include <ctime> //--------------------------------------------- #include <pcl/point_types.h> #include <pcl/point_cloud.h> #include <pcl/io/pcd_io.h> #include <pcl/io/vtk_lib_io.h> //obj讀取頭文件 #include <pcl/visualization/pcl_visualizer.h> #include <pcl/kdtree/kdtree_flann.h> #include <pcl/features/normal_3d.h> //-法向顯示錯誤:no override found for vtkActor-- #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRenderingOpenGL); //outlier #include <pcl/filters/statistical_outlier_removal.h>using namespace std;int main() {pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);pcl::PointCloud<pcl::PointXYZ>::Ptr bottom(new pcl::PointCloud<pcl::PointXYZ>);//-------------------------------讀取點云文件---------------------------------------------------pcl::PolygonMesh mesh;if (pcl::io::loadPolygonFile("D:/pcd/shoeData/0422/0423-1.obj", mesh) == -1){cout << "COULD NOT READ FILE mid.pcl \n";system("pause");return (-1);}pcl::fromPCLPointCloud2(mesh.cloud, *cloud);cout << "points sieze is:" << cloud->size() << endl;//outlierpcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;sor.setInputCloud(cloud);sor.setMeanK(200); //設置在進行統計時考慮查詢點鄰近點數sor.setStddevMulThresh(1.0); //設置判斷是否為離群點的閾值,如果一個點的距離超出平均距離一個標準差以上,則該點被標記為離群點,并將被移除。sor.filter(*cloud_filtered);//---------------------------------顯示---------------------------------------------------------------------boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D viewer"));int v1(0);viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1);//xmin, ymin, xmax, ymax,取值范圍0-1viewer->setBackgroundColor(0, 0, 0, v1);pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> green0(cloud, 0, 225, 0);viewer->addPointCloud(cloud, green0, "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);pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> green1(cloud_filtered, 0, 225, 0);viewer->addPointCloud(cloud_filtered, green1, "cloud_filtered", v2);while (!viewer->wasStopped()) {viewer->spinOnce(100);}system("pause");return 0; }效果圖
總結
以上是生活随笔為你收集整理的StatisticalOutlierRemoval:离群点移除的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 美团技术:百亿规模API网关服务Shep
- 下一篇: 阿里合伙人程立:阿里15年,我撕掉了身上