问题
I am facing some kind of problem when converting an integer image to a float image using scikit-image.
This is an example (the image is a 2 pixel image):
from numpy import array,uint8;
import skimage;
rgb = array([array([[0,0,0],[0,0,5]])])
i1 = skimage.img_as_float(rgb)#rgb.dtype ->dtype('int32')
i2 = skimage.img_as_float(rgb.astype(uint8))
i3 = skimage.img_as_float(rgb.astype(float))
print i1[0,1,:]
print i2[0,1,:]
print i3[0,1,:]
I expected this:
[ 0. 0. 5.]
[ 0. 0. 5.]
[ 0. 0. 5.]
But I got this:
[ 2.32830644e-10 2.32830644e-10 2.56113708e-09]
[ 0. 0. 0.01960784]
[ 0. 0. 5.]
It is normal to loss precision from float
to int
, but here I am losing the real information when passing from int
to float
using img_as_float
. I didn't found anything when reading the code on GitHub...
Why is this possible?
回答1:
img_as_float()
is not just type conversion, it convert full unsigned integer range to [0, 1], full signed integer range to [-1, 1].
- i1, the dtype is int32, means convert [-2147483648, 2147483647] to [-1, 1]
- i2, the dtype is uint8, means convert [0, 255] to [0, 1]
- i3, because the dtype is already float, do nothing.
来源:https://stackoverflow.com/questions/21429261/array-conversion-using-scikit-image-from-integer-to-float