【发布时间】:2020-12-21 15:11:04
【问题描述】:
我目前正在使用 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
输出是一个 numpy 数组,该图像的数据类型是 bool
[[ 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)
这使图像看起来像这样:
我也尝试将数据类型从 bool 更改为 uint8,但是我会得到以下异常:
AttributeError: 'numpy.ndarray' object has no attribute 'mask'
到目前为止,我还没有找到一个解决方案来获得看起来像阈值处理结果的 PIL 图像。
【问题讨论】:
-
“我也尝试将数据类型从 bool 更改为 uint8”,请展示一下尝试。它显然没有使用
view或astype,所以真的不确定你做了什么。 -
我尝试了以下行将 dtype 更改为 uint8
image = Image.fromarray(binary_sauvola.astype('uint8')) -
然后显示堆栈跟踪。这个错误似乎很奇怪。请编辑问题。如果可以避免,请不要将牛和错误放入 cmets。
标签: python numpy image-processing python-imaging-library scikit-image