如何在Python中以RGBE格式快速读取hdr图像?

2024-09-29 23:17:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道如何通过在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值的情况下,如何获得第四个通道,即每个像素的“E”通道? 我更喜欢只涉及imageio的解决方案,因为我仅限于使用该模块


Tags: path图像importformatimghdr格式rgb
1条回答
网友
1楼 · 发布于 2024-09-29 23:17:38

如果您更喜欢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(高度×宽度),这对于像素级图像处理方法来说似乎是合理的

相关问题 更多 >

    热门问题