scikit-image saves binary image as completely black image

Viewed 6655

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?

3 Answers

Simply casting binary into float also does the trick:

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

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

Related