【问题标题】:How to read a hdr image quickly in the RGBE format in Python?如何在 Python 中快速读取 RGBE 格式的 hdr 图像?
【发布时间】:2021-05-14 06:26:04
【问题描述】:

我想知道如何在 Python 中通过快速有效地获取 RGBE 格式的像素值来读取 HDR 图像 (.hdr)。

这些是我尝试过的:

    import imageio
    img = imageio.imread(hdr_path, format="HDR-FI")

或者:

    import cv2
    img = cv2.imread(hdr_path, flags=cv2.IMREAD_ANYDEPTH)

这会读取图像,但以 RGB 格式给出值。

如何在不改变 RGB 值的情况下获得第 4 个通道,即每个像素的“E”通道? 我更喜欢只涉及 imageio 的解决方案,因为我只能使用该模块。

【问题讨论】:

  • 两个问题:(a) 你有可以分享的示例图片,以便我可以测试一个完整的示例吗? (b) 你的img[0].dtype 是什么?
  • a) HDR 图像可以通过许多网站下载...(这是一个网站:all-free-download.com/free-photos/room-hdr.html); b) img[0].dtype = float32@FirefoxMetzger
  • 感谢您的澄清。 (a) 我要求提供图像,因为有时问题只存在于该特定图像,例如,由于图像的元数据。 (b) 由于您的图像已经是 32 位浮点,它应该已经是 RGBE 格式。是什么让你断定它不是?编辑:从技术上讲,它是 RGBE 的扩展版本,但这是一个实现细节。
  • 当我使用 imageio 或 cv2 读取任何 hdr 图像时,它给了我一个尺寸为 image_width x image_height x 3 的 numpy.ndarray,最里面的列表的长度都为 3,其中包含给定像素的各自 RGB 值,并且每个通道的值都从 0 及以上开始,因为像素中 [0,1] 范围内的 RGB 值已使用 E 通道以某种方式“指数化”。如果我手动解析 numpy 数组并将通道拆分为 E 通道,则需要花费太多时间......所以我需要一种直接的方法来快速获取 [0,1] 范围内的 E 通道和 RGB 值。 @FirefoxMetzger

标签: python cv2 python-imageio hdr


【解决方案1】:

如果您更喜欢 RGBE 表示而不是浮点表示,您可以在两者之间进行转换

def float_to_rgbe(image, *, channel_axis=-1):

    # ensure channel-last
    image = np.moveaxis(image, channel_axis, -1)

    max_float = np.max(image, axis=-1)
    
    scale, exponent = np.frexp(max_float)
    scale *= 256.0/max_float

    image_rgbe = np.empty((*image.shape[:-1], 4)
    image_rgbe[..., :3] = image * scale
    image_rgbe[..., -1] = exponent + 128

    image_rgbe[scale < 1e-32, :] = 0
    
    # restore original axis order
    image_rgbe = np.moveaxis(image_rgbe, -1, channel_axis)

    return image_rgbe

(注意:这是基于 RGBE 参考实现 (found here),如果它确实是瓶颈,可以进一步优化。)

在您的评论中,您提到“如果我手动解析 numpy 数组并将通道拆分为 E 通道,则需要花费太多时间......”,但如果没有看到代码。上面是O(height*width),对于像素级的图像处理方法来说似乎是合理的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多