[c++] Opencv Mat class details

1. Mat class common member functions and member variables

Because Mat class is widely used and has many forms, only the more commonly used member functions and member variables are sorted out here.

1.1 Constructor

(1) Default constructor

cv::Mat::Mat()

Default constructor: Generate a matrix and allocate storage using functions provided by OpenCV (generally Mat::create() and cv::imread()).

The Mat class can be divided into two parts: a matrix header and a matrix pointer to pixel data

Matrix Header: The size of the matrix header, including the size, storage method, storage address, and number of references to the digital image, is a constant and does not change with the size of the image. However, the matrix that stores the pixel data of the image changes with the size of the image, usually with a large amount of data, several orders larger than the matrix header. In this way, the main overhead in image copying and transfer is caused by the matrix holding the image pixels. Therefore, OpenCV uses the number of references, so when copying and transferring images, it no longer copies the entire Mat data, but just copies the matrix header and pointers to the pixel matrix, for example:

cv::Mat a ;                 //Default constructor, create matrix header
a = cv::imread("test.jpg"); //Read in the image, the matrix pointer points to the pixel data
cv::Mat b = a ;             //Copy

The A and b above have their own matrix headers, but their matrix pointers point to the same matrix, that is, any change in the matrix data will affect the other. So, if multiple Mat s share a single matrix data, who will release the matrix data in the end?
This is what reference counts do. Each time a Mat object is copied, it adds a reference count of 1. Each time a Mat object is destroyed (sharing the same matrix data), the reference count is reduced by 1. When the reference count is 0, the matrix data is cleaned up.

(2) Common Constructors - 1

cv::Mat::Mat(int rows,int cols,int type)

Overloaded constructor, which is also one of the common constructors, provides the size of the matrix (rows, rows, cols, columns) and storage type (type) while creating objects
This type represents the storage type of each element in the matrix in computer memory, such as CV_8UC3, meaning "3-channel 8-bit unsigned number".

eg:

Mat src(10,10,CV_32FC3);  //Represents that the src is a 10*10 matrix with matrix elements stored in 32-bit float

Similarly, OpenCV provides a Size() data structure to construct Mat objects

(3) Common Constructors--2

cv::Mat::Mat(Size size,int type )   

The Size class is equivalent to a paired data, size::Size(cols,rows), paying particular attention to the location of cols and rows

eg:

Mat src1(3, 4, CV_32FC3);
Mat src2(Size(3, 4), CV_32FC3);
cout << "src1.rows=" << src1.rows << " src1.cols=" << src1.cols <<endl;
cout << "src2.rows=" << src2.rows << " src2.cols=" << src2.cols << endl;
cout << "src1.size="<<src1.size() << endl <<"src2.size=" << src2.size() <<endl;

It must be said that the data structure of this Size class is somewhat "anti-human", but the advantage is that it facilitates internal computer operations (e.g. many OpenCV functions calculate Size-related data in this order, and I don't know why, personally, industry standards);

Also, the resolution we usually talk about is the type of Size, such as 1440*900 screen rate, where cols=1440 and rows=900;

(3) Common Constructors--3

cv::Mat::Mat(int ndims,const int *  sizes,int type,const Scalar& s) 

This constructor uses the Scalar parameter to initialize element values through the Scalar data class, for example, to generate a picture with a white background:

Mat src1(300, 400, CV_8UC3,Scalar(255,255,255));
imshow("test", src1);

Among them, (255,255,255) corresponds to the white value of the RGB color gamut stored in 8-bit unsigned numbers.

(5) Common constructors--4

cv::Mat::Mat(const Mat & m)

Referencing the m-matrix, note that this is the reference value;

1.2 Member Functions

(1) at function
The function of the at function is to access matrix elements, and depending on the usage scenario, there are several overloaded functions to choose from.
For example, to access a two-dimensional matrix, the at function can be prototyped as:

_Tp& cv::Mat::at(int i0,int i1)

eg:

Mat src = imread("test.jpg");
int elem = src.at<int>(0,0);

Access test. Element (0, 0) of a JPG image

(2) channels function

int cv::Mat::channels()  const

Number of channels to return images

(3) clone function

Mat cv::Mat::clone()    const

Matrix Replication

(4) convertTo function

void cv::Mat::convertTo(OutputArray m,int rtype,double alpha = 1,double beta = 0)   const

Convert the storage type of matrix with the following formula:

m(x,y)=saturate_cast<rType>(α(∗this)(x,y)+β)

m is the input matrix, rtype is the target type, alpha is the zoom factor, beta is the increment and decrement scalar

(5) CopTo function

void cv::Mat::copyTo(OutputArray    m)  const

Copying data data units from an m-matrix is similar to the clone function

(6) create function

void cv::Mat::create(int rows,int cols,int type) 

The storage unit for the allocation matrix, used in conjunction with the default constructor

(7) depth function

int cv::Mat::depth()    const

Returns the image depth, which is how matrix elements are stored

(8) diag function

Mat cv::Mat::diag(int d = 0)  const

Extracting Diagonal Elements of a Matrix

(9) mul function

MatExpr cv::Mat::mul(InputArray m,double scale = 1)    const

Multiplication of Matrix

(10) inv function

MatExpr cv::Mat::inv(int method = DECOMP_LU)   const

Inverse Matrix

(11) t-function

MatExpr cv::Mat::t() const

Find the transpose matrix
(12) total function

size_t cv::Mat::total() const

Returns the total number of elements of a matrix, such as a 30*40 image, with 1200 pixels

(13) pop_back function

eg:

object.pop_back();	//An object is a matrix, and this function functions to pop up the last row of elements

2.3 Member Variables

int cv::Mat::cols;     //Returns the number of columns of a matrix

int cv::Mat::rows      // Returns the number of rows of a matrix

uchar* cv::Mat::data   // Pointer to data unit of matrix

int cv::Mat::dims      // Returns the matrix dimension, which is greater than or equal to 2

MatSize cv::Mat::size  // Return Matrix Size

About opencv, I am in the initial stage of learning, in the process of learning Mat class is integrated, supplemented, and will continue to improve;

Keywords: C++ OpenCV

Added by Static_Nexus on Sat, 18 Dec 2021 02:22:18 +0200