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.

4
  • do you get error message or what ? What is data ? What result do you expect ? Commented Jan 16, 2017 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 Commented Jan 16, 2017 at 23:48
  • You should be using cv2.imdecode as explained in the following answer stackoverflow.com/questions/17170752/… Commented Jan 16, 2017 at 23:57
  • Possible duplicate of Python OpenCV load image from byte string Commented Jan 16, 2017 at 23:59

1 Answer 1

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])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.