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 //! [constructor] 28 Mat M(2,2, CV_8UC3, Scalar(0,0,255)); 29 cout << "M = " << endl << " " << M << endl << endl; 30 //! [constructor] 31 32 // create by using the create function() 33 //! [create] 34 M.create(4,4, CV_8UC(2)); 35 cout << "M = "<< endl << " " << M << endl << endl; 36 //! [create] 37 38 // create multidimensional matrices 39 //! [init] 40 int sz[3] = {2,2,2}; 41 Mat L(3,sz, CV_8UC(1), Scalar::all(0)); 42 //! [init] 43 44 // Cannot print via operator << 45 46 // Create using MATLAB style eye, ones or zero matrix 47 //! [matlab] 48 Mat E = Mat::eye(4, 4, CV_64F); 49 cout << "E = " << endl << " " << E << endl << endl; 50 Mat O = Mat::ones(2, 2, CV_32F); 51 cout << "O = " << endl << " " << O << endl << endl; 52 Mat Z = Mat::zeros(3,3, CV_8UC1); 53 cout << "Z = " << endl << " " << Z << endl << endl; 54 //! [matlab] 55 56 // create a 3x3 double-precision identity matrix 57 //! [comma] 58 Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0); 59 cout << "C = " << endl << " " << C << endl << endl; 60 //! [comma] 61 62 //! [clone] 63 Mat RowClone = C.row(1).clone(); 64 cout << "RowClone = " << endl << " " << RowClone << endl << endl; 65 //! [clone] 66 67 // Fill a matrix with random values 68 //! [random] 69 Mat R = Mat(3, 2, CV_8UC3); 70 randu(R, Scalar::all(0), Scalar::all(255)); 71 //! [random] 72 73 // Demonstrate the output formating options 74 //! [out-default] 75 cout << "R (default) = " << endl << R << endl << endl; 76 //! [out-default] 77 //! [out-python] 78 cout << "R (python) = " << endl << format(R, Formatter::FMT_PYTHON) << endl << endl; 79 //! [out-python] 80 //! [out-numpy] 81 cout << "R (numpy) = " << endl << format(R, Formatter::FMT_NUMPY ) << endl << endl; 82 //! [out-numpy] 83 //! [out-csv] 84 cout << "R (csv) = " << endl << format(R, Formatter::FMT_CSV ) << endl << endl; 85 //! [out-csv] 86 //! [out-c] 87 cout << "R (c) = " << endl << format(R, Formatter::FMT_C ) << endl << endl; 88 //! [out-c] 89 90 //! [out-point2] 91 Point2f P(5, 1); 92 cout << "Point (2D) = " << P << endl << endl; 93 //! [out-point2] 94 95 //! [out-point3] 96 Point3f P3f(2, 6, 7); 97 cout << "Point (3D) = " << P3f << endl << endl; 98 //! [out-point3] 99 100 //! [out-vector] 101 vector<float> v; 102 v.push_back( (float)CV_PI); v.push_back(2); v.push_back(3.01f); 103 cout << "Vector of floats via Mat = " << Mat(v) << endl << endl; 104 //! [out-vector] 105 106 //! [out-vector-points] 107 vector<Point2f> vPoints(20); 108 for (size_t i = 0; i < vPoints.size(); ++i) 109 vPoints[i] = Point2f((float)(i * 5), (float)(i % 7)); 110 cout << "A vector of 2D Points = " << vPoints << endl << endl; 111 //! [out-vector-points] 112 return 0; 113 } 114