测试环境:
pcl==1.12.1
python-pcl==0.3.1
python==3.7
代码:文章来源:https://www.toymoban.com/news/detail-513807.html
# -*- coding: utf-8 -*-
# http://pointclouds.org/documentation/tutorials/kdtree_search.php#kdtree-search
import numpy as np
import pcl
import random
def main():
# srand (time (NULL));
# pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
cloud = pcl.PointCloud()
# // 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);
# }
points = np.zeros((1000, 3), dtype=np.float32)
RAND_MAX = 1024
for i in range(0, 1000):
points[i][0] = 1024 * random.random() / (RAND_MAX + 1.0)
points[i][1] = 1024 * random.random() / (RAND_MAX + 1.0)
points[i][2] = 1024 * random.random() / (RAND_MAX + 1.0)
cloud.from_array(points)
# pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;
# kdtree.setInputCloud (cloud);
kdtree = cloud.make_kdtree_flann()
# 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);
searchPoint = pcl.PointCloud()
searchPoints = np.zeros((1, 3), dtype=np.float32)
searchPoints[0][0] = 1024 * random.random() / (RAND_MAX + 1.0)
searchPoints[0][1] = 1024 * random.random() / (RAND_MAX + 1.0)
searchPoints[0][2] = 1024 * random.random() / (RAND_MAX + 1.0)
searchPoint.from_array(searchPoints)
# // K nearest neighbor search
# int K = 10;
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;
# print ('K nearest neighbor search at (' + searchPoint[0][0] + ' ' + searchPoint[0][1] + ' ' + searchPoint[0][2] + ') with K=' + str(K))
print('K nearest neighbor search at (' + str(searchPoint[0][0]) + ' ' + str(
searchPoint[0][1]) + ' ' + str(searchPoint[0][2]) + ') with K=' + str(K))
# 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;
# }
[ind, sqdist] = kdtree.nearest_k_search_for_cloud(searchPoint, K)
# if nearest_k_search_for_cloud
for i in range(0, ind.size):
print('(' + str(cloud[ind[0][i]][0]) + ' ' + str(cloud[ind[0][i]][1]) + ' ' + str(
cloud[ind[0][i]][2]) + ' (squared distance: ' + str(sqdist[0][i]) + ')')
# 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;
radius = 256.0 * random.random() / (RAND_MAX + 1.0)
print('Neighbors within radius search at (' + str(searchPoint[0][0]) + ' ' + str(
searchPoint[0][1]) + ' ' + str(searchPoint[0][2]) + ') with radius=' + str(radius))
# 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;
# }
# NotImplement radiusSearch
[ind, sqdist] = kdtree.radius_search_for_cloud(searchPoint, radius)
for i in range(0, ind.size):
print('(' + str(cloud[ind[0][i]][0]) + ' ' + str(cloud[ind[0][i]][1]) + ' ' + str(
cloud[ind[0][i]][2]) + ' (squared distance: ' + str(sqdist[0][i]) + ')')
if __name__ == "__main__":
# import cProfile
# cProfile.run('main()', sort='time')
main()
运行结果:文章来源地址https://www.toymoban.com/news/detail-513807.html
K nearest neighbor search at (0.2606654763221741 0.5004734396934509 0.5399821996688843) with K=10
(0.2099999487400055 0.4809325039386749 0.507086455821991 (squared distance: 0.0040309737)
(0.2498379349708557 0.42376846075057983 0.5084726810455322 (squared distance: 0.0069937394)
(0.3286709487438202 0.5498905777931213 0.5591463446617126 (squared distance: 0.007434062)
(0.24990490078926086 0.48364347219467163 0.6444564461708069 (squared distance: 0.011313906)
(0.2469586580991745 0.42236608266830444 0.6174416542053223 (squared distance: 0.012288604)
(0.3753530979156494 0.49026110768318176 0.5504951477050781 (squared distance: 0.013368064)
(0.23193365335464478 0.6070741415023804 0.5041316151618958 (squared distance: 0.013474491)
(0.25095754861831665 0.4965510368347168 0.6574267148971558 (squared distance: 0.013902843)
(0.18482469022274017 0.5197768211364746 0.44867923855781555 (squared distance: 0.014460676)
(0.2100318819284439 0.5766696929931641 0.4454286992549896 (squared distance: 0.017309994)
Neighbors within radius search at (0.2606654763221741 0.5004734396934509 0.5399821996688843) with radius=0.20584062194897176
到了这里,关于[python][pcl]python-pcl案例之kdtree搜索的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!