将 XYZ 点云转换为灰度图像

Convert XYZ point cloud to grayscale image

大家 我正在尝试使用 python 将点云 (X, Y, Z) 转换为灰度图像。我了解到灰度图像可以由 Numpy 数组生成。但我现在拥有的是一组包含 X、Y 和高度的点。我想根据 X、Y 和灰度值(高度)生成灰度图像。

有人可以给我一些想法吗? 提前致谢。

罗文

让我们假设 X,Y 的排列使它们形成一个网格(这是构建矩形图像所必需的)。从那里这很容易:

import numpy as np
import matplotlib.pyplot as plt

# generate some data
ax = np.arange(-9, 10)
X, Y = np.meshgrid(ax, ax)
Z = X ** 2 + Y ** 2

# normalize the data and convert to uint8 (grayscale conventions)
zNorm = (Z - Z.min()) / (Z.max() - Z.min()) * 255
zNormUint8 = zNorm.astype(np.uint8)

# plot result
plt.figure()
plt.imshow(zNormUint8)

谢谢大家。我刚写完自己的代码来进行插值。但我的想法来自你。感谢@asaflotz 和@Paul Panzer。

问题是在我的场景中,点云中的点没有排列好。两个相邻点之间的间隔不均匀。直接用grid是不行的。所以我在 Scipy.Interpolate which has so many practical methods can be used depending on different use case. My code below is a modified version of the example from Scipy.Interpolate.griddata.

中选择了一个非结构化方法
x_range=((df.X.max()-df.X.min()))
y_range=((df.Y.max()-df.Y.min()))
grid_x, grid_y = np.mgrid[df.X.min():df.X.max():(x_range*1j), df.Y.min():df.Y.max():(y_range*1j)]
points = df[['X','Y']].values
values = df['new'].values
grid_z0 = griddata(points, values, (grid_x, grid_y), method='linear').astype(np.uint8)
im=Image.fromarray(grid_z0,'L')
im.show()

注意到在griddata中,可以根据您的场景应用'linear'、'nearest'、'cubic'等方法。 这是生成的灰度高程图像。

终于,我的问题基本解决了。如果您有任何好的想法或困惑,请对此post发表评论。谢谢大家!

罗文