为什么 skimage 均值过滤器不适用于浮点数组?

pd *_*hah 2 python filtering mean scikit-image

例如,我将在浮点数组上应用平均过滤器window_size=3。我找到了这个库:

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]]
Run Code Online (Sandbox Code Playgroud)

但是这个函数不能在浮点数组上运行:

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.
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

Esk*_*app 5

一般来说(这对其他编程语言有效),图像通常可以用两种方式表示:

  • 强度值在范围内[0, 255]。在这种情况下,值是类型uint8- 无符号整数 8 字节。
  • 强度值在范围内[0, 1]。在这种情况下,值的类型为float

根据语言和库,像素强度允许的值的类型和范围可能或多或少是允许的。

这里的错误告诉您图像的像素值(您array的类型float但它们不在范围内[-1, 1]。由于值介于 之间[0, 255],您只需要将它们全部除以255。将值转换为整数也可能工作。

是 scikit-image 的用户指南,解释了支持的图像数据类型。

本页的两句话:

  • 请注意,浮动图像应限制在 -1 到 1 的范围内,即使数据类型本身可能超出此范围
  • 你永远不应该在图像上使用 astype,因为它违反了关于 dtype 范围的这些假设