【问题标题】:scikit-image saves binary image as completely black imagescikit-image 将二进制图像保存为全黑图像
【发布时间】:2016-07-12 09:44:05
【问题描述】:

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

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

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

那么正确的做法是什么?

【问题讨论】:

  • 你能用随机生成的数组重现这个吗?

标签: python image scikit-image


【解决方案1】:
from skimage import img_as_uint
# ...
io.imsave("./testout/" + img_name, img_as_uint(binary))

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

另外,在 scikit-image repo 上打开了一个问题:https://github.com/scikit-image/scikit-image/issues/1623

【讨论】:

    【解决方案2】:

    只需将binary 转换为float 也可以解决问题:

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

    【讨论】:

      【解决方案3】:

      我更喜欢使用img_as_ubyte:

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

      此外,您不会看到来自img_as_uintLossy conversion from uint16 to uint8. Losing 8 bits of resolution... 警告

      【讨论】:

        猜你喜欢
        • 2013-04-16
        • 2018-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-03
        • 2018-11-02
        • 2014-08-06
        • 1970-01-01
        相关资源
        最近更新 更多