Opencv's understanding of depth, dims, channels, step, Data, elemsize, and data address computing in mat

Source: Internet
Author: User

Http://ggicci.blog.163.com/blog/static/210364096201261052543349/

Title:

  • CV: mat
  • Depth/dims/channels/step/data/elemsize
  • The class mat represents an N-dimen=dense numerical single-channel or multi-channel array. it can be used to store (MAT class objects are used to represent a multi-dimensional single-channel or multi-channel dense array, which can be used to store the following things)

    • Real or complex-valued vectors or matrices (real or composite value vectors and matrices)
    • Grayscale or color images (grayscale or color)
    • Voxel volumes (Stereo element)
    • Vector Fields)
    • Point Clouds)
    • Tensors (Tensor)
    • Histograms (though, very high-dimen1_histograms may be better stored in a sparsemat) (histogram, preferably stored in sparsemat at high latitudes)
  • The C struct in the old version of opencv contains cvmat and cvmatnd. Currently, I use Version 2.3. The document in it indicates that cvmat and cvmatnd are discarded and replaced by mat in the C ++ encapsulation, in addition, the old version also has an iplimage, which is also replaced by mat (refer to the structure, class, and emgu in blog opencv. the table corresponding to the CV ).
  • Formula for Calculating the address of data elements in a matrix (m:

ADDR (MI0, I1 ,... IM-1)
= M. Data + M. Step [0] * I0 + M. Step [1] * I1 +... + M. Step M-1] * IM-1 (M = M. dims M dimension)

    • Data: A pointer in the mat object pointing to a memory (uchar * data) in which the matrix data is stored)
    • Dims: the dimension of the matrix represented by mat. For example, a matrix of 3*4 is two dimensions, and a matrix of 3*4*5 is three dimensions.
    • Channels: channel. The number of values owned by each matrix element in the matrix. For example, there are 12 elements in the 3*4 matrix. If each element has three values, in this case, the matrix is 3 channels, that is, channels = 3. Generally, a color image has three channels: Red, green, and blue.
    • Depth: depth, that is, the bits of each pixel, In the mat of opencv. in depth (), A number ranging from 0 to 6 represents different 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 represent 8 bits, 2 and 3 represent 16 bits, 4 and 5 represent 32 bits, and 6 represent 64 bits;
    • Step: it is an array that defines the layout of the matrix. For details, see the image analysis below. Also, pay attention to Step 1 (STEP/elemsize1), M. step M-1] is always equal to elemsize, M. step 1 (S-1) is always equal to channels;
    • Elemsize: The data size of each element in the Matrix. If the data type in mat is cv_8u, elemsize = 1, cv_8uc3, elemsize = 3, cv_16uc2, and elemsize = 4; remember, another elemsize1 represents the size of the Data Type in the matrix, that is, elemsize/channels.
      Size

Image Analysis 1: Two-Dimensional storage (stored row by row)

The above is a matrix of 3x4. Assume that the data type is cv_8u, that is, the uchar type of a single channel.

  • This is a two-dimensional matrix, so the dimension is 2 (M. dims = 2 );
  • M. rows = 3; M. Cols = 4;
  • Sizeof (uchar) = 1, then the size of each data element is 1 (M. elemsize () = 1, M. elemsize1 () = 1 );
  • Cv_8u to obtain M. Depth () = 0, M. channels () = 1;
  • Because it is a two-dimensional matrix, the step array has only two values. Step [0] And step [1] represent the data size of a row and the data size of an element respectively, then M. step [0] = 4, M. step [1] = 1;
  • M. Step1 (0) = M. Cols = 4; M. Step1 (1) = 1;

Assume that the above matrix data type is cv_8uc3, that 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 () = 12, 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: 3D storage (stored plane by plane)

The above is a matrix of 3x4x6. Assume that the 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 (first dimension (number of elements) * number of channels );
  • M. step 1 (1) = m. step [1]/m. elemsize () = 12/2 = 24 (second dimension (number of elements in the row/column width) * number of channels );
  • M. step 1 (2) = m. step [2]/m. elemsize () = m. channels () = 4 (third dimension (element) * number of channels );

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.