转载自:http://blog.csdn.net/what_lei/article/details/54613575
Mat:
- the matrix header 包含矩阵尺寸;
- a pointer 包含矩阵中的数值。
- Mat M(2,2,CV_8UC3,Scalar(0,0,125));
注:2,2表示矩阵尺寸;
CV_8UC3表示
CV_[The number of bits per itrm][signed or Unsigned][Type prefix]C[the channel number];
Scalar(0,0,125)表示初始值。
- 使用create()函数
创建特殊的Mat矩阵:
- 零矩阵--zeros()
- 单位矩阵--eye()
- 全1矩阵--ones()
常见的其他输出项:
- 2D Point
- 3D Point
- std::vector via cv::Mat
- std::vector of points
#include<iostream>
#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;
using namespace cv::ml;
int main()
{
cout << "构造函数创建Mat:Mat M(2, 2, CV_8UC3, Scalar(0, 0, 125))" << endl;
Mat M(2, 2, CV_8UC3, Scalar(0, 0, 125));
cout << M.rows << endl;
cout << M.cols << endl;
cout << M << endl;
cout << "create函数创建Mat:Mat M1;M1.create(4, 4, CV_8UC(2)); " << endl;
Mat M1;
M1.create(4, 4, CV_8UC(2));
cout << M1.rows << endl;
cout << M1.cols << endl;
cout << M1 << endl;
cout << "Mat类静态函数zeros:Mat zero = Mat::zeros(3, 3, CV_8UC1);" << endl;
cout << "创建零矩阵" << endl;
Mat zero = Mat::zeros(3, 3, CV_8UC1);
cout << zero << endl;
cout << "单位矩阵:" << endl;
Mat I = Mat::eye(4, 4, CV_64F);
cout << I << endl;
cout << "全1矩阵:" << endl;
Mat one = Mat::ones(2, 2, CV_32F);
cout << one << endl;
Point2f p(5, 1);
cout << "Point(2D) = " << p << endl;
Point3f p3f(2, 6, 7);
cout << "Point (3D) = " << p3f << endl;
vector<float> v;
v.push_back((float)CV_PI); v.push_back(2); v.push_back(3.01f);
Mat t = Mat(v); //t表示3行1列的矩阵
cout << "Vector of floats via Mat = " << t << endl << endl;
cout << t.rows << endl; //3
cout << t.cols << endl; //1
return 0;
}