Mat object in Opencv-image data structure

Preface

The Mat object is a memory object that is loaded or all in, what exactly is it?

1. What is the target?

An object is a collection that can have properties and methods

2. How does the Mat object look at the image?

In the eyes of normal people, he is a picture;
in my eyes, he is my idol;
in the computer, he will be parsed as a two-dimensional array;

Insert picture description here

2. The source of the Mat object?

How did we say the Mat library appeared? In the version of opencv1.ji, the processing of images was implemented using the IplImage library (in the style of C language), but soon, the memory leak problem appeared;
on this basis, the version of opencv2.ji introduced For object-oriented programming, Mat objects are data structures in C++ style.

Three, the constructor of the Mat object:

1) Briefly introduce the constructor and destructor:

      3.1.1. Definition of constructor: Constructor is a special method . It is mainly used to initialize the object when it is created, that is, to assign initial values ​​to the object member variables. It is always used with the new operator in the object creation statement. A class can have multiple constructors, which can be distinguished based on the number of its parameters or the types of parameters.

      3.1.2. Definition of destructor: Destructor is the opposite of constructor . When an object ends its life cycle, such as the function of the object has been called, the system automatically executes the destructor. The destructor is often used to "clean up the aftermath" (for example, when creating an object, use new to open up a piece of memory space, and delete will automatically call the destructor to release the memory).

2) The constructor of the Mat object:
      3.2.1. The parameterless constructor of the Mat object:

Mat::Mat();
//生成一个矩阵并由OpenCV提供的函数(一般是Mat::create() 和 cv::imread() )来分配储存空间。
//Mat类可以分为两个部分:矩阵头和指向像素数据的矩阵指针

      We turn to the definition of the construction method: you can see the       meaning of the comment in
Insert picture description hereChinese is as follows:
      @param rows The number of rows in the two-dimensional array.
      @param cols represents the number of columns in the two-dimensional array.
      @param type array type. Use CV_8UC1,...,CV_64FC4 to create a matrix of 1-4 channels,
      or CV_8UC(n),...,CV_64FC(n) to create a matrix with multiple channels (up to CV_CN_MAX channels).

      3.2.2. The Mat object uses the row (rows), column (cols), and type (type) constructors:

Mat::Mat(int rows, int cols, int type);

      3.2.3. The Mat object uses the size and type constructors:

Mat::Mat(Size size, int type);

      3.2.4. The Mat object uses row (rows), column (cols), type (type), and Scalar scalar constructors:

Mat::Mat(int rows, int cols, int type, const Scalar& s);

      3.2.5. The Mat object uses the size, type, and Scalar scalar constructor:

Mat::Mat(Size size, int type, const Scalar& s);

      3.2.6. The Mat object uses size to specify the integer array and type constructor of the n-dimensional array shape:

Mat::Mat(int ndims, const int* sizes, int type);
//ndims数组维数

      3.2.7. The Mat object uses the dynamic integer array and type constructor specified by size:

Mat::Mat(const std::vector<int>& sizes, int type);
//vector,传入动态整数数组,在使用前,需导入#include <vector>

      3.2.8. The Mat object uses size to specify the integer array, type, and Scalar vector constructor of the n-dimensional array shape:

Mat::Mat(int ndims, const int* sizes, int type, const Scalar& s);

      3.2.9. The Mat object assigns m to the newly created object, without copying the image data, sharing the image data, which belongs to the shallow copy constructor:

Mat::Mat(const Mat& m);

      3.2.10. The Mat object uses the constructors for rows, cols, type, data pointers and the number of bytes occupied by matrix rows:

Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP);

/*@param size 2D array size:大小(列、行)。在Size()构造函数中,行数和列数按相反顺序排列。
@param type数组类型。使用CV_8UC1,…,CV_64FC4创建1-4个通道矩阵,或CV_8UC(n),…,CV_64FC(n)创建多通道(最多CV_CN_MAX通道)矩阵。
@param data指向用户数据的指针。接受数据和步长参数的矩阵构造函数不分配矩阵数据。相反,它们只是初始化指向指定数据,这意味着没有数据被复制。此操作非常有效,可用于使用OpenCV函数处理外部数据。外部数据不会自动释放,因此你应该照顾好它。
@param step每个矩阵行占用的字节数。该值应包括以下位置的填充字节:每行的末尾(如果有)。如果缺少参数(设置为AUTO\u STEP),则假定没有填充实际步长计算为cols*elemSize()。请参阅Mat::elemSize。
此构造函数不创建图像数据所需内存,而是直接使用data指针,图像的行步长由 step指定。
*/

      3.2.11. The Mat object uses the constructor of size, type, data pointer and the number of bytes occupied by matrix rows:

Mat::Mat(Size size, int type, void* data, size_t step=AUTO_STEP);

      3.2.12. The Mat object will newly create a part of m, the constructor of the range specified by roi:

Mat::Mat(const Mat& m, const Rect& roi);

3) These are the various constructors that make up the matrix. As described in Auto Positioning, usually the default constructor is sufficient, and the appropriate matrix will be allocated by the OpenCV function. The constructed matrix can be further allocated to another matrix or matrix expression, or can be allocated with Mat::create. In the former case, the old content is dereferenced.

4) The most commonly used Mat construction methods are as follows:

Mat::Mat();
Mat::Mat(int rows,int cols,int type);
Mat::Mat(Size size,int type ) ;
Mat::Mat(int ndims,const int *  sizes,int type,const Scalar& s) ;
Mat::Mat(const Mat & m);
//如有需要可自行学习其他构造函数

Four, examples of Mat object constructor:

Code block:

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main() {
    
    
	
	Mat src = imread("C:\\Users\\ASUS\\Desktop\\3.png");
	namedWindow("input_Image_Windows", WINDOW_AUTOSIZE);
	imshow("input_Image_Windows", src);

	//Mat dst = Mat();//无参数构造函数使用
	Mat dst = src;
	namedWindow("output_Image_Windows", WINDOW_AUTOSIZE);
	imshow("output_Image_Windows", dst);
	
	waitKey(0);
	return 0;
}

operation result:
Insert picture description here

Five, the member function of the Mat object:

1. The clone function, which copies a matrix to another matrix;
2. The copyTo function, which is similar to the previous one;
3. The Create function, which creates a new matrix;
4. The convertTo function, provides a function for converting the matrix;
5. depth Function, which returns the image depth;
6. The channels function, which returns the number of image channels;
7. The type function, which returns the matrix type;
8. The zeros function, which returns a matrix of 0;
other functions can be searched by themselves, and the methods of use are not introduced one by one.

to sum up

As the data structure of image processing, Mat objects are rich in content and need to be learned. After you use them, you will spend time learning. You don't need to spend time to learn, but the basics are to understand and use.

Please correct me if there are any errors!

Guess you like

Origin blog.csdn.net/ivan_9/article/details/113087688