gpt4 book ai didi

python - 为什么 skimage 意味着过滤器不适用于 float 数组?

转载 作者:太空宇宙 更新时间:2023-11-04 09:56:21 24 4
gpt4 key购买 nike

例如,我将对具有 window_size=3 的 float 组应用均值过滤器。我找到了这个库:

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]]

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

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.

如何解决?

最佳答案

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

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

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

此处的错误告诉您图像的像素值(您的数组float 类型,但它们不在[- 1, 1]。由于值介于 [0, 255] 之间,您只需将它们全部除以 255。将值转换为整数也可能有效。

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

本页的两句话:

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

关于python - 为什么 skimage 意味着过滤器不适用于 float 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45669794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com