在python中用openCV创建Mat [英] creating Mat with openCV in python

查看:395
本文介绍了在python中用openCV创建Mat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于 java OpenCV 的实现.我想创建一个 Mat 结构,将数据填充到其中,提取子垫,然后应用一些图像变换.因此,在 java 中,我使用

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

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);

但是我没有发现在 python OpenCV 中有任何工作.我发现了这个链接,但它已损坏

But I didn't find anything working in python's OpenCV. I found this link but it is broken

推荐答案

对于OpenCV 1.x:

您可以使用 CreateMat 来做到这一点:

You can use CreateMat to do that :

创建矩阵头并分配矩阵数据.

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);

对于cv2界面:

Python的新cv2接口将 numpy 数组集成到OpenCV框架中,这使操作变得更加简单用简单的多维数组表示.这是一个开始的例子:

The new cv2 interface for Python integrates numpy arrays into the OpenCV framework, which makes operations much simpler as they are represented with simple multidimensional arrays. here's a starting example :

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)

这篇关于在python中用openCV创建Mat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆