簡體   English   中英

查找從點雲數據到點 (0,0,0) 的最近點

[英]Finding the closest point from Point Cloud Data to point (0,0,0)

我制作了一個程序來根據點雲數據繪制圖形(x、y、z 坐標顯示為列表 x_list、y_list、z_list)。 現在,我必須找到最接近 (0,0,0) 的點。 有人有這樣做的想法嗎? 這是程序:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import math
from mpl_toolkits.mplot3d import axes3d


cloud = np.loadtxt('c1.txt')
rows = len(cloud)
columns = int(len(cloud[0])/5)

x_list = []
y_list = []
z_list = []

for i in range(rows):
    for j in range(columns):
    x_list.append(cloud[i][j])
    y_list.append(cloud[i][j+columns])
    z_list.append(cloud[i][j+2*columns])

#x_list = x_list[~pd.isnull(x_list)]


X = x_list
Y = y_list
Z = z_list

#Eliminating 'nan' values 
newlist_x = [X for X in x_list if math.isnan(X) == False]
newlist_y = [Y for Y in y_list if math.isnan(Y) == False]
newlist_z = [Z for Z in z_list if math.isnan(Z) == False]


display(newlist_x, newlist_y, newlist_z)


fig = plt.figure()
ax = fig.add_subplot(projection='3d')

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')


ax.scatter(newlist_x, newlist_y, newlist_z, c=newlist_z, cmap='plasma', linewidth=0.01) 
#3D plotting of points
plt.rcParams["figure.figsize"] = (12,15) #making plot more viewable

plt.show()

這應該可以解決問題。

def closest(X, Y, Z, P):
    """
        X: x-axis series
        Y: y-axis series
        Z: z-axis series
        P: centre point to measure distance from
        
        Returns: tuple(closest point, distance between P and closest point)
    """ 
    # Here we combine the X, Y, Z series to get a data struct. with all our points (similar to using the builtin's zip())
    points = np.column_stack((X, Y, Z))
    # Now we compute the distances between each of the points and the desired point
    distances = [np.linalg.norm(p) for p in (points - P)]
    # Finally we get the index of the smalles value in the array so that we can determine which point is closest and return it
    idx_of_min = np.argmin(distances)
    return (points[idx_of_min], distances[idx_of_min])

您可以這樣調用closest的:

X = [-1, -6, 8, 1, -2, -5, -1, 5, -3, -10, 6, 3, -4, 9, -5, -2, 4, -1, 3, 0]
Y = [-2, -1, -9, -6, -8, 7, 9, -2, -9, -9, 0, -2, -2, -3, 6, -5, -1, 3, 8, -5]
Z = [-1, 2, 3, 2, 6, -2, 9, 3, -10, 4, -6, 9, 8, 3, 3, -6, 4, 1, -10, 1]
closest_to_point(X, Y, Z, (0, 0, -100))

示例輸出如下所示: (array([ 3, 8, -10]), 90.40464589831653)

暫無
暫無

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

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