Why skimage mean filter does not work on float array?

送分小仙女□ 提交于 2019-12-11 04:24:57

问题


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.

How to solve this?


回答1:


In general (and this is valid for other programming languages), an image can be typically represented in 2 ways:

  • with intensity values in the range [0, 255]. In this case the values are of type uint8 - unsigned integer 8-bytes.
  • with intensity values in the range [0, 1]. In this case the values are of type float.

Depending on the language and library, the types and range of values allowed for the pixels' intensity can be more or less permissive.

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.

Here is the user-guide of scikit-image explaining the image data-types supported.

Two sentences from this page:

  • 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


来源:https://stackoverflow.com/questions/45669794/why-skimage-mean-filter-does-not-work-on-float-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!