pcl里面使用KdTree來搜索

from:https://blog.csdn.net/qq_25491201/article/details/51135054

下面這個教程我們將學會怎么用KdTree找一個特殊點附近的K個最近鄰,然后我們也將復習怎么通過一個特殊的半徑來找里面所有的近鄰。

一個k-d樹,或者k維的樹是一個計算機科學里面的數據結構。它是一個有其它約束影響的二叉搜索樹。K-d樹是在深度和最近鄰搜索里面很有用的。我們這次的目的是生成一個3維的k-d trees。一個k-d tree的每個層次在某個維度上分割成所有的子樹,使用一個垂直于相應坐標軸的高維平面。在樹的根部,所有的子樹將會被分割以第一維(如果第一維坐標系比根部少,它將會成為左子樹,如果比根部多,它將會成為右子樹)。每一層的樹將會在下一層進行分叉,它會跳轉到第一層如果全部都分完了。最有效的去建立k-d tree的方法是使用一個分割的方法,就像快速排序。你可以在你的左子樹和右子樹上重復這一過程,直到最后一個你要去分割的樹只有一個元素。

2維的k-d樹

下面是一個最近鄰搜索的工作

?

代碼

#include <pcl/point_cloud.h>
#include <pcl/kdtree/kdtree_flann.h>

#include <iostream>
#include <vector>
#include <ctime>

int
main (int argc, char** argv)
{
? 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 (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);
? }

? 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

? int K = 10;

? std::vector<int> pointIdxNKNSearch(K);
? std::vector<float> pointNKNSquaredDistance(K);

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

? if ( kdtree.nearestKSearch (searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance) > 0 )
? {
? ? for (size_t i = 0; i < pointIdxNKNSearch.size (); ++i)
? ? ? std::cout << " ? ?" ?<< ? cloud->points[ pointIdxNKNSearch[i] ].x?
? ? ? ? ? ? ? ? << " " << cloud->points[ pointIdxNKNSearch[i] ].y?
? ? ? ? ? ? ? ? << " " << cloud->points[ pointIdxNKNSearch[i] ].z?
? ? ? ? ? ? ? ? << " (squared distance: " << pointNKNSquaredDistance[i] << ")" << std::endl;
? }

? // Neighbors within radius search

? 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;


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


? return 0;
}
下面的代碼是創造了kdtree這個對象,并把我們隨機生成的點云作為輸入。

? 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);
我們接下去創造了一個整數(通常設置為10)和兩個向量存儲搜索后的K個最近鄰。

? // K nearest neighbor search

? int K = 10;

? std::vector<int> pointIdxNKNSearch(K);
? std::vector<float> pointNKNSquaredDistance(K);

? std::cout << "K nearest neighbor search at (" << searchPoint.x?
? ? ? ? ? ? << " " << searchPoint.y?
? ? ? ? ? ? << " " << searchPoint.z
? ? ? ? ? ? << ") with K=" << K << std::endl;
假設我們的kdtree返回了大于0個近鄰。那么它將打印出在我們"searchPoint"附近的10個最近的鄰居并把它們存到先前創立的向量中。

? if ( kdtree.nearestKSearch (searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance) > 0 )
? {
? ? for (size_t i = 0; i < pointIdxNKNSearch.size (); ++i)
? ? ? std::cout << " ? ?" ?<< ? cloud->points[ pointIdxNKNSearch[i] ].x?
? ? ? ? ? ? ? ? << " " << cloud->points[ pointIdxNKNSearch[i] ].y?
? ? ? ? ? ? ? ? << " " << cloud->points[ pointIdxNKNSearch[i] ].z?
? ? ? ? ? ? ? ? << " (squared distance: " << pointNKNSquaredDistance[i] << ")" << std::endl;
? }
?// Neighbors within radius search

? std::vector<int> pointIdxRadiusSearch;
? std::vector<float> pointRadiusSquaredDistance;

? float radius = 256.0f * rand () / (RAND_MAX + 1.0f);
我們把向量里面的點打印出來

? if ( kdtree.radiusSearch (searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0 )
? {
? ? for (size_t i = 0; i < pointIdxRadiusSearch.size (); ++i)
? ? ? std::cout << " ? ?" ?<< ? cloud->points[ pointIdxRadiusSearch[i] ].x?
? ? ? ? ? ? ? ? << " " << cloud->points[ pointIdxRadiusSearch[i] ].y?
? ? ? ? ? ? ? ? << " " << cloud->points[ pointIdxRadiusSearch[i] ].z?
? ? ? ? ? ? ? ? << " (squared distance: " << pointRadiusSquaredDistance[i] << ")" << std::endl;
? }
結果

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)
---------------------?
作者:Spongelady?
來源:CSDN?
原文:https://blog.csdn.net/qq_25491201/article/details/51135054?
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/458123.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/458123.shtml
英文地址,請注明出處:http://en.pswp.cn/news/458123.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Linux英文全稱

su&#xff1a;Swith user 切換用戶&#xff0c;切換到root用戶cat: Concatenate 串聯uname: Unix name 系統名稱df: Disk free 空余硬盤du: Disk usage 硬盤使用率chown: Change owner 改變所有者chgrp: Change group 改變用戶組ps&#xff1a;Process Status 進程狀態ta…

caffe caffe.cpp 程序入口分析

from&#xff1a;https://blog.csdn.net/u014114990/article/details/47747025 caffe.cpp 程序入口分析&#xff0c; &#xff08;1&#xff09;main()函數中&#xff0c;輸入的train&#xff0c;test&#xff0c;device_query&#xff0c;time。 通過下面兩行進入程序。 …

php文件加密

1.在線加密 網址&#xff1a;http://www.phpjm.net/encode.html 本人測試過還可以&#xff0c;就是純加密&#xff0c;沒有解密。 轉載于:https://www.cnblogs.com/wuheng1991/p/5332617.html

樹莓派3 編譯驅動

分為本地編譯和交叉編譯&#xff0c;主要是Makefile的寫法&#xff1a; 本地編譯&#xff1a; obj-m : bcm2835-i2s.o KDIR : /lib/modules/$(shell uname -r)/build PWD : $(shell pwd) all:make -C $(KDIR) M$(PWD) modules clean:rm *.o *.ko *.mod.c modules.order Module.…

caffe common 程序分析 類中定義類

caffe中 有 common.hpp 和common.cpp // The main singleton of Caffe class and encapsulates the boost and CUDA random number // generation function, providing a unified interface. caffe的singleton 類&#xff0c; 封裝boost和cuda等操作。 提供一個統一的接口&am…

相機標定究竟在標定什么?

https://mp.weixin.qq.com/s/sWpVgwXmPvIEbObXvo1HRg

SpringMVC+Shiro權限管理

SpringMVCShiro權限管理 什么是權限呢&#xff1f;舉個簡單的例子&#xff1a; 我有一個論壇&#xff0c;注冊的用戶分為normal用戶&#xff0c;manager用戶。對論壇的帖子的操作有這些&#xff1a;添加&#xff0c;刪除&#xff0c;更新&#xff0c;查看&#xff0c;回復我們規…

Caffe源碼解析1:Blob

from:https://www.cnblogs.com/louyihang-loves-baiyan/p/5149628.html 轉載請注明出處&#xff0c;樓燚(y)航的blog&#xff0c;http://www.cnblogs.com/louyihang-loves-baiyan/ 首先看到的是Blob這個類&#xff0c;Blob是作為Caffe中數據流通的一個基本類&#xff0c;網絡…

學后感

今天上了構建之法&#xff0c;我加深了對軟件工程的了解&#xff0c;也明白了單元測試和回歸測試對軟件開發的重要性&#xff0c;然而在軟件開發的過程中&#xff0c; 一個團隊是需要一定的流程來管理開發活動&#xff0c;每個工程師在軟件生命周期所做的工作也應該有一個流程&…

Caffe源碼解析2:SycedMem

from:https://www.cnblogs.com/louyihang-loves-baiyan/p/5150554.html 轉載請注明出處&#xff0c;樓燚(y)航的blog&#xff0c;http://www.cnblogs.com/louyihang loves baiyan/ 看到SyncedMem就知道&#xff0c;這是在做內存同步的操作。這類個類的代碼比較少&#xff0c;…

REST學習

RPC架構與REST架構 RPC&#xff1a;RPC將服務器看作一些列動作的集合(需要做某件事) REST&#xff1a;將服務器看作分布式對象集合&#xff0c;客戶端通過調用這些對象上的方法來執行特定的任務&#xff0c;組件交互的可伸縮性、接口的通用性、組件的獨立部署、以及用來減少交互…

HI3559A和AI深度學習框架caffe

from:http://blog.sina.com.cn/s/blog_156e567660102ygdf.html 1、HI3559A支持深度學習框架caffe。其中的NNIE神經網絡加速單元是主要的屬性。 2、caffe是一種快速深度學習框架和TensorFlow一樣是一組標準深度學習開源框架。 3、對應想嘗試AI深度學習的朋友可以按照網上的流…

UValive4195 Heroes of Money and Magic

斜率優化 想罵人了&#xff0c;馬格吉最后調了半小時 TMD造數據的人是SB吧&#xff1f; 我寫 while(scanf("%d%d",&n,&m)!EOF&&n) 然后就TMD無限WA...WA...WA... 尼瑪 改成while(scanf("%d%d",&n,&m),n) 就過了&#xff0c;就過了…

Google Protocol Buffer 的使用和原理

from: https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html 簡介 什么是 Google Protocol Buffer&#xff1f; 假如您在網上搜索&#xff0c;應該會得到類似這樣的文字介紹&#xff1a; Google Protocol Buffer( 簡稱 Protobuf) 是 Google 公司內部的混合語言…

Electron

跨平臺桌面app開發 Appjs hex nwjs electron 官網&#xff1a;http://electron.atom.io/ 中文文檔&#xff1a;https://github.com/atom/electron/tree/master/docs-translations/zh-CN zcbenz&#xff1a; https://github.com/zcbenz https://github.com/atom/electron simple…

WCF技術剖析之十八:消息契約(Message Contract)和基于消息契約的序列化

在本篇文章中&#xff0c;我們將討論WCF四大契約&#xff08;服務契約、數據契約、消息契約和錯誤契約&#xff09;之一的消息契約&#xff08;Message Contract&#xff09;。服務契約關注于對服務操作的描述&#xff0c;數據契約關注于對于數據結構和格式的描述&#xff0c;而…

【深度學習數據集】常用公開圖片數據集下載

1.MNIST MNIST是一個手寫數字數據庫&#xff0c;它有60000個訓練樣本集和10000個測試樣本集&#xff0c;每個樣本圖像的寬高為28*28。此數據集是以二進制存儲的&#xff0c;不能直接以圖像格式查看&#xff0c;不過很容易找到將其轉換成圖像格式的工具。 最早的深度卷積網絡Le…

常用的幾種卷積神經網絡介紹

常用的幾種卷積神經網絡介紹 標簽&#xff08;空格分隔&#xff09;&#xff1a; 深度學習 這是一篇基礎理論的博客&#xff0c;基本手法是抄、刪、改、查&#xff0c;畢竟介紹這幾個基礎網絡的博文也挺多的&#xff0c;就算是自己的一個筆記吧&#xff0c;以后忘了多看看。主…

計算客 (人人都有極客精神)爆力

人人公司是一家極為鼓舞極客精神的公司&#xff0c;當有重要的項目須要上線但又時間太緊。甚至須要當天上線的時候。往往會掛起海盜旗開啟電子日期顯示。讓大家能夠在對時間有更明白的感知的情況下&#xff0c;同心協力搞定重要的項目。海盜旗下方的電子屏顯示的日期形式為 YYY…

深度學習案例

1. neural-style&#xff1a;利用卷積神經網絡將一幅圖像的內容與另一幅圖像的風格相結合 https://github.com/jcjohnson/neural-style 2.Nerual Doodles&#xff1a;把 2 位的 Doodle 轉成精良的藝術品 https://github.com/alexjc/neural-doodle 3. srez&#xff1a;通過深度…