开发者

how to assign a list object to cvmat using python interface in OpenCV?

I want to fill a cvMat object using cv.SetData() using python interface in OpenCV 2.1. In C, it can be done using the code below.

double a[] = {1,2,3,4,5,6,7,8,9,10,11,12};
cvMa开发者_如何学编程t Ma;
cvInitMatHeader(&Ma,3,4,CV_64FC1,a,CV_AUTOSTEP);

but in python interface, there is no InitMatHeader function, so I try to code like this:

a = range(1,13)
Ma = cv.CreateMatHeader(3,4,cv.CV_64FC1)
cv.SetData(Ma,a,cv.CV_AUTOSTEP)
PrintMat(Ma) # my own function to display all elements in a mat

then I got a TypeError:CvMat argument 'src' has no data. It seems that variable Ma has not been initialized, so how can I finish this operation, using numpy?


Try passing an array.array('d', vals) or a numpy.array(vals, numpy.float64):

import array
import cv
import numpy

ar = array.array('d', range(1, 13))
na = numpy.array(range(1, 13), numpy.float64)
for vals in (ar, na):
    m = cv.CreateMatHeader(3, 4, cv.CV_64FC1)
    cv.SetData(m, vals, cv.CV_AUTOSTEP)
    print cv.mGet(m, 0, 2)

Output:

3.0
3.0
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜