为什么skimage表示过滤器不适用于浮点数组? [英] Why skimage mean filter does not work on float array?
问题描述
例如,我将对具有window_size=3
的float数组应用均值过滤器.我找到了这个图书馆:
I am going to apply a mean filter on an array of float with window_size=3
for example. I have found this library:
from skimage.filters.rank import mean
import numpy as np
x=np.array([[1,8,10],
[5,2,9],
[7,2,9],
[4,7,10],
[6,14,10]])
print(x)
print(mean(x, square(3)))
[[ 1 8 10]
[ 5 2 9]
[ 7 2 9]
[ 4 7 10]
[ 6 14 10]]
[[ 4 5 7]
[ 4 5 6]
[ 4 6 6]
[ 6 7 8]
[ 7 8 10]]
但是此函数不能在浮点数组上运行:
but this function can't run on float arrays:
from skimage.filters.rank import mean
import numpy as np
x=np.array([[1,8,10],
[5,2,9],
[7,2,9],
[4,7,10],
[6,14,10]])
print(x)
print(mean(x.astype(float), square(3)))
File "/home/pd/RSEnv/lib/python3.5/site-packages/skimage/util/dtype.py", line 236, in convert
raise ValueError("Images of type float must be between -1 and 1.")
ValueError: Images of type float must be between -1 and 1.
如何解决这个问题?
推荐答案
通常(这对其他编程语言有效)通常可以通过两种方式表示图像:
In general (and this is valid for other programming languages), an image can be typically represented in 2 ways:
- 强度值在
[0, 255]
范围内.在这种情况下,值的类型为uint8
-无符号整数8字节. - 强度值在
[0, 1]
范围内.在这种情况下,值的类型为float
.
- with intensity values in the range
[0, 255]
. In this case the values are of typeuint8
- unsigned integer 8-bytes. - with intensity values in the range
[0, 1]
. In this case the values are of typefloat
.
根据语言和库的不同,像素强度允许的值的类型和范围或多或少是允许的.
Depending on the language and library, the types and range of values allowed for the pixels' intensity can be more or less permissive.
这里的错误告诉您图像的像素值(您的array
的类型为float
,但它们不在[-1, 1]
范围内.由于这些值介于[0, 255]
之间,因此您只需将它们全部除以255
.将值转换为整数也可以.
The error here tells you that the pixels' values of your image (your array
are of type float
but that they are not in the range [-1, 1]
. As the values are in between [0, 255]
, you just need to divide them all by 255
. Converting the values to integers may also work.
此处是scikit-image解释支持的图像数据类型.
Here is the user-guide of scikit-image explaining the image data-types supported.
此页面上的两个句子:
- 请注意,即使数据类型本身可以超出该范围,浮动图像也应限制在-1到1范围内
- 您永远不要在图像上使用astype,因为它违反了有关dtype范围的这些假设
- Note that float images should be restricted to the range -1 to 1 even though the data type itself can exceed this range
- You should never use astype on an image, because it violates these assumptions about the dtype range
这篇关于为什么skimage表示过滤器不适用于浮点数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!