将图像从 float64 转换为 uint8 使图像看起来更暗

Convert image from float64 to uint8 makes the image look darker

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

Lossy conversion from float64 to uint8. Range [-0.9999998807907104, 0.9999175071716309]. Convert image to uint8 prior to saving to suppress this warning.

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

UserWarning: Possible precision loss when converting from float64 to uint8 .format(dtypeobj_in, dtypeobj_out))

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

这里是保存前转成uint8生成的一组图片

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

来自the documentation of skimage.img_as_ubyte that you linked

Negative input values will be clipped. Positive values are scaled between 0 and 255.

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

我通过使用

修复了这个警告
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)

就是这样!