简体   繁体   English

将 2D 深度图像/热图转换为 3D 高度场?

[英]Converting 2D depth image/heatmap to 3D height field?

I was wondering how to convert a 2D depth image/heatmap to a 3D height field.我想知道如何将 2D 深度图像/热图转换为 3D 高度场。 Where the Z values are the values of the 2D image.其中 Z 值是 2D 图像的值。

I have an RGB image:我有一个 RGB 图像:

在此处输入图像描述

I also have a corresponding depth image/heatmap:我也有相应的深度图像/热图:

在此处输入图像描述

I would like to combine them into a height field like this, where the Z values are the values of the depth image/heatmap:我想将它们组合成这样的高度场,其中 Z 值是深度图像/热图的值:

在此处输入图像描述

Except keeping the RGB values from the RGB image in the 3D heightfield.除了在 3D 高度场中保留来自 RGB 图像的 RGB 值。

What you are describing is essentially adding another dimension to the dataset.您所描述的本质上是向数据集添加另一个维度。 There are a few ways to achieve that, but note that some ways might be confusing to other people.有几种方法可以实现这一点,但请注意,有些方法可能会让其他人感到困惑。 The other program you are exporting to might also have strong expectations of the data's shape, etc, so check that.您要导出到的其他程序也可能对数据的形状等有强烈的期望,所以请检查一下。 What follows is just some thoughts about what you're trying to do...接下来是关于你正在尝试做的事情的一些想法......

Essentially, you're describing having 2 'attributes' of the data: the colour image (a 3-channel attribute), and the height field (a single 'channel').本质上,您描述的是具有 2 个数据“属性”:彩色图像(3 通道属性)和高度字段(单个“通道”)。 Some options:一些选项:

  • Keep them as 2 separate ndarrays.将它们保留为 2 个单独的 ndarray。 That is, a 3D array for the image and a 2D array for the height field.也就是说,图像的 3D 数组和高度场的 2D 数组。 Name them appropriately and save them in a sensible file structure.适当地命名它们并将它们保存在合理的文件结构中。 If I was in a hurry, this is what I would do (basically, do nothing).如果我赶时间,这就是我会做的(基本上,什么都不做)。
  • Put them both into an HDF5 file using h5py .使用h5py将它们都放入 HDF5 文件中。 This format is essentially like a little file system, with nested 'folders', and it allows for an arbitrary amount of documentation and metadata.这种格式本质上就像一个带有嵌套“文件夹”的小文件系统,它允许任意数量的文档和元数据。 It also provides compression, arbitrary array access, and other useful things.它还提供压缩、任意数组访问和其他有用的功能。 So it's great for organizing lots of (or two) ndarrays, especially if they are large.因此,它非常适合组织大量(或两个)ndarray,尤其是在它们很大的情况下。
  • Use xarray .使用xarray This is basically n-dimensional pandas and would let you store the two arrays in a single data structure.这基本上是 n 维pandas并且可以让您将两个 arrays 存储在单个数据结构中。 It's not ideal for this use case, but it does at least let you label the axes, so you can be clear about what the 4 'channels' are.对于这个用例来说它并不理想,但它至少让你 label 轴,所以你可以清楚 4 个“通道”是什么。
  • Make a Python class for your data.为您的数据制作 Python class This way you can document exactly what the two arrays are, and write methods to do things like plot them together.这样,您可以准确记录两个 arrays 是什么,并编写方法将它们一起执行 plot 之类的事情。 Class instances can be pickled and saved to disk, or you can write methods to save them in whatever way you like (as HDF5, for example). Class 实例可以腌制并保存到磁盘,或者您可以编写方法以您喜欢的任何方式保存它们(例如 HDF5)。 If I was going to be doing a lot of this and needed a robust, sustainable solution, this is what I would do.如果我要做很多这样的事情并且需要一个强大的、可持续的解决方案,这就是我会做的。

Here's how the class might look:以下是 class 的外观:

class BumpImage():
    """
    An image with a 3d height field attached.
    """
    def __init__(self, photo, height):
        if photo.ndim == 2:
            # Make greyscale image into RGB.
            photo = np.repeat(photo[:, :, None], 3, axis=-1)
        self.photo = photo
        if height.shape == photo.shape[:2]:
            self.height = height
        else:
            raise TypeError("Height must be 2d array")

    def size(self):
        return self.height.shape

    def plot(self, ax=None):
        # Make a 3d plot with matplotlib or whatever.
        return ax

    def save(self, fname):
        # Make HDF5 file called fname.
        return

Things I would not do:我不会做的事情:

  • Stack the height channel onto the other 3 channels in NumPy, to make a single 4-channel 'image'.将高度通道堆叠到 NumPy 中的其他 3 个通道上,以制作单个 4 通道“图像”。 I would say this is a bad idea, since there's no convenient way to document what you did in NumPy.我想说这是个坏主意,因为没有方便的方法来记录您在 NumPy 中所做的事情。
  • Try to render them together in some way, eg as a plot with the height field expressed as hillshade or in a 3D view, etc. It will be very hard to separate the two datasets again.尝试以某种方式将它们一起渲染,例如 plot,高度场表示为山体阴影或 3D 视图等。再次分离两个数据集将非常困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM