从点云数据中找到最接近点 (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)
我制作了一个程序来根据点云数据绘制图形(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)