今天总结了5种创建Mat图像的方法,都是可以直接运行的。
代码:
1 #include<opencv2/opencv.hpp>
2 #include<iostream>
3 using namespace cv;
4 using namespace std;
5
6 int main() {
7 /*
8 CV_8UC1 8位1通道
9 CV_8UC3 8位3通道
10 CV_32FC1 32位1通道
11 CV_64FC1 64位1通道
12 */
13
14 //创建Mat图像(像素值自定义)
15 Mat MM(5, 5, CV_8UC1, Scalar(128,0,0));// 参数(int rows, int cols, int type, const Scalar& s)
16 cout << "MM = " << endl << " " << MM << endl;
17
18 //创建Mat图像(像素值205)
19 Mat MC;
20 MC.create(5, 5, CV_8UC1);
21 cout << "MC = "<< endl << " " << MC << endl;
22
23 //创建Mat图像(像素值单位矩阵)
24 Mat E = Mat::eye(5, 5, CV_8UC1);
25 cout << "E = " << endl << " " << E << endl;
26
27 //创建Mat图像(像素值全1矩阵)
28 Mat O = Mat::ones(5, 5, CV_32FC1);
29 cout << "O = " << endl << " " << O << endl;
30
31 //创建Mat图像(像素值全0矩阵)
32 Mat Z = Mat::zeros(5, 5, CV_64FC1);
33 cout << "Z = " << endl << " " << Z << endl;
34
35 waitKey();
36 system("pause");
37 return 0;
38 }
结果: