The constructor of the mat class. destructor:
There are too many constructor and destructor for the mat class, which is understandable. A matrix is the basic data structure of an image. for digital image processing personnel, a matrix is an important angle for us to look at the image, or even a major angle, because, almost all the actions we do are performed on the basis of a matrix! The matrix is so basic that there is a long way to go. Therefore, the implementation of the matrix class cannot be slowed down. In specific applications, sometimes we directly declare a matrix, such as mat IMG. Sometimes, we need to define the dimension, length, width, data type, and other information of the matrix in detail: mat IMG (width, height, cv_8u); this directly leads to a large constructor Group of the mat class, as shown below:
Mat::Mat() Mat::Mat(int rows, int cols, int type) Mat::Mat(Size size, int type) ..... Mat::Mat(int ndims, const int* sizes, int type, void* data, const size_t* steps=0) Mat::Mat(const Mat& m, const Range* ranges)
A total of 20 functions should not be afraid of these functions. Let's look at the definition part of the constructor. In fact, these functions basically call the following create function family:
void create(int rows, int cols, int type); void create(Size size, int type); void create(int ndims, const int* sizes, int type);
For example, in the constructor mat: MAT (INT rows, int cols, int type:
inline Mat::Mat(Size _sz, int _type) : size(&rows){ initEmpty(); create( _sz.height, _sz.width, _type );}
So the essence of the entire mat constructor is the create function, and then dig a deeper layer of the create function to see how the create function is implemented and remove the small details of some boundary conditions of the create function, the core part is as follows:
Void mat: Create (int d, const int * _ sizes, int _ type) // first and then column {If (! Allocator) // if the current matrix does not have a memory configurator {// step. P [0] and size. P [0] is worth mentioning. This next blog of the mat source code analysis will analyze in detail. // The refcount part is the reference count, which is an important technology in C ++. It indicates that the memory of this section is referenced. It is very obvious that this is the c ++ opencv size_t totalsize = alignsize (step. P [0] * size. P [0], (INT) sizeof (* refcount); (total space size of matrix data, 4-byte alignment) Data = datastart = (uchar *) fastmalloc (totalsize + (INT) sizeof (* refcount); refcount = (int *) (Data + totalsize); * refcount = 1 ;} else // if the current matrix has a memory configurator, use the memory configurator to allocate memory {Allocator-> allocate (dims, size, _ type, refcount, datastart, Data, step. p); cv_assert (step [dims-1] ==( size_t) cv_elem_size (flags ));}}
In summary, the mat constructor completes initialization of some member variables in the mat structure and memory allocation. It is a regular constructor.
In this section, it is necessary to describe the alignsize function, which involves memory problems. The specific point is the number of memory bytes. The source code is as follows:
/*! Aligns buffer size by the certain number of bytes This small inline function aligns a buffer size by the certian number of bytes by enlarging it.*/static inline size_t alignSize(size_t sz, int n){ return (sz + n-1) & -n;}
This bit operation function is a very beautiful function in the computer. It perfectly utilizes the limited number of digits of Data Types in the computer. The source code comment shows that this function is aligned with n Bytes, A Brief Analysis: In alignsize,-N is a negative number, and a negative number is indicated by a complementary code. The complement code of a negative number is the bitwise value of all BITs in its absolute value, and 1 is reversed, therefore, for n = 4 in this example, the-N Bit mode is 111 ...... 11100, the minimum two digits are 0, that is, 4-byte alignment can be achieved, so (SZ + N-1) &-N can achieve the following4 bytes rounded up and aligned.