Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
729 views
in Technique[技术] by (71.8m points)

c++ - creating Mat with openCV in python

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For OpenCV 1.x :

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

For cv2 interface :

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...