1. Mat数据区和头信息(相同数据区、不同头信息)
Mat A, C; // creates just the header parts
A = imread(argv[1], CV_LOAD_IMAGE_COLOR); // here we'll know the method used (allocate matrix)
Mat B(A); // Use the copy constructor
C = A; // Assignment operator
All the above objects, in the end, point to the same single data matrix. Their headers are different, however, and making a modification using any of them will affect all the other ones as well.
2. 创建ROI
Mat D (A, Rect(10, 10, 100, 100) ); // using a rectangle
Mat E = A(Range::all(), Range(1,3)); // using row and column boundaries
3. 复制Mat数据区和头信息
Mat F = A.clone();
Mat G;
A.copyTo(G);
Sometimes you will want to copy the matrix itself too, so OpenCV provides the clone() and copyTo() functions.
4.构造函数定义Mat,并初始化
Mat M(2,2, CV_8UC3, Scalar(0,0,255));
cout << "M = " << endl << " " << M << endl << endl;
5.Create()函数定义Mat
M.create(4,4, CV_8UC(2));
cout << "M = "<< endl << " " << M << endl << endl;
You cannot initialize the matrix values with this construction. It will only reallocate its matrix data memory if the new size will not fit into the old one.
6.获取Mat的一行或者一列
Mat::row(int y)
The method makes a new header for the specified matrix row and returns it. This is an O(1) operation, regardless of the matrix size. The underlying data of the new matrix is shared with the original matrix. Here is the example of one of the classical basic matrix processing operations, axpy, used by LU and many other algorithms:
inline void matrix_axpy(Mat& A, int i, int j, double alpha)
{
A.row(i) += A.row(j)*alpha;
}
In the current implementation, the following code does not work as expected:
Mat A;
...
A.row(i) = A.row(j); // will not work
To achieve that, you should either turn this simple assignment into an expression or use the Mat::copyTo() method:
Mat A;
...
// works, but looks a bit obscure.
A.row(i) = A.row(j) + 0;
// this is a bit longer, but the recommended method.
A.row(j).copyTo(A.row(i));
7.opencv中感兴趣区域以及mask的使用
http://blog.csdn.net/dengtaocs/article/details/38022153?utm_source=tuicool&utm_medium=referral
Mat image,mask;
Rect r1(100,100,50,100);
Mat img1,img2,img3,img4;
for(;;)
{
sequence >> image;
mask = Mat::zeros(image.size(),CV_8UC1);
mask(r1).setTo(255);
img1 = image(r1);
image.copyTo(img2,mask);
image.copyTo(img3);
img3.setTo(0,mask);
imshow("Image sequence", image);
imshow("img1",img1);
imshow("img2",img2);
imshow("img3",img3);
imshow("mask",mask);
waitKey(0);
}