如何在python中绘制XYZ数据点以创建RGB深度图像

问题描述

我正在做一个小项目,在这里我得到了XYZ数据的二维数组。这样的事情:

MyGrid

其中,x表示数组的列,y表示数组的行,z表示数组的内容,即距离。我要完成的工作是使用2d数组并在RGB中绘制深度图像。详细地说,距离(z)值越近,我希望将该点绘制为红色。随着距离(z)值的增加,我想根据距离的大小将其绘制为黄色,绿色或蓝色。

添加了示例图片以更好地进行说明。

Depth image

我正在尝试在python中完成此操作。我尝试对其进行研究,但是大多数时候我发现如何从深度图像或点云中提取XYZ数据,而不是使用XYZ数据绘制深度图像。

请让我知道这是否可行,或者可以使用哪些python库实现此目的。

谢谢。

编辑:我认为这可能有助于指出2D阵列由测距仪传感器生成的点云数据组成。我希望能够使用数据点构建扫描区域的静止2D图像。我还想利用颜色图,以便让我可视化图像的深度。

解决方法

下面是使用seaborn的示例:

生成示例数据:

def func(x,y):
    return np.exp(-x**2-y**2)

xaxis = np.linspace(-1,1,100)
yaxis = np.linspace(-1,200)
result = func(xaxis[:,None],yaxis[None,:])

绘图:

sns.heatmap(result,cmap=sns.color_palette("Spectral_r",as_cmap=True))
plt.yticks([],[])
plt.xticks([],[])

结果: enter image description here

,

与此类似吗?

from matplotlib.pylab import plt
a = [[1,7,13,3,4],[6,21,32,11,2]]
plt.matshow(a,cmap=plt.cm.viridis)
plt.colorbar()

enter image description here

您可以传递大阵列,例如图像。在这里,我使用来自matplotlib的示例图像,裁剪颜色以获得(120,560)而不是(120,560,3)数组,然后显示它:

from matplotlib.pylab import plt
from matplotlib.cbook import get_sample_data
fn = get_sample_data("logo2.png",asfileobj=False)
img = plt.imread(fn,format='png')[...,0] #get single color channel
plt.matshow(img,cmap=plt.cm.jet,interpolation='bicubic')#see imshow for more arguments
plt.colorbar()

enter image description here

如果我没记错的话,plt.matshow是plt.imshow的子类