scikit-image saves binary image as completely black image

蓝咒 提交于 2020-05-15 04:12:30

问题


So, I am trying to get binary image with scikit-image and save it on disk with the following code:

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

When I do io.imshow(binary), I get what I expected. But the imsave() return to me completely black image, as if it turn both True and False values into (0,0,0) in rgb or something.

So what is the right way to do it?


回答1:


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

This seems to work, but I'm not sure it's the best way to do it.

Also, there's an issue opened on scikit-image repo: https://github.com/scikit-image/scikit-image/issues/1623




回答2:


Simply casting binary into float also does the trick:

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



回答3:


I prefer to use img_as_ubyte:

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

Besides, you don't get to see the Lossy conversion from uint16 to uint8. Losing 8 bits of resolution... warning from img_as_uint



来源:https://stackoverflow.com/questions/36206321/scikit-image-saves-binary-image-as-completely-black-image

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