本文介绍了将320x240x3点云矩阵转换为320x240x1深度图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

can anybody help me with the following problem using Python?

我有一个从虚拟摄像机获取的点云矩阵,其尺寸为320x240x3,它指示每个点(摄像机视图中的点)的x,y,z坐标.

I have point cloud matrix got from a virtual camera, its dimension is 320x240x3, which indicates x,y,z coordinates of each point (points in camera view).

所有值从负到正.如何将该点云矩阵转换为存储每个像素的正深度值的320x240x1深度图?预先感谢.

All values range from negative to positive. How can I convert this point cloud matrix to a 320x240x1 depth map which stores the positive depth value of each pixel? Thanks in advance.

推荐答案

好吧,如果您知道如何将深度图像转换为点图(即点云矩阵),我想您是反之亦然.

Well, if you knew how to convert depth image to point map (that is your point cloud matrix), I guess you'll know the other way round.

使用针孔相机模型,我们可以使用以下python代码恢复点图:

Given the associated camera intrinsics, using pinhole camera model, we can recover the point map using the following python code:

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)


现在回到您的问题.


Now back to your question.

假设您的点图位于相机坐标系中 (您尚未平移或旋转 pmap ),即可将点图转换为深度我们将要做的图像:

Assuming your point map is in camera coordinate system (you haven't translated or rotated pmap), to convert point map to depth image we shall do:

# 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)

希望它会有所帮助:)

这篇关于将320x240x3点云矩阵转换为320x240x1深度图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 03:18