Opencv deep learning (1)-Main member variables of Mat

Source: Internet
Author: User

I am determined to take a good look at opencv2.x, but now I have finished my thesis. I have some time to study it from the beginning!

Starting with the basic structure cv: Mat, we first analyzed the main member variables of Mat:

The following is the Mat statement in core. hpp:

class CV_EXPORTS Mat{public:    /** functions*/    enum { MAGIC_VAL=0x42FF0000, AUTO_STEP=0, CONTINUOUS_FLAG=CV_MAT_CONT_FLAG, SUBMATRIX_FLAG=CV_SUBMAT_FLAG };    /*! includes several bit-fields:         - the magic signature ---//Magic number-wikipedia         - continuity flag         - depth         - number of channels     */    int flags;    //! the matrix dimensionality, >= 2    int dims;    //! the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions    int rows, cols;    //! pointer to the data    uchar* data;    //! pointer to the reference counter;    // when matrix points to user-allocated data, the pointer is NULL    int* refcount;        //! helper fields used in locateROI and adjustROI    uchar* datastart;    uchar* dataend;    uchar* datalimit;        //! custom allocator    MatAllocator* allocator;        struct CV_EXPORTS MSize    {        MSize(int* _p);        Size operator()() const;        const int& operator[](int i) const;        int& operator[](int i);        operator const int*() const;        bool operator == (const MSize& sz) const;        bool operator != (const MSize& sz) const;                int* p;    };        struct CV_EXPORTS MStep    {        MStep();        MStep(size_t s);        const size_t& operator[](int i) const;        size_t& operator[](int i);        operator size_t() const;        MStep& operator = (size_t s);                size_t* p;        size_t buf[2];    protected:        MStep& operator = (const MStep&);    };        MSize size;    MStep step;};

Mat represents a multi-dimensional dense single-channel or multi-channel numeric array. It can be used to store real numbers, complex vectors, gray scales, color images, Vector Fields, point clouds, and so on. The data layout of array M is determined by the Parameter M. step [k] [the k dimension is not often used] determine the number of M (I _0 ,..., I _M.dims-1) the address of the element can be calculated as follows:

Addr (Mi_0 ,..., I _M.dims-1) = M. data + M. step [0] * I _0 + M. step [1] * I _1 +... + M. step [M. dims-1] * I _M.dims-1; [0 <= I _k <M. size [k] is the coordinate of the k dimension, ex. in a rows * cols = 300*200 2D image, the size of the 0th dimension is M. size: [0] = 300, M. size: [1] = 200, M. dims = 2. In this case, assuming the address of the value in the matrix (20, 30), the two indexes can be determined as: I _0 = 20, I _1 = 30, so that you can determine the M (20, 30) address ].
In the case of a 2-dimensional array, the formula for the above calculated address is changed to: addr (Mi, j) = M. data + M. step [0] * I + M. step [1] * j; note that M. step [I]> = M. step [I + 1] (in fact M. step [I]> = M. step [I + 1] * M. size [I + 1]). That is to say, the 2-dimensional matrix is stored in one row and one row, and the 3-dimensional matrix is stored in one side, so M. step [M. dims-1] is the smallest and must be equal to the size of the element M. elemSize (). [M. the elemSize () function returns the size of the byte space occupied by each element. If the element type is CV_16SC3, 3 * sizeof (short) is returned]
[Here we will explain the step size [k]. The step size can also be considered as the storage unit of the k-dimension. In the 2-dimensional matrix, because the storage is stored in the order of rows, and the entire matrix is stored as a plane, the step size of k = 0, that is, the Unit must be the number of bytes occupied by a row; for three dimensions, 0th dimensions are stored by plane, 1st dimensions are stored by behavior units, and 2nd dimensions are stored by element type units, each element type is the product of the basic type (uchar, float, short, etc.) and the number of channels ...;
That is, the basic data type and the number of channels constitute an element. Multiple elements form a row, multiple rows form a plane, and multiple faces form a three-dimensional body, multiple 3-dimensional bodies form a 4-dimensional hyperbody... Similarly, it seems that the step size of a certain dimension should be equal to the step size of the lower one dimension * the size of the lower one dimension, so> = what is going on?
This is the reason for byte alignment. In order to increase the computing speed, data storage is aligned according to the word length, which is 4-byte alignment in 32-bit machines, just like a row = 100, cols = 101-sized single-channel uchar image. If the bytes are aligned, the image is in step [0] = 104 instead of 101; ####-- when cv: Mat is used, if the data is loaded from the image, or the allocated data is created using constructors, create, and so on, there is no byte alignment. All the data is stored in sequence and is continuous, but if
(1) Use your own data and use the Mat header to access the data. If the specified step is not AUTO_STEP, it is not necessary to determine whether there is byte alignment, however, if step = AUTO_STEP is specified, there is no byte alignment. This can be seen from the introduction to step Parameters Using the constructor of external data in the manual.
(2) The image is loaded with IplImage and then converted to cv: Mat. There is a byte alignment phenomenon. The following is a test example of step and size, which can be verified from its output]
Mat load_mat = imread("d:/picture/temp1.bmp");cout<<"step[0]="<<load_mat.step[0]<<",size[0]="<<load_mat.size[0]<<",step[1]="<<load_mat.step[1]<<",size[1]="<<load_mat.size[1]<<endl;cout<<"rows="<<load_mat.rows<<",cols="<<load_mat.cols<<"elemSize="<<load_mat.elemSize()<<",continuous="<<load_mat.isContinuous()<<endl;cout<<"----------------------------------------------------"<<endl;Mat mem_mat(101,104, CV_8UC3, Scalar::all(255));cout<<"step[0]="<<mem_mat.step[0]<<",size[0]="<<mem_mat.size[0]<<",step[1]="<<mem_mat.step[1]<<",size[1]="<<mem_mat.size[1]<<endl;cout<<"rows="<<mem_mat.rows<<",cols="<<mem_mat.cols<<"elemSize="<<mem_mat.elemSize()<<",continuous="<<mem_mat.isContinuous()<<endl;cout<<"----------------------------------------------------"<<endl;int sz[]={101,101,101};Mat cube(3, sz, CV_32FC3, Scalar::all(0));cout<<"step[0]="<<cube.step[0]<<",size[0]="<<cube.size[0]<<",step[1]="<<cube.step[1]<<",size[1]="<<cube.size[1]<<endl;cout<<"step[2]="<<cube.step[2]<<",size[2]="<<cube.size[2]<<",step1*size1="<<cube.step[1]*cube.size[1]<<endl;cout<<"step2*size2="<<cube.size[2]*cube.step[2]<<",elemSize="<<cube.elemSize()<<",continuous="<<cube.isContinuous()<<endl;cout<<"rows="<<cube.rows<<",cols="<<cube.cols<<endl;cout<<"----------------------------------------------------"<<endl;uchar data[1212]={0};Mat createdmat(cv::Size(101,12),CV_8UC1, data, 104);cout<<"step[0]="<<createdmat.step[0]<<",size[0]="<<createdmat.size[0]<<",step[1]="<<createdmat.step[1]<<",size[1]="<<createdmat.size[1]<<endl;cout<<"rows="<<createdmat.rows<<",cols="<<createdmat.cols<<"elemSize="<<createdmat.elemSize()<<",continuous="<<createdmat.isContinuous()<<endl;cout<<"----------------------------------------------------"<<endl;Mat createdmat2(cv::Size(101,12),CV_8UC1, data);cout<<"step[0]="<<createdmat2.step[0]<<",size[0]="<<createdmat2.size[0]<<",step[1]="<<createdmat2.step[1]<<",size[1]="<<createdmat2.size[1]<<endl;cout<<"rows="<<createdmat2.rows<<",cols="<<createdmat2.cols<<"elemSize="<<createdmat2.elemSize()<<",continuous="<<createdmat2.isContinuous()<<endl;cout<<"----------------------------------------------------"<<endl;IplImage *image = cvLoadImage("d:/picture/temp1.bmp");cout<<"widthstep="<<image->widthStep<<",height="<<image->height<<",width="<<image->width<<endl;Mat cvted(image);cout<<"step[0]="<<cvted.step[0]<<",size[0]="<<cvted.size[0]<<",step[1]="<<cvted.step[1]<<",size[1]="<<cvted.size[1]<<endl;cout<<",step1*size1="<<cvted.step[1]*cvted.size[1]<<"elemSize="<<cvted.elemSize()<<",continuous="<<cvted.isContinuous()<<endl;cout<<"=========================================================="<<endl;cout<<"sizeof(loadmat)="<<sizeof(load_mat)<<",sizeof(memmat)="<<sizeof(mem_mat)<<",sizeof(cube)="<<sizeof(cube)<<endl;cout<<"sizeof(createdmat)="<<sizeof(createdmat)<<",sizeof(createdmat2)="<<sizeof(createdmat2)<<",sizeof(cvted)="<<sizeof(cvted)<<endl;cvReleaseImage(&image);
 
The output is as follows:
 
Combined with the above analysis, we can see that the first case is to use imread to load the image. Although the image's cols = 355, its step [0] = 1065 = 355*3 has no byte alignment, we can also see from the continuous = 1 that the second case below is also, the third case is the 3-dimensional matrix, and there is no byte alignment. Now we can see that cols = rows =-1; in the fourth case, use external data and specify step = 104 for byte alignment. We can see that step [0]! = Step [1] * size [1], continuous = 0, but in the fifth case, when step uses the default AUTO_STEP, there is no byte alignment. In the sixth case, when IplImage is used to load the same image as the first one, we can see that byte alignment is used, and its widthstep is equal to the value of step [0] After the Mat is converted, greater than step [1] * size [1], continuous = 0, which also verifies the above analysis.
Several other common member variables:
--------------------------------------------------
Int flags; flag bit, which contains several bit fields: -- magic signature or, more specifically, the file signature, used to distinguish between different file formats, is the "ID card" of the file. Some clues can be found in the Mat constructor. In the Mat constructor constructed using external data: flags (MAGIC_VAL + (_ type & TYPE_MASK )), in the default constructor: flags (0), the corresponding settings should also be made during imread image loading. [no confirmation? ], For details about the digital signature, refer to the Magic number entry in WikiPedia-whether the continuity flag is continuously stored, and whether the byte alignment is used-the depth of the depth element is the basic element type, uchar, short, float, etc. -- number of channels --------------------------------------------------
Cols, number of rows in the rows matrix, and number of columns [Note: the number of rows in the image corresponds to the height, and the number of columns corresponds to the width]. When the dimension is greater than 2, all rows are-1;
Dimensions of the dims matrix;
Uchar * data; address pointer for storing specific data.

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.