OpenCV2的Mat矩阵形式自定义初始化

我们知道,OpenCV2的矩阵形式是Mat,那么Mat矩阵的初始化怎么自定义呢 ?由于比较简单,文字部分我就不多加说明了,见代码,有下面几种:

 1     ///////////////////////////////////////////////////////////////////////////////////////////////
 2     //定义一个全0矩阵
 3     Mat zeroMatrix(Size(6, 5), CV_8UC1, Scalar(0));
 4     cout << "zeroMatrix=
 " << zeroMatrix << endl;
 5 
 6     uchar matrix[5][6] = { { 1, 0, 1, 1, 2, 0 }, { 0, 1, 0, 0, 1, 2 }, { 2, 1, 2, 2, 1, 2 }, { 2, 1, 2, 2, 1, 0 }, { 0, 0, 1, 1, 0, 2 } };
 7 
 8     Mat Matrix(Size(6, 5), CV_8UC1, matrix);//注意:opencv里的行列顺序是和maltab相反的
 9     //由于Mat矩阵默认的是uchar类型,所以前后一致,定义矩阵时也要定义uchar类型
10     //若将int double float 等类型 赋予Mat 那么Mat就要定义CV_32F等对应的类型
11 
12     cout << "Matrix=
 " << Matrix << endl;
13     cout << "Matrix.rows= " << Matrix.rows << endl;//行数
14     cout << "Matrix.cols= " << Matrix.cols << endl;//列数
15 
16     Mat d = (Mat_<double>(5, 6) << 1, 0, 1, 1, 2, 0, 0, 1, 0, 0, 1, 2 , 2, 1, 2, 2, 1, 2 , 2, 1, 2, 2, 1, 0 , 0, 0, 1, 1, 0, 2);
17 
18     cout << "d=
 " << d << endl;
19     cout << "d.rows= " << d.rows << endl;
20     cout << "d.cols= " << d.cols << endl;
21     cout << "d.element= " << d.at<double>(0,0) << endl;//第一个元素值
22     //////////////////////////////////////////////////////////////////////////////////////////////////

结果如下:

原文地址:https://www.cnblogs.com/ggYYa/p/6168863.html