赞
踩
最近更新时间:20230511
在本教程中,我们将介绍如何使用 KdTree 查找特定点或位置的 K 个最近邻居,然后我们还将介绍如何查找用户指定的某个半径内的所有邻居(在本例中为半径的大小随机)
k-d 树或 k 维树是计算机科学中用于组织 k 维空间中的一些点的数据结构。它是一个带有其他约束的二叉搜索树。 K-d 树对于范围和最近邻搜索非常有用。出于我们的目的,我们通常只处理三维点云,因此我们所有的 k-d 树都是三维的。k-d 树的每一层使用垂直于相应轴的超平面沿特定维度拆分所有子节点。在树的根部,所有子节点将根据第一维进行拆分(即,如果第一维坐标小于根,则它将位于左子树中,如果大于根,则显然位于右子树中)。树中的每一层都在下一个维度上划分,一旦所有其他维度都用完就返回到第一个维度。构建 k-d 树最有效的方法是使用分区方法,就像快速排序使用的分区方法一样,将中值点放在根部,所有具有较小一维值的东西都放在左边,较大的东西放在右边。然后在左右子树上重复此过程,直到要划分的最后一棵树仅由一个元素组成。
下图是一个2维k-d tree的例子:
下图是最近邻搜索工作过程的简图:
创建一个名为kdtree_search.cpp
的文件,然后将下面的内容复制到其中:
#include <pcl/point_cloud.h> #include <pcl/kdtree/kdtree_flann.h> #include <iostream> #include <vector> #include <ctime> int main () { /* 以下代码首先使用系统时间为 rand() 播种, 然后使用随机数据创建并填充 PointCloud。 **/ srand (time (NULL)); pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>); // Generate pointcloud data cloud->width = 1000; cloud->height = 1; cloud->points.resize (cloud->width * cloud->height); for (std::size_t i = 0; i < cloud->size (); ++i) { (*cloud)[i].x = 1024.0f * rand () / (RAND_MAX + 1.0f); (*cloud)[i].y = 1024.0f * rand () / (RAND_MAX + 1.0f); (*cloud)[i].z = 1024.0f * rand () / (RAND_MAX + 1.0f); } /* 下一段代码创建我们的 kdtree 对象并将我们随机创建的云设置为输入。 然后我们创建一个分配了随机坐标的“searchPoint”。 */ pcl::KdTreeFLANN<pcl::PointXYZ> kdtree; kdtree.setInputCloud (cloud); pcl::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 nearest neighbor search /* 现在我们创建一个整数(并将其设置为 10)和两个向量来存储我们搜索的 K 个最近的邻居。 */ int K = 10; std::vector<int> pointIdxKNNSearch(K); std::vector<float> pointKNNSquaredDistance(K); std::cout << "K nearest neighbor search at (" << searchPoint.x << " " << searchPoint.y << " " << searchPoint.z << ") with K=" << K << std::endl; /* 假设我们的 KdTree 返回超过 0 个最近的邻居, 然后它会打印出所有 10 个到我们的随机“搜索点”的最近邻居的位置, 这些搜索到的最近邻点存储在我们之前创建的向量中。 */ if ( kdtree.nearestKSearch (searchPoint, K, pointIdxKNNSearch, pointKNNSquaredDistance) > 0 ) { for (std::size_t i = 0; i < pointIdxKNNSearch.size (); ++i) std::cout << " " << (*cloud)[ pointIdxKNNSearch[i] ].x << " " << (*cloud)[ pointIdxKNNSearch[i] ].y << " " << (*cloud)[ pointIdxKNNSearch[i] ].z << " (squared distance: " << pointKNNSquaredDistance[i] << ")" << std::endl; } // Neighbors within radius search /* 现在我们的代码演示了在某个(随机生成的)半径内找到给定“searchPoint”的所有邻居。 再次创建 2 个向量来存储关于我们邻居的信息。 */ std::vector<int> pointIdxRadiusSearch; std::vector<float> pointRadiusSquaredDistance; float radius = 256.0f * rand () / (RAND_MAX + 1.0f); std::cout << "Neighbors within radius search at (" << searchPoint.x << " " << searchPoint.y << " " << searchPoint.z << ") with radius=" << radius << std::endl; /* 同样,和以前一样,如果我们的 KdTree 在指定半径内返回超过 0 个邻居, 它会打印出这些点的坐标,这些点已存储在我们前面定义的向量中。 */ if ( kdtree.radiusSearch (searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0 ) { for (std::size_t i = 0; i < pointIdxRadiusSearch.size (); ++i) std::cout << " " << (*cloud)[ pointIdxRadiusSearch[i] ].x << " " << (*cloud)[ pointIdxRadiusSearch[i] ].y << " " << (*cloud)[ pointIdxRadiusSearch[i] ].z << " (squared distance: " << pointRadiusSquaredDistance[i] << ")" << std::endl; } return 0; }
将以下行添加到您的 CMakeLists.txt 文件中:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(kdtree_search)
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable (kdtree_search kdtree_search.cpp)
target_link_libraries (kdtree_search ${PCL_LIBRARIES})
生成可执行文件后,您可以运行它。简单地做:
./kdtree_search
一旦你运行它,你应该看到类似这样的东西:
K nearest neighbor search at (455.807 417.256 406.502) with K=10 494.728 371.875 351.687 (squared distance: 6578.99) 506.066 420.079 478.278 (squared distance: 7685.67) 368.546 427.623 416.388 (squared distance: 7819.75) 474.832 383.041 323.293 (squared distance: 8456.34) 470.992 334.084 468.459 (squared distance: 10986.9) 560.884 417.637 364.518 (squared distance: 12803.8) 466.703 475.716 306.269 (squared distance: 13582.9) 456.907 336.035 304.529 (squared distance: 16996.7) 452.288 387.943 279.481 (squared distance: 17005.9) 476.642 410.422 268.057 (squared distance: 19647.9) Neighbors within radius search at (455.807 417.256 406.502) with radius=225.932 494.728 371.875 351.687 (squared distance: 6578.99) 506.066 420.079 478.278 (squared distance: 7685.67) 368.546 427.623 416.388 (squared distance: 7819.75) 474.832 383.041 323.293 (squared distance: 8456.34) 470.992 334.084 468.459 (squared distance: 10986.9) 560.884 417.637 364.518 (squared distance: 12803.8) 466.703 475.716 306.269 (squared distance: 13582.9) 456.907 336.035 304.529 (squared distance: 16996.7) 452.288 387.943 279.481 (squared distance: 17005.9) 476.642 410.422 268.057 (squared distance: 19647.9) 499.429 541.532 351.35 (squared distance: 20389) 574.418 452.961 334.7 (squared distance: 20498.9) 336.785 391.057 488.71 (squared distance: 21611) 319.765 406.187 350.955 (squared distance: 21715.6) 528.89 289.583 378.979 (squared distance: 22399.1) 504.509 459.609 541.732 (squared distance: 22452.8) 539.854 349.333 300.395 (squared distance: 22936.3) 548.51 458.035 292.812 (squared distance: 23182.1) 546.284 426.67 535.989 (squared distance: 25041.6) 577.058 390.276 508.597 (squared distance: 25853.1) 543.16 458.727 276.859 (squared distance: 26157.5) 613.997 387.397 443.207 (squared distance: 27262.7) 608.235 467.363 327.264 (squared distance: 32023.6) 506.842 591.736 391.923 (squared distance: 33260.3) 529.842 475.715 241.532 (squared distance: 36113.7) 485.822 322.623 244.347 (squared distance: 36150.5) 362.036 318.014 269.201 (squared distance: 37493.6) 493.806 600.083 462.742 (squared distance: 38032.3) 392.315 368.085 585.37 (squared distance: 38442.9) 303.826 428.659 533.642 (squared distance: 39392.8) 616.492 424.551 289.524 (squared distance: 39556.8) 320.563 333.216 278.242 (squared distance: 41804.5) 646.599 502.256 424.46 (squared distance: 43948.8) 556.202 325.013 568.252 (squared distance: 44751) 291.27 497.352 515.938 (squared distance: 45463.9) 286.483 322.401 495.377 (squared distance: 45567.2) 367.288 550.421 550.551 (squared distance: 46318.6) 595.122 582.77 394.894 (squared distance: 46938.1) 256.784 499.401 379.931 (squared distance: 47064.1) 430.782 230.854 293.829 (squared distance: 48067.2) 261.051 486.593 329.854 (squared distance: 48612.7) 602.061 327.892 545.269 (squared distance: 48632.4) 347.074 610.994 395.622 (squared distance: 49475.6) 482.876 284.894 583.888 (squared distance: 49718.6) 356.962 247.285 514.959 (squared distance: 50423.7) 282.065 509.488 516.216 (squared distance: 50730.4)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。