将 320x240x3 点云矩阵转换为 320x240x1 深度图

Converting 320x240x3 point cloud matrix to 320x240x1 depth map

有人可以使用 Python 帮助我解决以下问题吗?

我有一个虚拟相机的点云矩阵,它的尺寸是320x240x3,表示每个点(相机视图中的点)的x,y,z坐标。

所有值的范围都是从负到正。如何将此点云矩阵转换为存储每个像素的正深度值的 320x240x1 深度图?提前致谢。

好吧,如果您知道如何将深度图像转换为点图(即您的点云矩阵),我想您也会知道相反的方法。

鉴于关联的 camera intrinsics, using pinhole camera model,我们可以使用以下 python 代码恢复点图:

import numpy as np  
from imageio import imread, imwrite


# read depth image in
img = imread('depth.png')  
img = np.asarray(img)

# camera intrinsics, for demonstration purposes only. change to your own.
fx, fy = 570.0, 570.0
cx, cy = 320, 240 

# suppose your depth image is scaled by a factor of 1000
z = img.astype(float) / 1000.0  

# convert depth image of shape to point map 
# here we assume depth image is of shape (480, 640)
px, py = np.meshgrid(np.arange(640), np.arange(480))  # pixel_x, pixel_y
px, py = px.astype(float), py.astype(float)
x = ((px - cx) / fx) * z 
y = ((py - cy) / fy) * z 
pmap = np.concatenate([i[..., np.newaxis] for i in (x, y, z)], axis=-1)

现在回到你的问题。

假设你的点图在相机坐标系中(你没有平移或旋转pmap),到将点图转换为深度图我们要做的是:

# convert point map to depth image
# that is, we are only using the points' z coordinates values
depth = (pmap[:, :, 2] * 1000).astype(np.uint16)
# now we can save the depth image to disk
imwrite('output.png', depth)

希望对您有所帮助:)