我们知道,OpenCV2的矩阵形式是Mat,那么Mat矩阵的初始化怎么自定义呢 ?由于比较简单,文字部分我就不多加说明了,见代码,有下面几种:
///////////////////////////////////////////////////////////////////////////////////////////////
//定义一个全0矩阵
Mat zeroMatrix(Size(, ), CV_8UC1, Scalar());
cout << "zeroMatrix=\n " << zeroMatrix << endl; uchar matrix[][] = { { , , , , , }, { , , , , , }, { , , , , , }, { , , , , , }, { , , , , , } }; Mat Matrix(Size(, ), CV_8UC1, matrix);//注意:opencv里的行列顺序是和maltab相反的
//由于Mat矩阵默认的是uchar类型,所以前后一致,定义矩阵时也要定义uchar类型
//若将int double float 等类型 赋予Mat 那么Mat就要定义CV_32F等对应的类型 cout << "Matrix=\n " << Matrix << endl;
cout << "Matrix.rows= " << Matrix.rows << endl;//行数
cout << "Matrix.cols= " << Matrix.cols << endl;//列数 Mat d = (Mat_<double>(, ) << , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ); cout << "d=\n " << d << endl;
cout << "d.rows= " << d.rows << endl;
cout << "d.cols= " << d.cols << endl;
cout << "d.element= " << d.at<double>(,) << endl;//第一个元素值
//////////////////////////////////////////////////////////////////////////////////////////////////
结果如下: