我目前正在使用scikit-image库中的python进行图像处理。我正试图使用以下代码使用sauvola阈值将图像制作成二进制图像:
from PIL import Image
import numpy
from skimage.color import rgb2gray
from skimage.filters import threshold_sauvola
im = Image.open("test.jpg")
pix = numpy.array(im)
img = rgb2gray(pix)
window_size = 25
thresh_sauvola = threshold_sauvola(img, window_size=window_size)
binary_sauvola = img > thresh_sauvola
以下是结果:
![enter image description here](https://istack.dev59.com/Pfpjl.webp)
输出结果是一个numpy数组,数据类型为布尔型。
[[ True True True ... True True True]
[ True True True ... True True True]
[ True True True ... True True True]
...
[ True True True ... True True True]
[ True True True ... True True True]
[ True True True ... True True True]]
问题在于,我需要使用以下代码将该数组转换回 PIL 图像:
image = Image.fromarray(binary_sauvola)
这将使图像看起来像这样:
我还尝试将数据类型从布尔型更改为uint8,但是我会收到以下异常:
AttributeError: 'numpy.ndarray' object has no attribute 'mask'
到目前为止,我还没有找到一种方法来获取类似于阈值处理结果的PIL图像。
view
或astype
,所以我不确定你做了什么。 - Mad Physicistimage = Image.fromarray(binary_sauvola.astype('uint8'))
- R.hagens