首页 > 解决方案 > 将 320x240x3 点云矩阵转换为 320x240x1 深度图

问题描述

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

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

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

标签: image-processingcomputer-visionpoint-clouds

解决方案


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

给定相关的相机内在函数,使用针孔相机模型,我们可以使用以下 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)

希望能帮助到你 :)


推荐阅读