在 python 中使用 openCV 创建 Mat
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37182774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
creating Mat with openCV in python
提问by epsilones
I am used to java
's implementation of OpenCV
. I want to create a Mat
structure, fill data into it, extract a submat and then apply some image transform. So in java
, I use
我已经习惯java
了OpenCV
. 我想创建一个Mat
结构,将数据填充到其中,提取一个子垫,然后应用一些图像变换。所以在java
,我使用
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);
But I didn't find anything working in python
's OpenCV
. I found this linkbut it is broken
但是我在python
's 中没有找到任何工作OpenCV
。我找到了这个链接,但它坏了
回答by Vtik
For OpenCV 1.x :
对于 OpenCV 1.x :
You can use CreateMatto do that :
您可以使用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.
The function call is equivalent to the following code:
函数调用等价于以下代码:
CvMat* mat = cvCreateMatHeader(rows, cols, type);
cvCreateData(mat);
For cv2 interface :
对于 cv2 接口:
The new cv2 interface for Python integrates numpyarrays into the OpenCV framework, which makes operations much simpler as they are represented with simple multidimensional arrays. here's a starting example :
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)