Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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 on the OpenCV forums: cv2.CreateMat in python, but the link is broken

like image 593
epsilones Avatar asked May 12 '16 09:05

epsilones


People also ask

How use OpenCV mat in Python?

For OpenCV 1. 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.

What is mat in OpenCV?

The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc. This class comprises of two data parts: the header and a pointer.

How does OpenCV store images Python?

When working with OpenCV Python, images are stored in numpy ndarray. To save an image to the local file system, use cv2. imwrite() function of opencv python library.

How do you define a CV in Python?

OpenCV stands for Open Source Computer Vision Library, which is widely used for image recognition or identification. It was officially launched in 1999 by Intel. It was written in C/C++ in the early stage, but now it is commonly used in Python for the computer vision as well.


2 Answers

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)
like image 124
Vtik Avatar answered Oct 20 '22 02:10

Vtik


Even though the question was asked a long time ago, this should help anyone looking for an answer today.

This should be applicable to opencv versions >= 2.X. In python, the opencv images are now represented as numpy arrays. So, a Mat object can simply be created as follows:

cvImg = np.zeros((columns, rows, channels), dtype = "uint8")
like image 23
Ankit Avatar answered Oct 20 '22 01:10

Ankit