为什么skimage平均过滤器不适用于浮点数组?
问题描述:
例如,我将对浮点数组应用均值过滤器,例如window_size=3
。我发现这个库:为什么skimage平均过滤器不适用于浮点数组?
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]]
不过这个功能不能在浮标阵运行:
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.
如何解决这个问题?
答
通常(并且这适用于其他编程语言),可以将图像信号通常表示在2种方式:
- 与强度值的范围在
[0, 255]
。在这种情况下,这些值的类型为uint8
- 无符号整数8字节。 - 其强度值在
[0, 1]
的范围内。在这种情况下,值是float
。
根据语言和库的不同,像素强度允许的值的类型和范围可以更宽容或更宽松。
在这里的错误告诉你,像素你的形象价值(您array
是float
类型,但它们是不在范围[-1, 1]
。由于值是[0, 255]
之间,你只需要通过划分他们都。255
转换值,以整数也可以从这个页面工作
Here解释是支持的图像数据类型scikit图像的用户指南
两句话:。
- 需要注意的是浮动图像应该被限制在-1到1即使数据类型本身可以超过这个范围
- 你不应该在图像上使用astype,因为它违反了有关的D型这些假设范围