使用skimage.color.rgb2hsv时的一些注意事项

最近刚刚接触关于图像处理的一些东西,体会到了skimage库的强大,在这里记录下在使用过程中困扰过我的一些东西。一般在使用
img = skimage.data.astronaut()时其默认RGB的取值范围是0~255,数据类型是uint8。当我们使用skimage.color.rgb2hsv,期望将RGB转化为HSV时,有一些注意事项。
我使用的是v0.12版本的skimage,其中里面有一句话:

The conversion assumes an input data range of [0, 1] for all
color components.

我理解为它相当于先把0~255的RGB值转换为0~1,然后再进行处理。当我们输入的数据类型是uint8时没有任何问题,但当我们对图像数据进行处理后,例如不小心将其数据类型变成了float64时,就会出现一些问题。

from skimage import color
from skimage import data
img = data.astronaut()
img_hsv = color.rgb2hsv(img)
print(img_hsv[100,100,:])

输出结果为
这里写图片描述
利用官方例程,我们可以看出它默认最后输出的HSV的范围为0~1,但当我们改变数据类型

img = data.astronaut()
img_hsv = color.rgb2hsv(img.astype('float64'))
print(img_hsv[100,100,:])

输出结果为
这里写图片描述
我们可以看出HSV中V的值的范围变为0~255,通过查找源代码,发现代码里有一句相当于使用skimage.img_as_float的语句,其将我们输入的0~255的uint8的RGB转换为0~1的双精度值。

The range of a floating point image is [0.0, 1.0] or [-1.0, 1.0] when converting from unsigned or signed datatypes, respectively.

但当我们本来输入的就是float的数据,其便不会进行处理。返回的值不变,这就解释了为什么我们会得到V的范围为0~255。
所以最终处理办法就是我们将数据在处理前强制转换为uint8的类型,保证不会出错。

img_hsv = color.rgb2hsv(img.astype('uint8'))

刚刚接触这方面的知识,如果哪里理解错了,还望指正~

你可能感兴趣的:(使用skimage.color.rgb2hsv时的一些注意事项)