[Opencv Study Notes] 3: mat initial creation method-requires continuous data storage

Source: Internet
Author: User

There are many initial creation methods for mat. The following lists some of my favorite methods and some precautions during use.

A basic knowledge of using mat is that it can be divided into two parts: "Data header + Data body", and the two parts can be separated in the memory, the production process does not require the concurrency/order to complete the whole process. Therefore, the existence mode of a mat variable includes: Short, header + body. In addition, the data body can be shared with other mat variables. Shared data blocks can be destroyed and released only when they are not used. Worship the great gods of opencv, can design mat this rookie so beautiful and competent!
Haha

In opencv, when representing a range, the default format is "left closed and right open". Therefore, you need to pay attention to its boundary when extracting an image.

1. Single-value Filling
Mat A (rows, cols, cv_8uc4, scalar (,); // The four channels of a are filled with 1 2 3 4 mat B (MAT :: eye (rows, cols, cv_32s); // uses min (rows, cols) as the side length to form a square. the diagonal line is filled with 1 from (0, 0 ).

2. multi-value Filling

Mat sobelh = (MAT _ <char> (3, 3) <-1,-2,-1, 0, 0, 0, 1, 2, 1 ); // fill in the specified value mat A (getgaussiankernel (7, 2 ));//

3. Reference Filling

Since it is a reference, most of the time there is no data copy, of course, sometimes (based on the vector and pointer) can also specify the copy.

3.1 from mat

Extract part as ROI
Mat B (A, rect (10, 10,100,100); // The rectangle between using a rectangle [10,100) will be extracted from mat C (A (rect (10, 10,100,100); // B and c Equivalent mat D (A (range: All (), range (1, 3 ))); // using row and column boundaries extracts [1, 3) Columns
3.2 from Vector

Opencv supports and extends the vector in STL.

When you create a mat as follows, you can only get one column of data, but the number of channels differs from the data type. What's strange is that some functions do not support this mat, for example, the test showed that the inrang () function does not support mat C (the following 3.3 pointer method is used to obtain a row of mat,The inrang () function does not return an error-The size does not match..

// ~ E is a column of 60*1, the difference is the number of channels and Data Type mat A (vector <int> (60); // single channel, 32 smat B (vector <VEC <int, 8> (60); // 8 channel, 32 SMAT C (vector <vec4i> (60 )); // 4 channels, 32 smat d (vector <point> (60); // 2 channels, 32 smat e (vector <point2f> (60 )); // 2-channel, 32f // although no error is reported during compilation, F is invalid and unavailable, because such two-dimensional dynamic application memory is not consecutive mat F (vector <int> (60, vector <int> (4 )));


3.3 From pointer
Int rows = 15; // int Cols = 40; int size = rows * Cols; vector <int> VA (size); vector <VEC <int, 8> VB (size); vector <vec4i> VC (size); vector <point> VD (size); vector <point2f> ve (size ); vector <vector <int> VF (size, vector <int> (4); MAT a (rows, cols, cv_32sc1, & va [0]); // single channel, 32 smat B (rows, cols, cv_32sc (8), & VB [0] [0]); // 8 channel, 32 SMAT C (rows, cols, cv_32sc4, & VC [0] [0]); // 4-channel, 32 S, valid, but data is not normal, it is estimated that matx mat D (rows, cols, cv_32sc2, & VD [0]. x); // 2 channels, 32 smat e (rows, cols, cv_32fc2, & ve [0]. x); // 2 channels, 32 fmat F (rows, cols, cv_32sc4, & VF [0] [0]); // invalid, although it also occupies some memory space

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.