OPENCV Data Structures-mat structure detailed

Source: Internet
Author: User

1, the definition of OpenCV C structure has Cvmat and cvmatnd, but the subsequent application of the Cvmat and Cvmatnd deprecated, in the C + + package with mat, the other old version there is a iplimage, the same mat instead (you can refer to the blog OpenCV The corresponding table of the struct, class, and EMGU.CV in the
The address calculation formula for the data element in the matrix (M):
Addr (mi0,i1,... im-1) = M.data + m.step[0] * i0 + m.step[1] * i1 + ... + m.step[m-1] * im-1 (where m = m.dims m of dimensions)

Cv::mat
Depth/dims/channels/step/data/elemsize
The class Mat represents an n-dimensional dense numerical single-channel or multi-channel array. It can be used to store (the Mat class object is used to represent a multidimensional single-channel or multi-channel dense array, which is used to store the following things)


Real or complex-valued vectors or matrices

Real value or compound value vector, matrix)

(Grayscale or color images

(grayscale or color map)

Voxel volumes

(stereoscopic Element)

Vector fields

(vector field)

Point clouds

(Point Cloud)

tensors

(tensor)

Histograms (though, very high-dimensional histograms May is better stored in a sparsemat)

(histograms, preferably at high latitude, are stored in Sparsemat)


A pointer in an older version of the Data:mat object, pointing to a piece of memory that holds the matrix data (uchar* data)
The dimensions of the matrix represented by Dims:mat, such as The Matrix of 3 * 4, are 2 dimensions, 3 * 4 * 5 are 3 dimensions
Channels: Channel, the number of values that each matrix element in the matrix has, such as a total of 12 elements in the 3 * 4 matrix, if each element has three values, then it is said that the matrix is 3 channels, i.e. channels = 3. Common is a color picture of red, green, blue three channels.
Depth: depth, which is the number of bits per pixel (bits), in OpenCV's Mat.depth () is a 0–6 number that represents a different number of digits: enum {cv_8u=0, Cv_8s=1, cv_16u=2, cv_16s=3, cv_ 32s=4, cv_32f=5, cv_64f=6}; Visible 0 and 1 both represent 8-bit, 2 and 3 represent 16-bit, 4 and 5 for 32-bit, 6 for 64-bit;
Step: is an array, defines the layout of the matrix, see the following image analysis, in addition to note Step1 (STEP/ELEMSIZE1), m.step[m-1] always equals ELEMSIZE,M.STEP1 (m-1) always equals channels;
Elemsize: The data size of each element in the matrix, if the data type of the data in the mat is cv_8u then elemsize = 1,cv_8uc3 Then elemsize = 3,cv_16uc2 then elemsize = 4; Remember there's another ele. MSIZE1 represents the size of the data type in the matrix, that is, the size of the Elemsize/channels
Image Analysis 1: Consider a two-dimensional case (stored row by row) storage by row

Above is a 3 X 4 matrix, assuming its data type is cv_8u, that is, the single-channel UCHAR type
This is a two-dimensional matrix, then the dimension is 2 (m.dims = = 2);
M.rows = = 3; M.cols = = 4;
sizeof (UCHAR) = 1, then each data element size is 1 (m.elemsize () = = 1, m.elemsize1 () = = 1);
Cv_8u get m.depth () = = 0, m.channels () = = 1;
Because it is a two-dimensional matrix, then the step array has only two values, step[0] and step[1] respectively represents the data size of a row and the data size of an element, then m.step[0] = = 4, m.step[1] = = 1;
M.STEP1 (0) = = M.cols = 4; M.STEP1 (1) = = 1;
Assuming that the matrix data type above is CV_8UC3, which is three channels
M.dims = = 2; M.channels () = = 3;m.depth () = = 0;
M.elemsize () = = 3 (each element contains 3 Uchar values) m.elemsize1 () = = 1 (elemsize/channels)
M.step[0] = = M.cols * M.elemsize () = =, m.step[1] = = M.channels () * M.ELEMSIZE1 () = = m.elemsize () = = 3;
M.step (0) = = M.cols * M.channels () = = 12; M.step (1) = = M.channels () = = 3;
Image Analysis 2: Consider three-dimensional conditions (stored plane by plane) storage by face

Above is a 3 x 4 x 6 matrix, assuming its data type is CV_16SC4, that is, the short type
M.dims = = 3; M.channels () = = 4; M.elemsize1 () = = sizeof (short) = = 2;
M.rows = = M.cols ==–1;
M.elemsize () = = M.elemsize1 () * m.channels () = = m.step[m.dims-1] = = m.step[2] = = 2 * 4 = = 8;
M.step[0] = = 4 * 6 * m.elemsize () = = 192;
M.STEP[1] = = 6 * M.elemsize () = = 48;
M.STEP[2] = = m.elemsize () = = 8;
M.STEP1 (0) = = m.step[0]/m.elemsize () = = 48/2 = = 96 (the first dimension (that is, the number of elements in the polygon) * number of channels);
M.STEP1 (1) = = M.step[1]/m.elemsize () = = 12/2 = = 24 (second dimension (number of elements/column width) * number of channels);
M.STEP1 (2) = = M.step[2]/m.elemsize () = = M.channels () = = 4 (third dimension (i.e. element) * channel number);

OPENCV Data Structures-mat structure detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.