PCL——快速邻域搜索

PCL——快速邻域搜索

以下代码参考《点云库PCL从入门到精通》

1. 代码示例

  • 用 k-d tree找到具体点在空间中的k近邻
  • 找到指定的某一半径内的所有近邻
    在这里插入图片描述
#include <pcl/point_cloud.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <iostream>
#include <vector>
#include <ctime>

using namespace std;
using namespace pcl;

int main(int argc, char**argv)
{
	// 用系统时间初始化随机种子
	srand(time(NULL));	
	PointCloud<PointXYZ>::Ptr cloud(new PointCloud<PointXYZ>);
	// 点云初始化
	cloud->width = 1000;
	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.0f* rand() / (RAND_MAX + 1.0f);
		cloud->points[i].y = 1024.0f* rand() / (RAND_MAX + 1.0f);
		cloud->points[i].z = 1024.0f* rand() / (RAND_MAX + 1.0f);
	}

	// 创建k-d tree对象
	KdTreeFLANN<PointXYZ> kdtree;
	// 设置搜索的点云
	kdtree.setInputCloud(cloud);
	// 定义查询点并赋值
	PointXYZ searchPoint;
	searchPoint.x = 1024.0f* rand() / (RAND_MAX + 1.0f);
	searchPoint.y = 1024.0f* rand() / (RAND_MAX + 1.0f);
	searchPoint.z = 1024.0f* rand() / (RAND_MAX + 1.0f);

	// k近邻搜索 找最近的K个点,最终的点数有可能小于K
	int K = 10;
	vector<int> pointIdxNKNSearch(K);			//存储搜索到查询点近邻的索引
	vector<float> pointNKNSquaredDistance(K);	//存储对应近邻的平方距离

	cout << "K nearest neighbor search at (" 
		<< searchPoint.x
		<< " " << searchPoint.y
		<< " " << searchPoint.z
		<< ") with K=" << K << endl;

	cout << endl;

	//n 是返回的点的数量
	//所有的返回点已经按照近邻平方距离排列好了,然后对应索引从小到大排列
	int n = kdtree.nearestKSearch(searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance);
	if (n > 0)
	{
		for (size_t i = 0; i < pointIdxNKNSearch.size(); ++i)
		{
			cout << " " << cloud->points[pointIdxNKNSearch[i]].x
				 << " " << cloud->points[pointIdxNKNSearch[i]].y
				 << " " << cloud->points[pointIdxNKNSearch[i]].z
				 << " (squared distance: " << pointNKNSquaredDistance[i] << ")" << endl;
		}
		cout << endl;
	}


	// 在半径r内搜索近邻 找半径内的所有点,点的数量无法确定,有可能一个都没有,也有可能有很多
	vector<int> pointIdxRadiusSearch;
	vector<float> pointRadiusSquaredDistance;
	float radius = 100;

	cout << "Neighbors within radius search at (" << searchPoint.x
		<< " " << searchPoint.y
		<< " " << searchPoint.z
		<< ") with radius=" << radius << endl;
	cout << endl;

	//n 是返回的点的数量
	//所有的返回点已经按照近邻平方距离排列好了,然后对应索引从小到大排列
	n = kdtree.radiusSearch(searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance);

	if (n >0)
	{
		for (size_t i = 0; i < pointIdxRadiusSearch.size(); ++i)
		{
			cout << "    " << cloud->points[pointIdxRadiusSearch[i]].x
				<< " " << cloud->points[pointIdxRadiusSearch[i]].y
				<< " " << cloud->points[pointIdxRadiusSearch[i]].z
				<< " (squared distance: " << pointRadiusSquaredDistance[i] << ")" << endl;
		}
	}

	system("Pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MechMaster

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值