how to transform a OpenCV cvMat back to ndarray in numpy ?


Question

I follow the code in OpenCV cookbook for python interface to transform cvMat to numpy array:

mat = cv.CreateMat(3,5,cv.CV_32FC1)
cv.Set(mat,7)
a = np.asarray(mat)

but with OpenCV 2.1 on my PC, it does not work. The result a here is a object array, using "print a" does not print all element in a, only print <cvmat(type=42424005 rows=3 cols=5 step=20 )>. so how to fully transform a OpenCV Mat object to original numpy.ndarray object.

1
10
4/23/2011 5:41:54 AM

Accepted Answer

Try using appending [:,:] to the matrix (ie. use mat[:,:] instead of mat) in your call to np.asarray - doing this will also allows asarray to work on images.

Your example:

>>> import cv
>>> import numpy as np
>>> mat = cv.CreateMat( 3 , 5 , cv.CV_32FC1 )
>>> cv.Set( mat , 7 )
>>> a = np.asarray( mat[:,:] )
>>> a
array([[ 7.,  7.,  7.,  7.,  7.],
       [ 7.,  7.,  7.,  7.,  7.],
       [ 7.,  7.,  7.,  7.,  7.]], dtype=float32)

And for an image:

>>> im = cv.CreateImage( ( 5 , 5 ) , 8 , 1 )
>>> cv.Set( im , 100 )
>>> im_array = np.asarray( im )
>>> im_array
array(<iplimage(nChannels=1 width=5 height=5 widthStep=8 )>, dtype=object)
>>> im_array = np.asarray( im[:,:] )
>>> im_array
array([[100, 100, 100, 100, 100],
       [100, 100, 100, 100, 100],
       [100, 100, 100, 100, 100],
       [100, 100, 100, 100, 100],
       [100, 100, 100, 100, 100]], dtype=uint8)
9
5/4/2011 2:41:29 PM

For the 2.1 version of OpenCV, if you need to share the memory and if you don't care about a bit of C programming and SWIG wrapping, you could try this solution that I used for some time:

CvMat * npymat_as_cvmat_32f(float * npymat_float, int rows, int cols)
{
  CvMat * cvmat;

  cvmat = cvCreateMatHeader(rows, cols, CV_32FC1);
  cvSetData(cvmat, npymat_float, cols * sizeof(float));

  return cvmat;
}

Create a header, for example, mat_conversion.h:

/* npymat_as_cvmat_32f
 * 
 * Create an OpenCV CvMat that shared its data with the input NumPy double array
 */
CvMat * npymat_as_cvmat_32f(float * npymat_float, int rows, int cols);

and a interface file (numpy_meets_opencv.i):

/* numpy_meets_opencv */
%module numpy_meets_opencv

%{
#define SWIG_FILE_WITH_INIT
#include <cv.h>
#include "mat_conversion.h"
%}

%include "numpy.i"

%init %{
import_array();
%}

%apply (float* INPLACE_ARRAY2, int DIM1, int DIM2) {(float* npymat_float, int rows, int cols)};

%include "mat_conversion.h"

Compile:

numpy_meets_opencv: numpy_meets_opencv.i mat_conversion.c
        swig -python -classic numpy_meets_opencv.i
        $(CC) $(NPY_CFLAGS) -fPIC -fno-stack-protector -c mat_conversion.c `pkg-config --cflags $(PKGS)`        
        $(CC) $(NPY_CFLAGS) -fPIC -fno-stack-protector -c numpy_meets_opencv_wrap.c `pkg-config --cflags $(PKGS)`
        ld -shared mat_conversion.o numpy_meets_opencv_wrap.o `pkg-config --libs $(PKGS)` -o _numpy_meets_opencv.so

Finally, you can do your stuff:

In [1]: import numpy_meets_opencv as npyocv
In [2]: import opencv as cv
In [4]: import numpy as npy
In [12]: Inpy = npy.zeros((5,5), dtype=npy.float32)
In [13]: Iocv = npyocv.npymat_as_cvmat_32f(Inpy)
In [14]: Inpy
Out[14]: 
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]], dtype=float32)
In [15]: Iocv
Out[15]: <opencv.cv.CvMat; proxy of <Swig Object of type 'CvMat *' at 0x30e6ed0> >
In [17]: cv.cvSetReal2D(Iocv, 3,3, 255)
In [18]: Inpy
Out[18]: 
array([[   0.,    0.,    0.,    0.,    0.],
       [   0.,    0.,    0.,    0.,    0.],
       [   0.,    0.,    0.,    0.,    0.],
       [   0.,    0.,    0.,  255.,    0.],
       [   0.,    0.,    0.,    0.,    0.]], dtype=float32)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon