Opencv C interface and C ++ Interface

Source: Internet
Author: User

From www.opencvchina.com

Opencv from C to C ++
After opencv2.0 is released, its new C ++ interface, CV: mat, replaces the original C-style cvmat and iplimage. Currently, version 2.0 also supports C interfaces.

Compared with the C interface, the C ++ CV: mat unifies the two concepts of matrix and image. In fact, matrices and images are actually the same. Because CV: mat is a class of C ++, it also has some relevant features. For example, the memory is released. In C ++, when an object exceeds its usage range, it will automatically call the destructor to destroy it. In C, if you use the cvcreateimage function for variables of the cvmat type
If the function is allocated with memory space, you must call the corresponding function to release it, instead of destroying it automatically. If no corresponding release occurs, memory leakage may occur.

CV: mat Introduction
Before using the C ++ interface, first include the corresponding opencv namespace

After using the # include statement to include the corresponding header file, use the following statement to include the corresponding opencv namespace

Using namespace CV;

If this statement is not available, the relevant resources in the namespace must carry the CV prefix, such as CV: mat, which indicates that the mat in the namespace CV is used;

With the using namespace CV statement, you can directly write the mat

This new type of Mat supports matrix algebra operations similar to MATLAB, for example:

Mat A = MAT (3, 4, cv_32fc1 );

Mat B = MAT (4,3, cv_32fc1 );

...

// Initialization of matrix A and matrix B is ignored here

...

Mat c = 2 * a * B;

Then, matrix C is a 3*3 matrix. It is the result of multiplication of matrix A and matrix B by factor 2. This method is more intuitive than the C interface.

Mat also has other methods for matrix operations, such:

Mat C = C. inv (); // now C is its own Inverse Matrix

Mat d = A. T (); // D is the transposed matrix of

Internal Structure of mat
The mat and C-style cvmat are the same as the iplimage. The origin (origin) is at the top left, and the count of rows and columns starts from 0.

Matrix mat declaration method

The matrix can have 1, 2, 3, or 4 channels.

The simplest method to create a matrix is:

Mat M = MAT (rows, cols, type );

Rows and Cols represent the number of rows and columns of the matrix respectively. type is the matrix type, which is the same as the original C style.

If you create an image, the recommended method is:

Mat M = mat (SIZE (width, height), type );

If the image is created and the other image is of the same size, then:

Mat n = MAT (M. Size (), type );

Access to Matrix Elements
Accessing a single-channel matrix is the simplest and of course the most important. You can use the mat method at to access the value at the vertex (I, j.

Mat A = MAT (4,3, cv_32fc1 );

Float elem_a =. at <float> (I, j); // obtain the matrix element AIJ. The value range of I is 0 T to rows-1, and that of J is 0 to Cols-1.

In addition, you can also use this method:

Point P = point (x, y );

Float elem_a = A. at <float> (p); // Note: The value range of Y is 0 to rows-1, and the value range of X is 0 to Cols-1.

If the matrix to be accessed is a multi-channel matrix, it may be a little bit of trouble, but it will not be too troublesome. Use the PTR method of mat to obtain the pointer pointing to a row, and then use [] to access specific values on a specific channel.

Type ELEM = matrix. PTR <type> (I) [nchannels * j + C]

Where,

Type: refers to the Data Type of the matrix (float, double, uchar, etc)

I: row number (which row do you want to access)

Nchannels: number of channels in the matrix (number of channels in the matrix: 1, 2, 4)

J: column number (which column do you want to access)

C: Channel Number (the value of the channel you want to access)

The above method can also be used to access the matrix of a single channel, but the nchannels value is 1 and C = 0.

Reshape of the matrix
The Reshape of a matrix is a conversion between the matrix channel and the row of the matrix. For example, assume that we have a matrix where the number of channels is NC and the matrix size is N * 1. to convert this matrix into a matrix of N * NC sizes in a single channel, use the following code:

Mat A = MAT (, cv_32fc3); // A is a 4*1, 3 Channel Matrix

Mat B = A. reshape (1); // B is a 4*3, 1 Channel Matrix

In what scenarios is the matrix reshape used? Suppose we have a series of point objects, as follows:

Vector <; point32f> V; // assume that the data is full.

The data in V is:

[(X0, y0, z0)]

[(X1, Y1, Z1)]

[(X2, Y2, Z2)]

[(X3, Y3, Z3)]

[(...,...,...)]

The following code imports the data in vector V to mat:

Mat M1 = MAT (v, true); // If the Boolean variable is true, data is copied from V to M1.

If the Boolean variable is not true, the V data is not copied to M1, but the Data Pointer of M1 is directed to v.

After the code above, the matrix M1 contains multiple rows of data. To be precise, the matrix M1 is a three-channel, one-column matrix. Of course, there are multiple rows, the number of rows is the same as the number of point objects, that is, m1.rows and V. the size () is the same. In this case, we can reshape this matrix into a matrix of rows v. Size, three columns, and one channel.

After reshape, you can perform the aforementioned matrix algebra operation on the matrix. Reshape is a required step for processing the coordinates of a video clip.

Some equivalence between C interface and C ++ Interface
Cvsize-> size

Cvvideocapture-> videocapture

Iplimage, cvmat-> mat

Cvqueryframe-> (operator)

Cvshowimage-> imshow

Cvloadimage-> imread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.