scikit-image 将二值图像保存为全黑图像

scikit-image saves binary image as completely black image

因此,我尝试使用 scikit-image 获取二进制图像并使用以下代码将其保存在磁盘上:

gray = color.rgb2gray(img)
thresh = filters.threshold_otsu(gray)
binary = gray >= thresh
io.imsave("./testout/" + img_name, binary)

当我执行 io.imshow(binary) 时,我得到了预期的结果。但是 imsave() return 对我来说是完全黑色的图像,就好像它把 True 和 False 值都变成了 rgb 或其他东西的 (0,0,0)。

那么正确的做法是什么?

from skimage import img_as_uint
# ...
io.imsave("./testout/" + img_name, img_as_uint(binary))

这似乎可行,但我不确定这是最好的方法。

此外,在 scikit-image 存储库上打开了一个问题:https://github.com/scikit-image/scikit-image/issues/1623

简单地将 binary 转换为 float 也可以达到目的:

binary = (gray >= thresh).astype(float)

我更喜欢使用img_as_ubyte:

from skimage import img_as_ubyte
io.imsave("demo.jpg", img_as_uint(binary))

此外,您不会看到来自 img_as_uint

Lossy conversion from uint16 to uint8. Losing 8 bits of resolution... 警告