Mat
1. Mat对象的创建方法:
cv::Mat::Mat Constructor
Mat M(2,2, CV_8UC3, Scalar(0,0,255)); cout << "M = " << endl << " " << M << endl << endl;
利用数组可以创建多维矩阵
int sz[3] = {2,2,2}; Mat L(3,sz, CV_8UC(1), Scalar::all(0));
cv::Mat::create function:
M.create(4,4, CV_8UC(2)); cout << "M = "<< endl << " " << M << endl << endl;
熟悉的MATLAB初始化方式
Mat E = Mat::eye(4, 4, CV_64F); cout << "E = " << endl << " " << E << endl << endl; Mat O = Mat::ones(2, 2, CV_32F); cout << "O = " << endl << " " << O << endl << endl; Mat Z = Mat::zeros(3,3, CV_8UC1); cout << "Z = " << endl << " " << Z << endl << endl;
采用 cv::Mat::clone() or cv::Mat::copyTo() 方法
Mat RowClone = C.row(1).clone(); cout << "RowClone = " << endl << " " << RowClone << endl << endl;
2. Mat一些特征概括
官方文档中,对于Mat的几点概括如下:
1. Output image allocation for OpenCV functions is automatic (unless specified otherwise).
2. You do not need to think about memory management with OpenCVs C++ interface.
3. The assignment operator and the copy constructor only copies the header.
4. The underlying matrix of an image may be copied using the cv::Mat::clone() and cv::Mat::copyTo() functions.
Mat的以下操作均为浅复制,A,B,C中每个Mat对象共享同一个矩阵数据,只是header不一样:
Mat A, C; // creates just the header parts
A = imread(argv[1], IMREAD_COLOR); // here we'll know the method used (allocate matrix)
Mat B(A); // Use the copy constructor
C = A; // Assignment operator
在实际应用中,也经常是多个Mat实例共用一个矩阵数据,或者矩阵数据中的某部分,如下面:
Mat D (A, Rect(10, 10, 100, 100) ); // using a rectangle
Mat E = A(Range::all(), Range(1,3)); // using row and column boundaries