Scikit Image load binary image and convert to binary matrix

Issue

I have created a numpy array shape(11 x 11) with all pixels 0 excluding one column filled with 1.

[[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]]

The array was saved as a png image using matplotlib.imsave yielding the expected image – black background with a white line in the middle.

When trying to reimport the saved png image
skipy.imread and Pil.Image.Open yield an array of the form

[[[ 68   1  84 255]
  [ 68   1  84 255]
  [ 68   1  84 255]
  [ 68   1  84 255]
  [ 68   1  84 255]
  [253 231  36 255]
  [ 68   1  84 255]
  [ 68   1  84 255]
  [ 68   1  84 255]
  [ 68   1  84 255]
  [ 68   1  84 255]]
...
]

What does this file format mean (could not find an explanation in the scikit image documentation) ?

And how do I convert it back to the binary input image?

Solution

What you see is explained thusly:

  • your data was grayscale
  • then you plotted that with a colormap
    — the line looks yellow and the background looks dark blue/violet?
  • then you told matplotlib to save that false-color picture
  • then you read that false-color picture back
  • now you have RGBA pixel data. you see the first pixel row, and each value of each color pixel

If you wanted to maintain the grayscale appearance of your data, you’d have some choices.

Use plt.imshow(arr, cmap="gray"), which uses a gray color map rather than a colorful one.

When reading the image, and also converting any color to grayscale, you can choose scikit-image or OpenCV. OpenCV has cv.imread(fname, cv.IMREAD_GRAYSCALE). scikit-image offers skimage.io.imread(fname, as_gray=True).

And really you should use scikit-image or OpenCV for writing your picture in the first place. Matplotlib is for plotting, not for storing data authentically. Matplotlib took your data and rescaled it so the maximum and minimum value become 0 and 1, which is black and white for the gray cmap.

Answered By – Christoph Rackwitz

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published