Home > OS >  Scikit Image load binary image and convert to binary matrix
Scikit Image load binary image and convert to binary matrix

Time:06-11

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?

CodePudding user response:

On grayscale, a pixel with value 1 doesn't appear white - this simply happens because matplotlib normalizes the image before displaying it.

Choose either:
a) Keep the original binary values, then the saved image won't have a white line in the middle
b) Have a white line in the middle, but then you'll have to modify the array before saving and after loading it.

Ad b)

import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np

# This is the array you have
arr = np.zeros((11, 11), dtype=np.uint8)
arr[:, 5] = 1

plt.figure()
plt.imshow(arr, cmap='gray')
plt.show()


# This will ensure that the line appears white in the .png
arr_png = arr * 255  # 2**8 - 1

# Write to disk
cv.imwrite('line.png', arr_png)

# Load from disk
arr_from_disk = np.array(cv.imread('line.png', 0), dtype=np.uint8)

# Rescale
arr_from_disk = np.divide(arr_from_disk, 255)

assert np.array_equal(arr, arr_from_disk), 'Oops'

CodePudding user response:

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.

  • Related