【问题标题】:Convert image from float64 to uint8 makes the image look darker将图像从 float64 转换为 uint8 使图像看起来更暗
【发布时间】:2019-12-28 07:22:23
【问题描述】:

我有 GAN 生成的 float64 类型的图像,我通过skimage.io.imsave 保存它们。该过程运行良好,保存的图像看起来不错,但我收到如下警告消息:

从 float64 到 uint8 的有损转换。范围 [-0.9999998807907104, 0.9999175071716309]。在保存之前将图像转换为 uint8 以抑制此警告。

然后我尝试通过在使用函数skimage.img_as_ubyte 保存之前将图像转换为 uint8 来消除此警告。这给了我一个明显更暗的图像并带有警告

用户警告:从 float64 转换为 uint8 时可能会丢失精度 .format(dtypeobj_in, dtypeobj_out))

我还尝试在保存之前使用其他功能,例如来自 tensorflow tf.image.convert_image_dtype 的功能。它们都返回比我直接调用 skimage.io.imsave 更暗的图像。这里有什么问题?

这是一组在保存前转换为 uint8 生成的图像

这里是直接保存生成的一组图片

【问题讨论】:

    标签: python image tensorflow image-processing scikit-image


    【解决方案1】:

    来自the documentation of skimage.img_as_ubyte that you linked

    负输入值将被剪裁。正值在 0 到 255 之间缩放。

    由于您的图像在 [-1,1] 范围内,一半的数据将设置为 0,这就是为什么东西看起来更暗的原因。在调用skimage.img_as_ubyte 之前,先尝试将图像缩放到仅正数范围,例如将其加1。

    【讨论】:

      【解决方案2】:

      我通过使用来修复此警告,

      import numpy as np
      import imageio
      
      # suppose that img's dtype is 'float64'
      img_uint8 = img.astype(np.uint8)
      # and then
      imageio.imwrite('filename.jpg', img_uint8)
      

      就是这样!

      【讨论】:

        猜你喜欢
        • 2019-11-14
        • 1970-01-01
        • 2016-06-19
        • 2019-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-22
        • 1970-01-01
        相关资源
        最近更新 更多