1

How do I convert a byte array to Mat object in Python.

import numpy as np

mat = np.asarray(0, 0, data)        
print 'Cols n Row ' + str(mat.cols) + " " + str(mat.rows)

But it is not working. Can some one help.

jgm
  • 1,230
  • 1
  • 19
  • 39
  • do you get error message or what ? What is `data` ? What result do you expect ? – furas Jan 16 '17 at 23:44
  • `data` is the byte array, which I want to convert as `OpenCV Mat`. I am not getting any output. This is the error `TypeError: data type not understood ` – jgm Jan 16 '17 at 23:48
  • You should be using `cv2.imdecode` as explained in the following answer http://stackoverflow.com/questions/17170752/python-opencv-load-image-from-byte-string – Surya Avala Jan 16 '17 at 23:57
  • Possible duplicate of [Python OpenCV load image from byte string](http://stackoverflow.com/questions/17170752/python-opencv-load-image-from-byte-string) – Surya Avala Jan 16 '17 at 23:59

1 Answers1

1

What about giving it a try like this? Probably you need to define the data type of the matrix, else it is not obvious how to divide the byte array into 'chunks' that will be stored in one cell.

mat = np.asarray(data, dtype=np.uint8)        
print 'Cols n Row ' + str(mat.shape[1]) + " " + str(mat.shape[0])
ilke444
  • 2,641
  • 1
  • 17
  • 31