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;
}
相关推荐
人道领域10 分钟前
【LeetCode刷题日记】47.全排列Ⅱ
java·开发语言·算法·leetcode
漂流瓶jz12 分钟前
UVA-1606 两亲性分子 题解答案代码 算法竞赛入门经典第二版
数据结构·算法·向量·aoapc·算法竞赛入门经典·atan2·浮点
Navigator_Z14 分钟前
LeetCode //C - 1095. Find in Mountain Array
c语言·算法·leetcode
不会就选b41 分钟前
算法日常・每日刷题--<二分查找>1
算法
「維他檸檬茶」1 小时前
大模型算法学习2026.6.13
学习·算法
叫我:松哥1 小时前
基于Python的共享单车租赁数据分析与预测系统,技术栈flask+boostrap+随机森林+XGBoost
人工智能·python·深度学习·算法·随机森林·数据分析·flask
BAGAE1 小时前
星链卫星数据获取:从太空安全到实时通信的技术革命
网络·数据结构·数据库·算法·云计算·hbase
happymaker06261 小时前
LeetCodeHor100——438.找到字符串中所有的字母异位词
算法
西安邮电大学1 小时前
有关栈的经典算法题
java·后端·其他·算法·面试
h_a_o777oah1 小时前
【算法专项】扩展域并查集:原理详解及解决大部分种类并查集问题(洛谷P5937 P2024 C++代码)
数据结构·c++·算法·acm·并查集·扩展域·逻辑建模