image-processing - 将 320x240x3 点云矩阵转换为 320x240x1 深度图
问题描述
任何人都可以使用 Python 帮助我解决以下问题吗?
我有从虚拟相机获得的点云矩阵,它的尺寸是 320x240x3,表示每个点的 x、y、z 坐标(相机视图中的点)。
所有值的范围从负到正。如何将此点云矩阵转换为存储每个像素的正深度值的 320x240x1 深度图?提前致谢。
解决方案
好吧,如果您知道如何将深度图像转换为点图(即您的点云矩阵),我想您会反过来知道。
给定相关的相机内在函数,使用针孔相机模型,我们可以使用以下 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)
希望能帮助到你 :)
推荐阅读
- c# - 在哪里添加分组以获得准确的输出
- jsf - 如何在 JSF 视图中使用不是 ManagedBean 的类?
- reactjs - 如何在 Google Cloud Storage 存储桶中为 React 静态 Web 应用添加环境变量?
- c++ - QFileDialog,如何访问网络位置
- asp.net-mvc - 使用 @Html.CheckBoxFor 禁用多项选择,同时允许框之间有空格 - ASP.Net (Core) MVC
- android - 如何通过 Uri 链接打开 GooglePhoto 的视频/图像?
- javascript - Bootstrap 中的自适应徽标
- android - Flutter Provider (ChangeNotifierProxyProvider) 失败,因为 hasListeners 为 true
- python - 如何在 Tensorflow 2.0 中将占位符从 Tensorflow 1.0 转换为 tf.keras.input
- r - 从动态字符串向量中提取某些单词