在python中使用openCV创建Mat

在python中使用openCV创建Mat

问题描述:

我习惯了java的实现OpenCV。我想创建一个Mat结构,向其中填充数据,提取一个子域,然后应用一些图像转换。因此,在java,我用在python中使用openCV创建Mat

my_mat = new Mat(my_rows, my_cols, CvType.CV_8U); 
my_mat.put(0, 0, my_data); 
my_mat.submat(0, my_other_rows, 0, my_other_cols); 

但我没有发现任何东西在pythonOpenCV工作。我发现这link,但它被打破

+0

为什么投降? – Newben

对于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_<bit depth><S|U|F>C<number of channels> , 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) 
+0

谢谢,问题是我得到'AttributeError:'模块'对象没有属性'CreateMat'' ... – Newben

+0

哪个版本使用cv/cv2? – Vtik

+0

我正在使用'OpenCV 3.0' – Newben