Skip to content
Advertisement

Convert a binary image to 2D array or Matrix in Python?

I’m new to image processing and I’m really having a hard time understanding stuff…so the idea is that how do you create a matrix from a binary image in python?

to something like this:

It not the same image though the point is there. Thank you for helping, I appreciate it cheers

Advertisement

Answer

Using cv2 -Read more here

import cv2
img = cv2.imread('path/to/img.jpg')
resized = cv2.resize(img, (128, 128), cv2.INTER_LINEAR)
print pic

Using skimage – Read more here

import skimage.io as skio
faces = skio.imread_collection('path/to/images/*.pgm',conserve_memory=True) # can load multiple images at once

Using Scipy – Read more here

from scipy import misc
pic = misc.imread('path/to/img.jpg')
print pic

Plotting Images

import matplotlib.pyplot as plt
plt.imshow(faces[0],cmap=plt.cm.gray_r,interpolation="nearest")
plt.show()
Advertisement