You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从3D点数组中匹配与给定x,y角点最邻近的点

从3D点数组中匹配最接近的预估角点(仅匹配X、Y坐标)

核心思路

仅对比X、Y坐标的距离,为每个预估角点找到points数组中距离最近的3D点,全程用Numpy实现,无额外依赖。

实现步骤

  1. 提取2D坐标子集:从3D点数组中分离出X、Y坐标,用于后续距离计算
  2. 计算距离矩阵:利用Numpy广播机制,批量计算每个预估角点与所有3D点的X、Y坐标距离
  3. 定位最近点索引:找出每个预估角点对应的最小距离的点索引
  4. 提取匹配的3D点:根据索引从原数组中获取完整的3D点数据

完整代码

import numpy as np

# 给定的3D点数组
points = np.array([[-1.33309284,-1.30808319,4.00199986],[-1.33209359,-1.30417236,3.99900007],[-1.33109427,-1.30026584,3.99600005],[-1.35235626,-1.30090892,4.00699997],[-1.34447409,-1.29896096,4.00099993],[-1.33627494,-1.29668835,3.99399996],[-1.35134384,-1.29700102,4.00400019],[-1.34346598,-1.29505737,3.99799991],[-1.33527122,-1.29278989,3.99099994],[-0.43118706,-1.29732492,4.00500011],[-0.42564261,-1.29829663,4.0079999,],[-1.35033125,-1.29309735,4.00099993],[-1.34245787,-1.29115818,3.99499989],[-1.33393295,-1.28857266,3.98699999],[-1.35809791,-1.28919816,3.99799991],[-1.35089223,-1.28790834,3.99399996],[-1.34470857,-1.2875859,3.99300003],[-1.36034349,-1.28562515,3.99600005],[-1.35381569,-1.28498166,3.99399996],[-1.34695627,-1.28401647,3.99099994]])

# 预估的2D角点数组
corner_points = np.array([[-1.33109423,-1.30026583],[-1.33527123,-1.29278983],[-1.35089222,-1.28790833],[-1.33393293,-1.28857263]])

# 1. 提取points的X、Y坐标
points_xy = points[:, :2]

# 2. 计算每个预估角点与所有points的X、Y平方距离(比欧氏距离计算更快,不影响最小值判断)
distances = np.sum((points_xy - corner_points[:, np.newaxis]) ** 2, axis=2)

# 3. 找到每个预估角点对应的最小距离的索引
closest_indices = np.argmin(distances, axis=1)

# 4. 获取匹配的3D点
closest_points = points[closest_indices]

# 输出结果
print("匹配到的3D点:")
print(closest_points)

结果说明

运行上述代码后,得到的closest_points即为每个预估角点对应的最匹配3D点:

[[-1.33109427 -1.30026584  3.99600005]
 [-1.33527122 -1.29278989  3.99099994]
 [-1.35089223 -1.28790834  3.99399996]
 [-1.33393295 -1.28857266  3.98699999]]

每个点的X、Y坐标与预估角点几乎完全一致,仅因浮点精度存在极小差异,符合需求。

内容的提问来源于stack exchange,提问作者Martin Pedersen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 03:15:48