[python][pcl]python-pcl案例之kdtree搜索

测试环境:

pcl==1.12.1

python-pcl==0.3.1

python==3.7

代码:

# -*- 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()

运行结果:

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点云库(pclpy)是一个通过使用CppHeaderParser和pybind11从PCL标头生成的Python绑定。它提供了处理三维点云的功能,包括计算点特征直方图(VFH)和可视化点云数据。 要使用pclpy计算点云的VFH,您可以按照以下步骤进行操作: 1. 导入pclpy和numpy库。 2. 读取一个PLY格式的点云文件并将其存储在一个pclpy的PointCloud对象中。 3. 创建一个pclpy的NormalEstimation对象,并设置输入点云。 4. 创建一个pclpy的KdTree对象,并设置为法线向量估计对象的搜索方法。 5. 设置法线向量估计对象的K值(KSearch)。 6. 创建一个pclpy的PCLPointCloud2对象,并使用法线向量估计对象计算曲率。 7. 创建一个pclpy的VFHEstimation对象,并设置输入点云和法线向量。 8. 设置VFHEstimation对象的搜索方法。 9. 使用VFHEstimation对象计算VFH特征。 10. 将VFH特征转换为numpy数组格式,以便后续处理或可视化。 下面是一个示例代码,演示了如何使用pclpy计算点云的VFH: ```python import pclpy import numpy as np # 读取点云数据 cloud = pclpy.pcl.PointCloud.PointXYZ() cloud.from_file("sample.ply") # 计算法线向量 normals = pclpy.pcl.NormalEstimation.PointXYZ_Normal() normals.setInputCloud(cloud) kdtree = pclpy.pcl.search.KdTree.PointXYZ() normals.setSearchMethod(kdtree) normals.setKSearch(20) curvatures = pclpy.pcl.PCLPointCloud2() normals.compute(curvatures) # 计算VFH vfhsignature = pclpy.pcl.VFHEstimation.PointXYZ_Normal_VFHSignature308() vfhestimation = pclpy.pcl.VFHEstimation.PointXYZ_Normal_VFHSignature308() vfhestimation.setInputCloud(cloud) vfhestimation.setInputNormals(normals.getOutput()) vfhestimation.setSearchMethod(kdtree) vfhestimation.compute(vfhsignature) # 将VFH转化为numpy格式 vfhsignature_np = np.array(vfhsignature.data, dtype=np.float32) vfhsignature_np = vfhsignature_np.reshape((1, vfhsignature.size())) print(f"VFH Signature:\n{vfhsignature_np}") ``` 通过以上步骤,您可以使用pclpy计算点云的VFH特征,并将结果以numpy数组的形式打印出来。您可以根据自己的需求进一步处理或可视化这些特征。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农张三疯

你的打赏是我写文章最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值