问题
Everyone I'm trying to convert point cloud (X, Y, Z) to the grayscale image using python. I learned that the grayscale image could be generated by a Numpy array. But what I have now is a set of points which contains X, Y and height. I wanna generate a grayscale image based on X, Y and grayscale value which is Height.
Can someone give me an idea about this? Thanks beforehand.
Rowen
回答1:
let's assume that the X,Y are arranged so they will form a grid (which is mandatory in order to build a rectangular image). from there this is easy:
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)
回答2:
Thanks, guys. I just finished writing my own codes to do interpolation. But my idea is from yours. Thank you to @asaflotz and @Paul Panzer.
The thing is in my scenario, points in point cloud are not arranged well. The intervals between two nearby points are not uniform. It's impossible to use grid directly. So I picked up an unstructured method in 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()
Noticed that in griddata, methods like 'linear', 'nearest', 'cubic' can be applied depending on your scenarios. Here is the grayscale elevation image generated.
Lastly, my question has been solved basically. Please comment on this post if you have any good ideas or confusion. Thanks all!
Rowen
来源:https://stackoverflow.com/questions/53700089/convert-xyz-point-cloud-to-grayscale-image