PCL用KDtree,给搜索到的邻近点上色

用KDtree,给搜索到的邻近点上色。

复制代码
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>

#include <pcl/search/kdtree.h> // 包含kdtree头文件
#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>
#include <pcl/common/centroid.h>

typedef pcl::PointXYZ PointT;

int main()
{
	pcl::PointCloud<PointT>::Ptr cloud(new pcl::PointCloud<PointT>);
	pcl::io::loadPCDFile("../new.pcd", *cloud);

	pcl::visualization::PCLVisualizer viewer; 
	viewer.setBackgroundColor(100, 100, 100); // rgb
	pcl::visualization::PointCloudColorHandlerCustom<PointT> red1(cloud, 0, 0, 0); // rgb
	viewer.addPointCloud(cloud, red1, "cloud");
    //先画一个底图



	// 定义KDTree对象
	pcl::search::KdTree<PointT>::Ptr kdtree(new pcl::search::KdTree<PointT>);
	kdtree->setInputCloud(cloud); // 设置要搜索的点云,建立KDTree

	std::vector<int> indices; // 存储查询近邻点索引
	std::vector<float> distances; // 存储近邻点对应距离的平方

	PointT point = cloud->points[0]; // 初始化一个查询点
	
	// 查询距point最近的k个点
	int k = 1000;
	int size = kdtree->nearestKSearch(point, k, indices, distances);

    pcl::PointCloud<pcl::PointXYZ>::Ptr cloudOut(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::copyPointCloud(*cloud, indices, *cloudOut);
	pcl::visualization::PointCloudColorHandlerCustom<PointT> red(cloudOut, 255, 0, 0); // rgb
	viewer.addPointCloud(cloudOut, red, "cloudOut");
    //按照索引单独给点云上色上色并叠加到点云上

	// 查询point半径为radius邻域球内的点
	std::vector<int> indices1; // 存储查询近邻点索引
	std::vector<float> distances1; // 存储近邻点对应距离的平方
	double radius = 30.0;
	int size1 = kdtree->radiusSearch(point, radius, indices1, distances1);

	std::cout << "search point : " << size1 << std::endl;

	viewer.spin();

	// 2. 非阻塞式
	while (!viewer.wasStopped())
	{
		viewer.spinOnce(100);
		boost::this_thread::sleep(boost::posix_time::microseconds(100000));
		// 可添加其他操作
	}

	system("pause");
    
	return 0;
}
相关推荐
为何创造硅基生物31 分钟前
C 语言 typedef 结构体私有化
c语言·开发语言·算法
yzx99101334 分钟前
递归算法入门:像俄罗斯套娃一样思考
人工智能·算法
心中有国也有家40 分钟前
从零上手 CANN 学习中心:像逛技术便利店一样学昇腾
学习·算法·开源
oo哦哦1 小时前
搜索矩阵系统的最短路密码:用Dijkstra算法和网络流理论,解释为什么你做了1000个关键词,流量还不如别人30个
网络·算法·矩阵
Matlab程序猿小助手1 小时前
【MATLAB源码-第319期】基于matlab的帝王蝶优化算法(MBO)无人机三维路径规划,输出做短路径图和适应度曲线.
开发语言·算法·matlab
图码1 小时前
二分查找进阶:如何在有序数组中快速找到Upper Bound?
数据结构·算法·面试·分类·柔性数组
试剂界的爱马仕1 小时前
《古董局·终局5:潮生》第 2 章:镜子的天赋
大数据·人工智能·算法
Cthy_hy1 小时前
树状数组(BIT)进阶:差分优化实现区间修改、区间查询
数据结构·python·算法
YsyaaabB2 小时前
ACM 模式通用代码模板
java·c++·python·算法
ComputerInBook2 小时前
Euclid 几何变换——仿射(affine)变换
算法·仿射变换·几何变换