我们可以这样使用:cv::Mat M(…); cout << M;,直接将矩阵内容输出到控制台。
输出格式支持多种风格,包括OpenCV的原生格式、MATLAB风格、Python风格、NumPy风格、CSV格式以及C语言风格等。
cv::Mat::eye 是一个静态方法,用于创建一个单位矩阵(对角线上的元素为1,其余位置为0)的 cv::Mat 对象。这个方法使得初始化一个单位矩阵变得简单快捷。
cv::Mat eye(int rows, int cols, int type = CV_64F);
cv::Mat identityMatrix = cv::Mat::eye(3, 3, CV_32F);
这段代码将创建一个3x3的浮点型单位矩阵。
使用 cv::Mat 的构造函数来初始化一个矩阵
cv::Mat::Mat(int rows, int cols, int type);
生成单个均匀分布的随机数或随机数数组。
此非模板函数变体将矩阵dst填充来自指定范围的均匀分布随机数:
low c ≤ dst ( I ) c < high c \texttt{low} _c \leq \texttt{dst} (I)_c < \texttt{high} _c lowc≤dst(I)c<highc
-参数 dst 输出随机数数组;该数组必须预先分配.
-参数 low 生成随机数的包含下限.
-参数 high 生成随机数的不包含上限.
/*
*
*该程序演示了cv::Mat类的串行输出功能,
*也就是说,你现在可以这样使用:cv::Mat M(...); cout << M;,直接将矩阵内容输出到控制台。
*输出格式支持多种风格,包括OpenCV的原生格式、MATLAB风格、Python风格、NumPy风格、CSV格式以及C语言风格等。
*
*/
#include "opencv2/core.hpp"
#include
using namespace std;
using namespace cv;
int main( int argc, char** argv )
{
Mat I = Mat::eye( 4, 4, CV_64F );
I.at< double >( 1, 1 ) = CV_PI;
cout << "I = \n" << I << ";" << endl << endl;
Mat r = Mat( 10, 3, CV_8UC3 );
randu( r, Scalar::all( 0 ), Scalar::all( 255 ) );
cout << "r (默认风格) = \n" << r << ";" << endl << endl;
cout << "r (matlab风格) = \n" << format( r, Formatter::FMT_MATLAB ) << ";" << endl << endl;
cout << "r (python风格) = \n" << format( r, Formatter::FMT_PYTHON ) << ";" << endl << endl;
cout << "r (numpy风格) = \n" << format( r, Formatter::FMT_NUMPY ) << ";" << endl << endl;
cout << "r (csv风格) = \n" << format( r, Formatter::FMT_CSV ) << ";" << endl << endl;
cout << "r (c风格) = \n" << format( r, Formatter::FMT_C ) << ";" << endl << endl;
Point2f p( 5, 1 );
cout << "p = " << p << ";" << endl;
Point3f p3f( 2, 6, 7 );
cout << "p3f = " << p3f << ";" << endl;
vector< float > v;
v.push_back( 1 );
v.push_back( 2 );
v.push_back( 3 );
cout << "shortvec = " << Mat( v ) << endl;
vector< Point2f > points( 20 );
for ( size_t i = 0; i < points.size(); ++i )
points[ i ] = Point2f( ( float )( i * 5 ), ( float )( i % 7 ) );
cout << "points = " << points << ";" << endl;
return 0;
}
I =
[1, 0, 0, 0;
0, 3.141592653589793, 0, 0;
0, 0, 1, 0;
0, 0, 0, 1];
r (默认风格) =
[ 91, 2, 79, 179, 52, 205, 236, 8, 181;
239, 26, 248, 207, 218, 45, 183, 158, 101;
102, 18, 118, 68, 210, 139, 198, 207, 211;
181, 162, 197, 191, 196, 40, 7, 243, 230;
45, 6, 48, 173, 242, 125, 175, 90, 63;
90, 22, 112, 221, 167, 224, 113, 208, 123;
214, 35, 229, 6, 143, 138, 98, 81, 118;
187, 167, 140, 218, 178, 23, 43, 133, 154;
150, 76, 101, 8, 38, 238, 84, 47, 7;
117, 246, 163, 237, 69, 129, 60, 101, 41];
r (matlab风格) =
(:, :, 1) =
91, 179, 236;
239, 207, 183;
102, 68, 198;
181, 191, 7;
45, 173, 175;
90, 221, 113;
214, 6, 98;
187, 218, 43;
150, 8, 84;
117, 237, 60
(:, :, 2) =
2, 52, 8;
26, 218, 158;
18, 210, 207;
162, 196, 243;
6, 242, 90;
22, 167, 208;
35, 143, 81;
167, 178, 133;
76, 38, 47;
246, 69, 101
(:, :, 3) =
79, 205, 181;
248, 45, 101;
118, 139, 211;
197, 40, 230;
48, 125, 63;
112, 224, 123;
229, 138, 118;
140, 23, 154;
101, 238, 7;
163, 129, 41;
r (python风格) =
[[[ 91, 2, 79], [179, 52, 205], [236, 8, 181]],
[[239, 26, 248], [207, 218, 45], [183, 158, 101]],
[[102, 18, 118], [ 68, 210, 139], [198, 207, 211]],
[[181, 162, 197], [191, 196, 40], [ 7, 243, 230]],
[[ 45, 6, 48], [173, 242, 125], [175, 90, 63]],
[[ 90, 22, 112], [221, 167, 224], [113, 208, 123]],
[[214, 35, 229], [ 6, 143, 138], [ 98, 81, 118]],
[[187, 167, 140], [218, 178, 23], [ 43, 133, 154]],
[[150, 76, 101], [ 8, 38, 238], [ 84, 47, 7]],
[[117, 246, 163], [237, 69, 129], [ 60, 101, 41]]];
r (numpy风格) =
array([[[ 91, 2, 79], [179, 52, 205], [236, 8, 181]],
[[239, 26, 248], [207, 218, 45], [183, 158, 101]],
[[102, 18, 118], [ 68, 210, 139], [198, 207, 211]],
[[181, 162, 197], [191, 196, 40], [ 7, 243, 230]],
[[ 45, 6, 48], [173, 242, 125], [175, 90, 63]],
[[ 90, 22, 112], [221, 167, 224], [113, 208, 123]],
[[214, 35, 229], [ 6, 143, 138], [ 98, 81, 118]],
[[187, 167, 140], [218, 178, 23], [ 43, 133, 154]],
[[150, 76, 101], [ 8, 38, 238], [ 84, 47, 7]],
[[117, 246, 163], [237, 69, 129], [ 60, 101, 41]]], dtype='uint8');
r (csv风格) =
91, 2, 79, 179, 52, 205, 236, 8, 181
239, 26, 248, 207, 218, 45, 183, 158, 101
102, 18, 118, 68, 210, 139, 198, 207, 211
181, 162, 197, 191, 196, 40, 7, 243, 230
45, 6, 48, 173, 242, 125, 175, 90, 63
90, 22, 112, 221, 167, 224, 113, 208, 123
214, 35, 229, 6, 143, 138, 98, 81, 118
187, 167, 140, 218, 178, 23, 43, 133, 154
150, 76, 101, 8, 38, 238, 84, 47, 7
117, 246, 163, 237, 69, 129, 60, 101, 41
;
r (c风格) =
{ 91, 2, 79, 179, 52, 205, 236, 8, 181,
239, 26, 248, 207, 218, 45, 183, 158, 101,
102, 18, 118, 68, 210, 139, 198, 207, 211,
181, 162, 197, 191, 196, 40, 7, 243, 230,
45, 6, 48, 173, 242, 125, 175, 90, 63,
90, 22, 112, 221, 167, 224, 113, 208, 123,
214, 35, 229, 6, 143, 138, 98, 81, 118,
187, 167, 140, 218, 178, 23, 43, 133, 154,
150, 76, 101, 8, 38, 238, 84, 47, 7,
117, 246, 163, 237, 69, 129, 60, 101, 41};
p = [5, 1];
p3f = [2, 6, 7];
shortvec = [1;
2;
3]
points = [0, 0;
5, 1;
10, 2;
15, 3;
20, 4;
25, 5;
30, 6;
35, 0;
40, 1;
45, 2;
50, 3;
55, 4;
60, 5;
65, 6;
70, 0;
75, 1;
80, 2;
85, 3;
90, 4;
95, 5];