Mat是Opencv2里面主要的类。Mat的对象常常用来表示一副图像的信息。

Mat的基本操作十分简单,不多说了。下面这段代码能够读七八分明白,应该就算对Mat有大体了解。

 1 /*  For description look into the help() function. */
 2 
 3 #include "opencv2/core/core.hpp"
 4 #include <iostream>
 5 
 6 using namespace std;
 7 using namespace cv;
 8 
 9 static void help()
10 {
11     cout
12     << "\n--------------------------------------------------------------------------" << endl
13     << "This program shows how to create matrices(cv::Mat) in OpenCV and its serial"
14     << " out capabilities"                                                            << endl
15     << "That is, cv::Mat M(...); M.create and cout << M. "                            << endl
16     << "Shows how output can be formated to OpenCV, python, numpy, csv and C styles." << endl
17     << "Usage:"                                                                       << endl
18     << "./cvout_sample"                                                               << endl
19     << "--------------------------------------------------------------------------"   << endl
20     << endl;
21 }
22 
23 int main(int,char**)
24 {
25     help();
26     // create by using the constructor
27     Mat M(2,2, CV_8UC3, Scalar(0,0,255));
28     cout << "M = " << endl << " " << M << endl << endl;
29 
30     // create by using the create function()
31     M.create(4,4, CV_8UC(2));
32     cout << "M = "<< endl << " "  << M << endl << endl;
33 
34     // create multidimensional matrices
35     int sz[3] = {2,2,2};
36     Mat L(3,sz, CV_8UC(1), Scalar::all(0));
37     // Cannot print via operator <<
38 
39     // Create using MATLAB style eye, ones or zero matrix
40     Mat E = Mat::eye(4, 4, CV_64F);
41     cout << "E = " << endl << " " << E << endl << endl;
42 
43     Mat O = Mat::ones(2, 2, CV_32F);
44     cout << "O = " << endl << " " << O << endl << endl;
45 
46     Mat Z = Mat::zeros(3,3, CV_8UC1);
47     cout << "Z = " << endl << " " << Z << endl << endl;
48 
49     // create a 3x3 double-precision identity matrix
50     Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
51     cout << "C = " << endl << " " << C << endl << endl;
52 
53     Mat RowClone = C.row(1).clone();
54     cout << "RowClone = " << endl << " " << RowClone << endl << endl;
55 
56     // Fill a matrix with random values
57     Mat R = Mat(3, 2, CV_8UC3);
58     randu(R, Scalar::all(0), Scalar::all(255));
59 
60     // Demonstrate the output formating options
61     cout << "R (default) = " << endl <<        R           << endl << endl;
62     cout << "R (python)  = " << endl << format(R,"python") << endl << endl;
63     cout << "R (numpy)   = " << endl << format(R,"numpy" ) << endl << endl;
64     cout << "R (csv)     = " << endl << format(R,"csv"   ) << endl << endl;
65     cout << "R (c)       = " << endl << format(R,"C"     ) << endl << endl;
66 
67     Point2f P(5, 1);
68     cout << "Point (2D) = " << P << endl << endl;
69 
70     Point3f P3f(2, 6, 7);
71     cout << "Point (3D) = " << P3f << endl << endl;
72 
73 
74     vector<float> v;
75     v.push_back( (float)CV_PI);   v.push_back(2);    v.push_back(3.01f);
76 
77     cout << "Vector of floats via Mat = " << Mat(v) << endl << endl;
78 
79     vector<Point2f> vPoints(20);
80     for (size_t i = 0; i < vPoints.size(); ++i)
81         vPoints[i] = Point2f((float)(i * 5), (float)(i % 7));
82 
83     cout << "A vector of 2D Points = " << vPoints << endl << endl;
84     return 0;
85 }
View Code

相关文章: