The msize class and mstep class are embedded classes of MAT:
class CV_EXPORTS Mat{public: ...... struct CV_EXPORTS MSize { MSize(int* _p); ...... int* p; }; struct CV_EXPORTS MStep { MStep(); ...... size_t* p; }; MSize size; MStep step; ......};
Size and step member variables to facilitate matrix access. The size records the dimension of each dimension in the matrix, and the step records the data volume in each dimension. The following is a detailed analysis:
How to store matrices:
A one-dimensional matrix is very simple, that is, an array. A two-dimensional matrix can be seen as an array, that is, every element of an array is an array. A three-dimensional matrix can be seen as a two-dimensional matrix of an array, that is, every element of a two-dimensional matrix is an array. Likewise, four-dimensional ,....., n-dimensional. As shown in:
The above matrix is only a formal representation of a matrix in the human brain. In computer systems, there is only one representation in memory, whether it is a one-dimensional array, two-dimensional matrix, three-dimensional or n-dimensional, that is:
Shows how to store arrays of different dimensions in memory:
Therefore, for a matrix, dimension information is required. In the mat class, the dimension information of the matrix is stored in the size of the member variable. Another big question is how to locate the elements in the matrix and determine the step size between dimensions!
Dimension |
Representation Method |
Representation after compiler Processing |
1 |
A [I] |
A [I] |
2 |
A [I] [J] |
A [I * len2 + J] |
3 |
A [I] [J] [k] |
A [I * len2 * len3 + J * len3 + k] |
... |
... |
... |
N |
A [I] [J] [k]... [w] |
A [I * len2 * len3 *... * Lenn +... w] |
Leni indicates the length of dimension I.
A simple explanation of the preceding table is as follows:
One-dimensional self Needless to say; two-dimensional space, one-dimensional each "slot" contains len2 elements; three-dimensional space, one-dimensional each slot contains len2 x len3 elements. Therefore, taking positioning in three-dimensional space (I, j, k) as an example, combined with the storage mode of the matrix in the memory, I X len2 x len3 first located the position of the point in the one-dimensional space, then, J x len3 locates the point in the two-dimensional space, and K locates the point in the three-dimensional space. The above analysis shows that len2 x len3 is in one-dimensional space.Step size between two adjacent "slots"Len3 is the step of two adjacent "slots" in two-dimensional space. The two processes are as follows,
The step size between two adjacent "slots" in different dimensions is very important. In the mat class, the step size is stored as a step data member and can be conveniently programmed. Below is the PTR function of the three-dimensional matrix in the mat class (this function obtains the first address of the three-dimensional data through the 1st and 2 dimensional coordinates)
template<typename _Tp> inline const _Tp* Mat::ptr(int i0, int i1) const{ return (const _Tp*)(data + i0*step.p[0] + i1*step.p[1]);}
In the above program, data is the first address of the matrix, where step. P [0] stores the step of the First-dimensional space slot, step. P [1] stores the step of the Second-dimensional space slot. Data + I0 * step. P [0] + I1 * step. P [1] operation, Data Pointer from first address of matrix --> first address of two-dimensional space --> first address of three-dimensional space. Then, you can use (PTR (I, j) [k] to obtain matrix elements.
In summary, the step size can be defined as follows:: For n-dimensional matrix m, the I-dimensional space step is the product of the length of each dimension data after I. Step 1 = len2 × len3 ×... × Lenn, step 2 = len3 ×... × Lenn.
The above analysis is verified through the initialization process of size and step:
For (I = _ dims-1; I> = 0; I --) // {int S = _ SZ [I]; // _ SZ [] storage matrix dimension size M. size. P [I] = s; // initialize the size ...... {M. step. P [I] = total; // initialize step (pay attention to the total calculation method! Is the definition of the above step !) Int64 total1 = (int64) Total * s; // I * len3 * len2 + J * len3 Total = (size_t) total1 ;}......}
In summary, the step members in the mat class store the step size of the dimension, and the size members store the dimension size.