使用 scikit-image 的数组转换:从整数到浮点数 [英] Array conversion using scikit-image: from integer to float
问题描述
我在使用 scikit-image 将整数图像转换为浮点图像时遇到了一些问题.
I am facing some kind of problem when converting an integer image to a float image using scikit-image.
这是一个例子(图像是一个 2 像素的图像):
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,:]
我预料到了:
[ 0. 0. 5.]
[ 0. 0. 5.]
[ 0. 0. 5.]
但我明白了:
[ 2.32830644e-10 2.32830644e-10 2.56113708e-09]
[ 0. 0. 0.01960784]
[ 0. 0. 5.]
从 float
到 int
损失精度是正常的,但是在这里我从 int
到 传递时丢失了真实信息>float
使用 img_as_float
.在阅读 GitHub...
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...
为什么会这样?
推荐答案
img_as_float()
不仅仅是类型转换,它将完整的无符号整数范围转换为 [0, 1],完整的有符号整数范围到 [-1, 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,dtype 为 int32,表示将 [-2147483648, 2147483647] 转换为 [-1, 1]
- i2,dtype 为 uint8,表示将 [0, 255] 转换为 [0, 1]
- i3,因为 dtype 已经是 float,所以什么都不做.
这篇关于使用 scikit-image 的数组转换:从整数到浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!