OpenCV basics-Mat data structure and opencv Basics
Procedure and Analysis
/* * FileName : MatObj.cpp * Author : xiahouzuoxin @163.com * Version : v1.0 * Date : Thu 15 May 2014 09:12:45 PM CST * Brief : * * Copyright (C) MICL,USTB */#include <cv.h>#include
Mat is the most basic data structure of OpenCV. Mat is the abbreviation of Matrix. Mat data structure consists of two parts: Header and Pointer. The Header contains information such as the size, storage method, and address of the matrix. The Pointer stores the Pointer to the pixel value. When reading an image, we define the image as the Mat type, and there are a lot of overloaded constructors,
class CV_EXPORTS Mat{public: //! default constructor Mat(); //! constructs 2D matrix of the specified size and type // (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.) Mat(int _rows, int _cols, int _type); Mat(Size _size, int _type); //! constucts 2D matrix and fills it with the specified value _s. Mat(int _rows, int _cols, int _type, const Scalar& _s); Mat(Size _size, int _type, const Scalar& _s); //! constructs n-dimensional matrix Mat(int _ndims, const int* _sizes, int _type); Mat(int _ndims, const int* _sizes, int _type, const Scalar& _s); //! copy constructor Mat(const Mat& m); //! constructor for matrix headers pointing to user-allocated data Mat(int _rows, int _cols, int _type, void* _data, size_t _step=AUTO_STEP); Mat(Size _size, int _type, void* _data, size_t _step=AUTO_STEP); Mat(int _ndims, const int* _sizes, int _type, void* _data, const size_t* _steps=0); //! creates a matrix header for a part of the bigger matrix Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all()); Mat(const Mat& m, const Rect& roi); Mat(const Mat& m, const Range* ranges); //! converts old-style CvMat to the new matrix; the data is not copied by default Mat(const CvMat* m, bool copyData=false); //! converts old-style CvMatND to the new matrix; the data is not copied by default Mat(const CvMatND* m, bool copyData=false); //! converts old-style IplImage to the new matrix; the data is not copied by default Mat(const IplImage* img, bool copyData=false); ...... }
To understand how to initialize the Mat structure, you should understand its constructor. For example, the number of calls in the first Initialization Method in the program is
Mat(int _rows, int _cols, int _type, const Scalar& _s);
This constructor.
IplImage * is the data structure used to operate OpenCV in C language. When C was operating OpenCV at that time, it was equivalent to Mat, and OpenCV provided an interface for it, it is very convenient to directly convert IplImage to Mat, that is, using the constructor
Mat(const IplImage* img, bool copyData=false);
The second method in the above program is to use this constructor.
About Mat data replication: As mentioned earlier, Mat includes headers and data pointers. When the Mat constructor is used for initialization, the headers and data pointers are copied (Note: Only pointer replication, the address to which the Pointer Points will not be copied). To copy data, you must use the copyTo or clone function.
Mat also has several common member functions, which will be used in subsequent articles:
//! returns true iff the matrix data is continuous// (i.e. when there are no gaps between successive rows).// similar to CV_IS_MAT_CONT(cvmat->type)bool isContinuous() const;
Before learning about the functions above, we can find out how to store pixels in OpenCV. The following shows how to store grayscale images (Single Channel) by row and column,
The three-channel RGB storage method is as follows. Each column contains three channels,
To speed up access, openCV stores pixel data in a row continuously in the memory. The isContinus () function is used to determine whether to store data in a row consecutively. What are the benefits of storing a row? Given the header pointer p of this row, you only need to use the p ++ operation to access data one by one.
Therefore, when determining whether the data is stored in a row, you can easily traverse the image pixels through the Data Pointer ++:
Long nRows = M. rows * M. channels (); // channels () is also a common function in Mat, used to obtain the number of channels (RGB = 3, gray = 1) long nCols = M. cols; uchar * p = M. data; // data Pointer if (M. isContinuous () {nCols * = nRows; for (long I = 0; I <nCols; I ++) {* p ++ = ...; // pixel value assignment or read operation }}
Note the following commonly used Mat member traversal and functions:
M. row; // number of returned image rows M. nCols; // number of returned image columns M. channels (); // number of returned channels M. isContinuous (); // The returned bool type indicates whether to store continuously