【问题标题】:How to save a full size image with RGB and float32?如何使用 RGB 和 float32 保存全尺寸图像?
【发布时间】:2021-08-14 00:34:43
【问题描述】:

我有一个图像数组,其 RGB、np.float32 和在0 ... 1 范围内的值。

当我将数组与255 相乘并使用cv2.imwrite 时,输出不正确。当我使用plt.imshow(img.astype(np.float32))时,输出是对的,但是图像尺寸太小了。

plt.imshow(img.astype(np.float32))
plt.axis("off")
#plt.show()
#io.imsave("233.jpg", img)
plt.savefig('233.jpg')

怎样才能输出好图?

【问题讨论】:

    标签: python image opencv matplotlib rgb


    【解决方案1】:

    常见的JPEG 格式不支持存储 32 位浮点值。即使是某种常见的JPEG 2000 格式也无法做到这一点。您可能会看一下OpenEXR 格式,OpenCV 也支持这种格式,并且每个通道可以存储 32 位浮点值。不过,总体而言,OpenEXR 似乎不太受支持。

    这是一些小例子,也与一些 JPEG 导出比较:

    import cv2
    from matplotlib import pyplot as plt
    import numpy as np
    
    # Generate image
    x = np.linspace(0, 1, 1024, dtype=np.float32)
    x, y = np.meshgrid(x, x)
    img = np.stack([x, y, np.fliplr(x)], axis=2)
    
    # Save image as JPG and EXR
    cv2.imwrite('img.jpg', img * 255)
    cv2.imwrite('img.exr', img)
    
    # Read JPG and EXR images
    jpg = cv2.imread('img.jpg', cv2.IMREAD_UNCHANGED)
    exr = cv2.imread('img.exr', cv2.IMREAD_UNCHANGED)
    
    # Compare original with EXR image
    print('Original image shape and type: ', img.shape, img.dtype)
    print('EXR image shape and type: ', exr.shape, exr.dtype)
    print('Original image == EXR image:', np.all(img == exr))
    # Original image shape and type:  (1024, 1024, 3) float32
    # EXR image shape and type:  (1024, 1024, 3) float32
    # Original image == EXR image: True
    
    # Show all images
    plt.figure(1, figsize=(18, 9))
    plt.subplot(2, 3, 1), plt.imshow(img), plt.title('Original')
    plt.subplot(2, 3, 2), plt.imshow(jpg), plt.title('JPG image')
    plt.subplot(2, 3, 3), plt.imshow(exr), plt.title('EXR image')
    plt.subplot(2, 3, 4), plt.imshow(img[200:300, 500:600, ...])
    plt.subplot(2, 3, 5), plt.imshow(jpg[200:300, 500:600, ...])
    plt.subplot(2, 3, 6), plt.imshow(exr[200:300, 500:600, ...])
    plt.tight_layout(), plt.show()
    

    剧情是这样的:

    我不确定,在比较 JPEG 导出和 Matplotlib 图时,您看到了什么差异,但对于给定的示例,从我的角度来看,原始图像、JPG 图像和 EXR 图像之间的差异几乎没有见过。当然,你可以在缩放时看到 JPG 图片中的一些块,但这真的“伪造”了“图片”吗?

    ----------------------------------------
    System information
    ----------------------------------------
    Platform:      Windows-10-10.0.19041-SP0
    Python:        3.9.1
    PyCharm:       2021.1.1
    Matplotlib:    3.4.1
    NumPy:         1.20.2
    OpenCV:        4.5.1
    ----------------------------------------
    

    【讨论】:

    • 嗨,我终于找到了我的问题。我使用 [skimage.color.hsv2rgb] 创建我的图像。但是,我假设的程序输出是 bgr,与需要的 [plt.imshow] 输入相同,而 [cv2.imwrite] 输入是 rgb。所以我改变了我的图像数组,然后使用 cv2 输出正确的图像
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    相关资源
    最近更新 更多