PCL 之离群点去除

基于半径(规定圆内和邻居个数限制)和条件(滤波域和值范围)实行离群点去除 


#include "stdafx.h"


//int _tmain(int argc, _TCHAR* argv[])
//{
//	return 0;
//}
#include
#include
#include
#include
#include

using namespace std;
int main(int argc, char** argv){
	if (argc != 2){
		cerr << "please specify commond line arg -r or -c" << endl;
		exit(0);
	}
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud), filtered_cloud(new pcl::PointCloud);

	//读入数据
	pcl::PCDReader pcdr;
	pcdr.read("table_scene_lms400.pcd", *cloud);
	 //Fill in the cloud data
	/*cloud->width = 10;
	cloud->height = 1;
	cloud->points.resize(cloud->width * cloud->height);

	for (size_t i = 0; i < cloud->points.size(); ++i)
	{
		cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
		cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
		cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
	}
*/
	if (strcmp(argv[1], "-r") == 0){
		//基于半径的离群点剔除
		pcl::RadiusOutlierRemoval  rout;
		rout.setInputCloud(cloud);
		rout.setRadiusSearch(0.01);//设置搜索半径的值
		rout.setMinNeighborsInRadius(15);//设置最小邻居个数,默认是1
		rout.filter(*filtered_cloud);
	}
	else if (strcmp(argv[1],"-c")==0)
	{
		//条件滤波器去除离群点
		pcl::ConditionAnd::Ptr range_c(new pcl::ConditionAnd());//初始化条件滤波对象
		range_c->addComparison(pcl::FieldComparison::ConstPtr(new pcl::FieldComparison("z", pcl::ComparisonOps::GT, 0.0)));//设定滤波条件z轴 GT表示greater than  LT表示less than
		range_c->addComparison(pcl::FieldComparison::ConstPtr(new pcl::FieldComparison("z", pcl::ComparisonOps::LT, 0.8)));
		pcl::ConditionalRemoval condrem;
		condrem.setCondition(range_c);
		condrem.setInputCloud(cloud);
		condrem.setKeepOrganized(true);//保持点云数据的结构
		condrem.filter(*filtered_cloud);
	}
	else{
		cerr << "please specify commond line arg -r or -c" << endl;
		exit(0);
	}
	cerr << "the size of cloud before filtering "<points.size() << endl;
	cerr << "the size of cloud after filtering " << argv[1] << " : " << filtered_cloud->points.size() << endl;
	pcl::PLYWriter plyw;
	plyw.write("origin_table.ply", *cloud);
	plyw.write("r_filtered_cloud.ply", *filtered_cloud); //根据方法的不同修改文件名
	return 0;
}

 

你可能感兴趣的:(C++,三维重建)