PCL编程->基于PFH特征匹配测试

PFH特征匹配测试函数
本文介绍了一个基于PFH特征匹配的测试函数实现过程。该函数通过加载点云数据,进行体素滤波预处理,并进一步估计表面法线及PFH特征。最终,通过可视化工具展示处理后的点云及其法线。

基于PFH特征匹配的测试函数如下:

void algoritmpfh()
{
	pcl::PointCloud<pcl::PointXYZ>::Ptr target(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::PointCloud<pcl::PointXYZ>::Ptr ptr1(new pcl::PointCloud<pcl::PointXYZ>);
	//pcl::io::loadPCDFile("table_scene_mug_stereo_textured.pcd", *target);
	
	pcl::io::loadPCDFile("0000.pcd", *target);
	//pcl::PointXYZ min_coordinate, max_coordinate;
	//ofstream fout("results.txt", ios::app);
	pcl::VoxelGrid<pcl::PointXYZ> sor;
	sor.setInputCloud(target);
	sor.setLeafSize(15, 15, 15);
	sor.filter(*ptr1);
	pcl::io::savePCDFileBinary("0001.pcd",*ptr1);
	//pcl::getMinMax3D(*ptr1, min_coordinate, max_coordinate);
	// Create the normal estimation class, and pass the input dataset to it
	pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
	ne.setInputCloud(ptr1);

	// Create an empty kdtree representation, and pass it to the normal estimation object.
	// Its content will be filled inside the object, based on the given input dataset (as no other search surface is given).
	pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>());
	ne.setSearchMethod(tree);

	// Output datasets
	pcl::PointCloud<pcl::Normal>::Ptr cloud_normals(new pcl::PointCloud<pcl::Normal>);

	// Use all neighbors in a sphere of radius 3cm
	ne.setRadiusSearch(50);

	// Compute the features
	ne.compute(*cloud_normals);

	// Create the PFH estimation class, and pass the input dataset+normals to it
	pcl::PFHEstimation<pcl::PointXYZ, pcl::Normal, pcl::PFHSignature125> pfh;
	pfh.setInputCloud(ptr1);
	pfh.setInputNormals(cloud_normals);
	// alternatively, if cloud is of tpe PointNormal, do pfh.setInputNormals (cloud);

	// Create an empty kdtree representation, and pass it to the PFH estimation object.
	// Its content will be filled inside the object, based on the given input dataset (as no other search surface is given).
	//pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>());
	//pcl::KdTreeFLANN<pcl::PointXYZ>::Ptr tree (new pcl::KdTreeFLANN<pcl::PointXYZ> ()); -- older call for PCL 1.5-
	pfh.setSearchMethod(tree);

	// Output datasets
	pcl::PointCloud<pcl::PFHSignature125>::Ptr pfhs(new pcl::PointCloud<pcl::PFHSignature125>());

	// Use all neighbors in a sphere of radius 5cm
	// IMPORTANT: the radius used here has to be larger than the radius used to estimate the surface normals!!!
	pfh.setRadiusSearch(0.05);

	// Compute the features
	pfh.compute(*pfhs);

	// cloud_normals->points.size () should have the same size as the input cloud->points.size ()*
	boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
	viewer.reset(new pcl::visualization::PCLVisualizer("viewer", true));
	//pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> handle(ptr1, 0, 255, 0);
	//viewer->addPointCloud(ptr1, handle, "cloud");
	viewer->addPointCloudNormals<pcl::PointXYZ, pcl::Normal>(ptr1, cloud_normals);
	//viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud");
	viewer->resetCamera();

	while (!viewer->wasStopped())
	{
		viewer->spinOnce(100);
	}
	system("pause");
}


// 统计滤波器 void MainWindow::doActionPCLStatisticalOutlierRemoval() { if (getSelectedEntities().size() != 1) { ccLog::Print(QStringLiteral("只能选择一个点云实体")); return; } ccHObject* entity = getSelectedEntities()[0]; ccPointCloud* ccCloud = ccHObjectCaster::ToPointCloud(entity); // ---------------------------读取数据到PCL---------------------------------- pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); cloud->resize(ccCloud->size()); for (int i = 0; i < cloud->size(); ++i) { const CCVector3* point = ccCloud->getPoint(i); cloud->points[i].x = point->x; cloud->points[i].y = point->y; cloud->points[i].z = point->z; } // -----------------------------对话框--------------------------------------- //float radius = QInputDialog::getDouble(this, QStringLiteral("参数设置"), QStringLiteral("搜索半径: "), 0.1, 0, 100, 4); // ----------------------------统计滤波器-------------------------------------- pcl::PointCloud<pcl::PointXYZ>::Ptr filtered(new pcl::PointCloud<pcl::PointXYZ>); pcl::StatisticalOutlierRemoval<pcl::PointXYZ> us; us.setInputCloud(cloud); //设置待滤波点云 us.setMeanK(50); //设置查询点近邻点的个数 us.setStddevMulThresh(1.0); //设置标准差乘数,来计算是否为离群点的阈值 //sor.setNegative(true); //默认false,保存内点;true,保存滤掉的离群点 us.filter(*filtered); // ------------------------PCL->CloudCompare-------------------------------- if (!filtered->empty()) { ccPointCloud* newPointCloud = new ccPointCloud(QString("StatisticalOutlierRemoval")); for (int i = 0; i < filtered->size(); ++i) { double x = filtered->points[i].x; double y = filtered->points[i].y; double z = filtered->points[i].z; newPointCloud->addPoint(CCVector3(x, y, z)); } newPointCloud->setRGBColor(ccColor::Rgba(255, 255, 255, 255)); newPointCloud->showColors(true); if (ccCloud->getParent()) { ccCloud->getParent()->addChild(newPointCloud); } ccCloud->setEnabled(false); addToDB(newPointCloud); refreshAll(); updateUI(); } else { ccCloud->setEnabled(true); // Display a warning message in the console dispToConsole("Warning: example shouldn't be used as is", ccMainAppInterface::WRN_CONSOLE_MESSAGE); } } 统计滤波器代码解析
07-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值