Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert CvMat to Numpy array?

Looking to convert a CvMat to numpy array.

like image 324
jmnwong Avatar asked Jul 08 '26 04:07

jmnwong


1 Answers

From the OpenCV Python Cookbook:

>>> import cv, numpy                       # Comments added by me
>>> mat = cv.CreateMat(3, 5, cv.CV_32FC1)  # make a 3x5 mat
>>> cv.Set(mat, 7)                         # set the cells to 7
>>> a = numpy.asarray(mat)                 # convert it to a numpy array
>>> print a
[[ 7.  7.  7.  7.  7.]
 [ 7.  7.  7.  7.  7.]
 [ 7.  7.  7.  7.  7.]]

Here is the numpy.asarray documentation.

like image 75
agf Avatar answered Jul 10 '26 17:07

agf