Simple concepts that must be understood in opencv
1. Matrix header and matrix data space
We can declare a matrix header using mat-like statements in opencv, but the matrix header does not contain the data in the matrix, the data in the matrix is stored in the position pointed to by the mat member data. Opencv automatically manages matrix data through reference counting. For example, if a new matrix header points to the matrix data space, the reference counting automatically increases, if a matrix header pointing to a matrix data space points to another location or is destructed, the reference count is automatically reduced. When the reference count is reduced to 0, no matrix header points to the space, opencv Automatically releases the data space.
Similarly, when we use the value assignment operation to copy a matrix, the matrix data is not actually copied. It only adds the reference count for the matrix data space to indicate that the new matrix header points to the same data space. If you need to assign a value to the matrix data matrix at the same time, you can use the mat: Clone () function for in-depth copying.
2. Matrix Element type
Single Channel: if each element of the matrix is a scalar, the matrix is called a single channel. For a single-channel matrix, we can use the following enumerated variable to represent the element type.
Enum {cv_8u = 0, cv_8s = 1, cv_16u = 2, cv_16s = 3, cv_32s = 4, cv_32f = 5, cv_64f = 6 };
CV is the abbreviation of computer vision. digits 8, 16, and 32 indicate the digits of an element. Letters U, S, and f indicate the first letter of unsigned signed and float, respectively.
For example, cv_32s indicates that the element type is 32-bit signed integer, and cv_64f indicates 64-bit floating point.
Multi-channel: if each element in the matrix is a vector, the matrix is called multi-channel. For multi-channel matrices, we can use the following constants to represent
Cv_8uc1 cv_8uc2... cv_64fc3 cv_64fc4
Cv_8uc (n)... cv_64fc (N)
The preceding constants except C (n) have the same meaning as those in the single-channel matrix. The added constants c indicate the channel and N indicate the number of channels.
For example, cv_32fc2 indicates dual-channel 32-bit floating point data.
3. Arrange matrix data in space
Is the matrix obtained by reading a colored image. We can see how a 3-Channel Two-dimensional matrix stores data in the memory space, note that each pixel is discharged according to BGR, rather than RGB.
As shown in, the matrix data is arranged by security lines. The last column of row0 joins the first column of row1.
4. Matrix continuity
The so-called matrix continuity refers to whether the elements in the matrix are in a continuous space in space. For the new matrix created by the mat class constructor or the CREATE () function, the system allocates a continuous data space. Such a matrix must be continuous. However, for matrices created by functions such as mat: COL (), mat: colrange (), mat: diag, we can see from the matrix header and the data space of the matrix described above, the arrangement of matrix data in the space, and other knowledge, such a matrix does not allocate a new data space but indexes the original data space. Therefore, if the elements pointed to by the matrix header are separated from the original data space, the data of the matrix is not consecutive in space.
We can use the mat: iscontinuous () function to determine whether the matrix is continuous.