Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert image from float64 to uint8 makes the image look darker

I have images of type float64 generated by GANs, and I save them through skimage.io.imsave. The process works well and the saved image looks nice, but I get a warning message as follows:

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

Then I try to get rid of this warning by convert images to uint8 before saving using function skimage.img_as_ubyte. This gives me a apparently much darker image with a warning

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

I've also tried to use other functions such as the one from tensorflow tf.image.convert_image_dtype before saving. They all return a darker image than I directly call skimage.io.imsave. What's the problem here?

Here's a set of images generated by converting to uint8 before saving enter image description here

Here's a set of images generated by saving directly

like image 290
Maybe Avatar asked Aug 22 '19 23:08

Maybe


2 Answers

From the documentation of skimage.img_as_ubyte that you linked:

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

Since your images are in the range [-1,1], half of the data will be set to 0, which is why stuff looks darker. Try first scaling your image to a positive-only range, for example by adding 1 to it, before calling skimage.img_as_ubyte.

like image 158
Cris Luengo Avatar answered Nov 17 '22 10:11

Cris Luengo


I fix this warning by using,

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)

That's it!

like image 8
Anh-Thi DINH Avatar answered Nov 17 '22 12:11

Anh-Thi DINH