[Ubuntu + opencv] matrix creation and initialization of opencv-learning notes [4]

Source: Internet
Author: User

I. Create a Matrix

 


Multiple matrix creation methods
There are multiple methods to define a matrix variable:

(1) The most common method is to use cvcreatemat ()

It consists of two original functions, cvcreatematheader
()
And
Cvcreatedata
()
.

// Create a new rows by Cols matrix of Type 'type'. <br/> // <br/> cvmat * cvcreatemat (INT rows, int cols, int type );

Type: Type of the matrix element. the format is CV _ <bit_depth> (S | u | f) C <number_of_channels>. for example, cv_8uc1 indicates the 8-bit unsigned single-channel matrix, and cv_32sc2 indicates the 32-Bit Signed dual-channel matrix. routine:Cvmat * m = cvcreatemat (4,4, cv_32fc1 );Create a 4x4 matrix. When the Data Type of the matrix element is float (not double type !!) Cvcreatemat
Creates a matrix header and allocates the matrix data.
Create a matrix header and allocate memory space for the matrix data
Cvmat * cvcreatemat (
Int rows,
Int cols,
Int type );
Rows number of rows in the matrix // number of rows in the Matrix
Cols Number of columns in the matrix // Number of columns in the Matrix
Type the type of the matrix elements in the form CV <bit depth> <S | u | f> C <Number
Channels>, where S = signed, u = unsigned, F = float. For example, CV 8uc1 means
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.

Cvcreatemat is actually a concise form in the following forms
This is the concise form:
Cvmat * m = cvcreatematheader (4, 4, cv_32fc1); cvcreatedata (m); // the preceding two statements are equivalent to the following statements: cvmat * m = cvcreatemat (4, 4, cv_32fc1 );

(2) cvcreatematheader ()
Function creates a cvmat structure without allocating memory for data, while cvcreatedata

() The function is only responsible for data memory allocation. Sometimes, you only need the cvcreatematheader function.
() Because the bucket has been allocated for other reasons, or because the bucket is not yet ready to be allocated.


Cvcreatematheader

Creates a matrix header but does not allocate the matrix data.

// Create a matrix header without allocating memory space for the matrix data

Cvmat * cvcreatematheader
(
Int rows,
Int cols,
Int type );
Rows number of rows in the Matrix
Cols Number of columns in the Matrix
Type type of the matrix elements, see cvcreatemat


The function allocates a new matrix header and returns a pointer to it. The matrix data can
Then be allocated using cvcreatedata
Or set explicitly to user-allocated data via cvsetdata.

 


Cvcreatedata

Allocates array data // allocate memory space for the Array

Void cvcreatedata (cvarr * ARR );
Arr array header // arr is the array Header


The function allocates image, matrix or multi-dimen1_array data. Note that in the case
Of Matrix types opencv allocation functions are used and in the case of iplimage they are used
Unless CV turn on IPL compatibility was called. In the latter case IPL functions are used
To allocate the data.

// This function allocates memory space for images, matrices, or multi-dimensional arrays. Note that the opencv memory allocation function is called when arr is a matrix. When arr is an image
CV turn on IPL compatibility is also
Call the opencv memory allocation function. The latter (that is, when arr is an image) calls the IPL function to allocate data memory.

// Create only matrix header without allocating data <br/> // <br/> cvmat * cvcreatematheader (INT rows, int cols, int type );

(3) Use the cvclonemat (cvmat *) function *)

Creates a new matrix based on an existing matrix.

// Allocate a new matrix just like the matrix 'Mat '. <br/> // <br/> cvmat * cvclonemat (const cvmat * mat );

Cvmat * m1 = cvcreatemat (4,4, cv_32fc1); <br/> cvmat * m2; <br/> m2 = cvclonemat (M1 );

Cvclonemat
Creates a full matrix copy. // create a matrix with both a matrix data header and a matrix data memory space
Cvmat * cvclonemat (const cvmat * mat );
Mat matrix to be copied
Creates a full copy of a matrix and returns a pointer to the copy.

(4) direct definition

// Of course, this method is not commonly used, and familiar with opencv will know that there is a pointer everywhere, so it is generally used
Cvcreatemat ()



Cvmatmb;

(5) When the created matrix is no longer needed, you can call the cvreleasemat (cvmat *) function *)
Release it.

Note:
: The Matrix created from above (4) does not need to be called
Cvreleasemat (cvmat *)
Release it.

// Free the matrix 'Mat ', both header and data. <br/> // <br/> void cvreleasemat (cvmat *** mat );

 

Ii. Matrix assignment (initialization)

 

(1) initialize cvinitmatheader and existing data




// Initialize header on existiong cvmat structure <br/> // <br/> cvmat * cvinitmatheader (</P> <p> cvmat * mat, <br/> int rows, <br/> int cols, <br/> int type, <br/> void * Data = NULL, <br/> int step = cv_autostep <br/> );


// If it is set to double, float is supported only !! <Br/> float arr [] = {, 3, <br/>, 6, }; <br/> // 1. use existing data for initialization <br/> cvmat Ma; <br/> cvinitmatheader (& Ma, 2,3, cv_32fc1, arr, cv_autostep );

This function is often used to process raw data with opencv matrix functions. for example, <br/> the following code computes the matrix product of two matrices, stored as ordinary Arrays: <br/> double A [] = {1, 2, 3, 4, <br/> 5, 6, 7, 8, <br/> 9, 10, 11, 12}; <br/> double B [] = {1, 2, 3, 4, 5, <br/> 6, 7, 8, 9, 10, 11, 12 }; <br/> double C [9]; <br/> cvmat Ma, MB, MC; <br/> cvinitmatheader (& Ma, 3, 4, cv_64fc1, a); <br/> cvinitmatheader (& MB, 4, 3, cv_64fc1, B ); <br/> cvinitmatheader (& Mc, 3, 3, cv_64fc1, c); <br/> cvmatmuladd (& Ma, & MB, 0, & Mc ); <br/> // The C array now contains the product of a (3x4) and B (4x3) <br/>

 

(2) Use the cvmat constructor for initialization.

Cvmat
Initializes matrix header (lightweight variant ).
Cvmat (
Int rows,
Int cols,
Int type,
Void * Data = NULL );

Rows number of rows in the Matrix
Cols Number of columns in the Matrix
Type type of the matrix elements-see cvcreatemat
Data optional Data Pointer assigned to the matrix Header
Initializes a matrix header and assigns data to it. The matrix is filled row-wise (the first Cols
Elements of data form the first row of the matrix, etc .)
This function is a fast inline substitution for cvinitmatheader. Namely, it is equivalent:

Cvmat;
Cvinitmatheader (& mat, rows, cols, type, Data, cv_autostep );

// If it is set to double, float is supported only !! <Br/> float arr [] = {1, 2, 3, <br/> 4, 5, 6, }; <br/> // 2. the cvinitheader and cvmat principles are almost the same, but the function prototype is not the same <br/> cvmatmb; <br/> MB = cvmat (2, 3, cv_32fc1, arr );

Iii. Practice

# Include "CV. H "<br/> # include" cxcore. H "<br/> # include <stdio. h> <br/> // print matrix elements <br/> // see Example 3-9 of learning opencv <br/> void print_mat (const cvmat * mat, const int row, const int col) <br/>{< br/> int I; <br/> Int J; </P> <p> // use a pointer to access a matrix element <br/> for (I = 0; I <row; I ++) <br/> {<br/>/* uchar * PTR = (uchar *) (mat-> data. PTR + I * mat-> step); */<br/> // floar is incorrect <br/> float * PTR = (float *) (mat-> data. PTR + I * Mat-> step); <br/> for (j = 0; j <Col; j ++) <br/>{< br/> printf ("/T % F", PTR [J]); // use * PTR ++ or <br/>}< br/> printf ("/N "); <br/>}< br/> printf ("/n"); <br/>}< br/> // <br/> int main () <br/> {<br/> // It cannot be set to double. Float is supported only !! <Br/> float arr [] = {, 3, <br/>, 6, }; <br/> // 1. use existing data for initialization <br/> cvmat Ma; <br/> cvinitmatheader (& Ma, 2,3, cv_32fc1, arr, cv_autostep); <br/> // 2. the cvinitheader and cvmat principles are almost the same, but the function prototype is not the same <br/> cvmatmb; <br/> MB = cvmat (2, 3, cv_32fc1, arr ); <br/> // 3. cvcreatemat returns a matrix pointer <br/> cvmat * MC = cvcreatemat (2,2, cv_32fc1); <br/> // cvmget (...), cvmset (...) valid only for floating-point single-channel data <br/> cvmset (MC, 1); <br/> cvmset (MC, 2); <br/> cvmset (MC,, 3); <br/> cvmset (MC, 4); <br/> cvmat * MD = cvcreatemat (, cv_64fc1); <br/> int I; <br/> Int J; <br/> int n = 0; <br/> // use pointers to Access Matrix Elements <br/> for (I = 0; I <3; I ++) <br/> {<br/> float * PTR = (float *) (MD-> data. PTR + I * MD-> step); <br/> for (j = 0; j <4; j ++) <br/>{< br/> PTR [J] = ++ N; <br/>}< br/> printf ("/N "); <br/>}< br/> print_mat (& Ma, 2, 3); <br/> print_mat (& MB, 2, 3); <br/> print_mat (MC, 2, 2 ); <br/> print_mat (MD, 3,4); </P> <p> cvreleasemat (& Mc); <br/> cvreleasemat (& MD ); <br/> return 0; <br/>}

Running result:

Opencv @ Ubuntu :~ /Desktop/opencv $ GCC 'pkg-config opencv -- Libs -- cflags opencv '-G mat. C-o mat <br/> opencv @ Ubuntu :~ /Desktop/opencv $. /MAT <br/> 1.0000002.0000003.000000 <br/> 4.0000005.0000006.000000 <br/> renew <br/> 1.0000002.000000 <br/> 3.0000004.000000 <br/> renew <br/>/> 5.0000006.0000007.0000008.000000 <br/> 9.00000010.00000011.00000012.000000

 

It is not easy to code one by one. If you are afraid of mistakes, you need to refer to the English reference. Reprinted please indicate the source http://blog.csdn.net/moc062066/archive/2011/06/15/6546612.aspx
. Thank you !!

 

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.