簡體   English   中英

從 x,y 坐標查找最近的 x,y,z 點

[英]Finding closest x,y,z points from x,y coordinates

我有一組稱為點的 3D 點。 這是該數組的前 20 個點:

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]])

我有四個 x,y 坐標,它們代表估計的角點,但由於分辨率,它們不一定存在於點數組中。

corner_points = np.array([[-1.33109423,-1.30026583],[-1.33527123,-1.29278983],[-1.35089222,-1.28790833],[-1.33393293,-1.28857263]])

我想在點數組中找到與角點的 x,y 坐標最接近的四個點。

注意:有人提到 pandas 可能是更強大的任務工具,或者我假設來自 scikit 的球樹搜索。 我不僅限於使用 numpy。 這很實用,因為我已經在腳本中使用了它。

首先,我同意 Jérôme Richard 的觀點,即假設 2D/3D 點之間相等並不是一個好習慣(通常,人們會“取消投影”2D 點來做類似的事情)。

這是使用 numpy 的方法。 numpy 的關系/映射方面不如 pandas 好,因此如果您想將依賴關系擴展為更相關的庫,例如它(Pandas 在幕后使用 Z55F00E1DAA52B7CB79C5BC865E),可能會有其他方法。

目前,我想不出只有在兩個條件都為真(沒有 pandas)時才有條件地將 mask_2d 數組縮減為 1d 掩碼的方法,但我確信有辦法。 其他任何人,請隨時擴展此答案。

import numpy as np

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]])

# corner_points: removed [-1.26,0.48,3.5999999999999996], and fixed too many ] 
corner_points = np.array([-1.33109427,-1.30026584],[-1.33527122,-1.29278989],,[-1.33393295,-1.28857266],[-1.36034349,-1.28562515]]) 

mask_2d = np.isin(points, corner_points)
""" 
Out[92]: 
array(
       [[False, False, False],
       [False, False, False],
       [ True,  True, False], ....
"""

mask_1d = mask[:,0] # Note that this assumes the first and second value  are equal. Possible hole in code.
"""
Out[93]: 
array([False, False,  True, False, False, False, False, False,  True,
   False, False, False, False,  True, False, False, False,  True,
   False, False])
"""

points[mask_1d]
"""
Out[94]: 
array([[-1.33109427, -1.30026584,  3.99600005],
       [-1.33527122, -1.29278989,  3.99099994],
       [-1.33393295, -1.28857266,  3.98699999],
       [-1.36034349, -1.28562515,  3.99600005]])
"""

我在這里找到了答案: Finding index of nearest point in numpy arrays of x and y 坐標

並進行了一些編輯以使其適合我的問題。

import scipy
outer_points = [[1.44, 0.48, 0.600* 6],[-1.26, -1.02, 0.600* 6],[-1.26, 0.48, 0.600* 6],[1.44, -1.02, 0.600* 6]]
mytree = scipy.spatial.cKDTree(np.asarray(point_cloud.points))
dist, indexes = mytree.query(outer_points)
points_with_height = [np.asarray(point_cloud.points)[idx] for idx in indexes]

我需要角點的高度來做一些三角學,所以當我知道這四個點時,我必須弄清楚左上角、右角和左下角、右角。

top_left = [0,0,0]
top_right = [0,0,0]
bottom_left = [0,0,0]
bottom_right = [0,0,0]
for point in points_with_height:
    if point[0] < top_left[0] and point[1] > top_left[1]:
        top_left = point
    if point[0] > top_left[0] and point[1] > top_right[1]:
        top_right = point
    if point[0] < bottom_left[0] and point[1] < bottom_left[1]:
        bottom_left = point
    if point[0] > bottom_right[0] and point[1] < bottom_right[1]:
        bottom_right = point

我不認為它很漂亮,但它確實有效。 因此,如果有人有更優雅的方式來執行此操作,請隨時編輯或評論。 感謝所有幫助,為我指明了正確的方向。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM