对于OpenCV 1.x:
您可以使用CreateMat来执行此操作:Creates a matrix header and allocates the matrix data.Python: cv.CreateMat(rows, cols, type) → mat
Parameters:
rows – Number of rows in the matrix
cols – Number of columns in the matrix
type – The type of the matrix elements in the form CV_C , where S=signed, U=unsigned, F=float. For example, CV _ 8UC1 means the elements are 8-bit unsigned and the there is 1 channel, and CV _ 32SC2 means the elements are 32-bit signed and there are 2 channels.
函数调用等效于以下代码:CvMat* mat = cvCreateMatHeader(rows, cols, type);
cvCreateData(mat);
对于cv2接口:
Python的新cv2接口将numpy数组集成到OpenCV框架中,这使得操作更加简单,因为它们是用简单的多维数组表示的。
下面是一个开始的例子:import numpy as np, cv
vis = np.zeros((384, 836), np.float32)
h,w = vis.shape
vis2 = cv.CreateMat(h, w, cv.CV_32FC3)
vis0 = cv.fromarray(vis)