The Mat class can be used not only for image storage, but also as a common matrix class that can be used to create and manipulate multidimensional matrices, and later will update the mat as a learning note for the Matrix class. This paper focuses on the re-learning of the structure of the Mat class, mainly the important member variables and constructors.
First, Mat class
The key properties in the Mat class are as follows:
Mat class Cv_exports mat{public://a series of functions/* includes several fields:-mat's identity-continuity flag-depth (bit depth)-Number of channels */int flags;//matrix dimension, Value > = 2int dims;// The number of rows and columns of the matrix, if the matrix exceeds 2 dimensions, the variable value rows = -1,cols = -1int rows,cols;//pointer to the data Uchar *data;//pointer reference counter;//the array points to user-assigned data when the pointer is Nullint * RefCount;//other Members};
Ii. Creating a Mat object
There are several ways to create a mat object.
2.1 Common Constructors
The parameterless constructor Mat::mat ();//create rows with row, number of columns is cols, image type is//type can be cv_8uc1,cv_16sc1, ..., CV_64FC4, etc. The 8U inside represents a 8-bit unsigned integer, 16S represents a 16-bit signed integer, and 64F represents a 64-bit floating-point number (that is, a double type)//c followed by numbers that represent the number of channels, for example C1 represents an image of a channel, C4 represents an image of 4 channels, and so on. If you need more channels, you need to use the macro C//v_8uc (n), for example://Mat M (3,2, Cv_8uc (5));//Create an image mat::mat (int rows,int cols,int type) with 3 rows, 2 columns and 5 channels; /Create an image with size of type Mat::mat (size size, int type);//Create an image with rows, number of columns cols, type types, and initialize all elements to Smat::mat (int rows, int cols, int type,const scalar& s);//Create an image of size of type, and initialize all elements to s mat::mat (size size, int type, constscalar& s) ;//assigns M to the newly created object, where the image data is not copied, and M and the new object share the image data Mat::mat (const mat& m);//Create an image with rows, columns of col, type This constructor does not create the memory required for image data, but instead directly uses the memory referred to by data, and the line step of the image is specified by step. Mat::mat (int rows, int cols, int type, void* data, size_t step=auto_step); Creates an image of size, type, which does not create the memory required for the image data, but instead uses the memory indicated by data directly, and the line step of the image is specified by step. Mat::mat (size size, int type, void* data, size_t step=auto_step);//The new image created is part of M, the specific range is specified by RowRange and Colrange, and this constructor does not enter Copy operation of image data, new image and M common image data; MaT::mat (const mat& m, const range& rowrange, const range& colrange);//Create a new image as part of M, specific range ROI designation, This constructor also does not perform the copy operation of the image data, and the new image is shared with the M image data. Mat::mat (const mat& m, const rect& ROI);
2.2 Create method
If the Create () function specifies the same parameters as before the image, no real memory request operation is made, and if the parameters are different, the index of the original data memory is reduced and the memory is re-requested. Use the method as shown in the following routines:
Mat M (2,2, CV_8UC3);//constructor creates image M.create (3,2, CV_8UC2);//frees memory to recreate image
When you need to be aware, you cannot set the initial value of an image pixel using the Create () function.
OPENCV Study Notes (6): Mat class/Data structure details (1)