首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >[python][pcl]python-pcl案例之kdtree使用

[python][pcl]python-pcl案例之kdtree使用

作者头像
云未归来
发布2025-07-19 15:03:29
发布2025-07-19 15:03:29
7600
代码可运行
举报
运行总次数:0
代码可运行

测试环境:

pcl==1.13.0

python-pcl==0.3.1

python==3.7

代码:

代码语言:javascript
代码运行次数:0
运行
复制
# -*- coding: utf-8 -*-
from __future__ import print_function

import numpy as np
import pcl


def main():
    points_1 = np.array([[0, 0, 0],
                         [1, 0, 0],
                         [0, 1, 0],
                         [1, 1, 0]], dtype=np.float32)
    points_2 = np.array([[0, 0, 0.2],
                         [1, 0, 0],
                         [0, 1, 0],
                         [1.1, 1, 0.5]], dtype=np.float32)

    pc_1 = pcl.PointCloud()
    pc_1.from_array(points_1)
    pc_2 = pcl.PointCloud()
    pc_2.from_array(points_2)
    kd = pcl.KdTreeFLANN(pc_1)

    print('pc_1:')
    print(points_1)
    print('\npc_2:')
    print(points_2)
    print('\n')

    pc_1 = pcl.PointCloud(points_1)
    pc_2 = pcl.PointCloud(points_2)
    kd = pc_1.make_kdtree_flann()

    # find the single closest points to each point in point cloud 2
    # (and the sqr distances)
    indices, sqr_distances = kd.nearest_k_search_for_cloud(pc_2, 1)
    for i in range(pc_1.size):
        print('index of the closest point in pc_1 to point %d in pc_2 is %d'
              % (i, indices[i, 0]))
        print('the squared distance between these two points is %f'
              % sqr_distances[i, 0])


if __name__ == "__main__":
    # import cProfile
    # cProfile.run('main()', sort='time')
    main()

输出:

pc_1: [[0. 0. 0.]  [1. 0. 0.]  [0. 1. 0.]  [1. 1. 0.]] pc_2: [[0.  0.  0.2]  [1.  0.  0. ]  [0.  1.  0. ]  [1.1 1.  0.5]] index of the closest point in pc_1 to point 0 in pc_2 is 0 the squared distance between these two points is 0.040000 index of the closest point in pc_1 to point 1 in pc_2 is 1 the squared distance between these two points is 0.000000 index of the closest point in pc_1 to point 2 in pc_2 is 2 the squared distance between these two points is 0.000000 index of the closest point in pc_1 to point 3 in pc_2 is 3 the squared distance between these two points is 0.260000

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-07-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档