Reference article: http://www.cnblogs.com/tornadomeet/archive/2012/07/19/2599376.html
Experiment Description: (citation)
This article mainly talk about some OpenCV 2.0 after the appearance of the mat rectangle class, mainly refers to the OpenCV from the Doc folder Tutiol textbooks. In this experiment, I think we need to pay special attention to the following points (which can be reflected in the code):
1. Use the CREATE function to re-change the layout of the mat data in memory. 2. Note that the multi-channel data in the mat actually occupies an element's position. 3. Learn how to create a multidimensional mat. 4. When the mat matrix compares hours, learn the method of direct assignment, that is, with Mat_. 5. Master Several common formats for the mat matrix content output to the terminal. 6. Note that if the vector is a single dimension, it needs to be converted to a mat to output, multidimensional can be directly output, such as the vector is stored in a point.
#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp># Include <opencv2/imgproc/imgproc.hpp>using namespace std;using namespace Cv;int Demo_mat () {int next_num = 1;char Tmp;while (next_num<7) {cout<< "--------" <<next_num<< "--------" <<endl;waitkey (0); Switch (next_num) {Case 1: {Mat M (2, 2, CV_8UC3, Scalar (0,255,0)),//is actually a 2*6 matrix because each element has 3 channels. cout<< "Create by using the constructor ..." <<endl;cout<< "M = \ n" <<M<<endl; M.create (4, 4, CV_8UC (2));//The 2 in parentheses represents a 2-channel cout<< "Create by using CREATE Function ..." <<endl;cout<< "M = \ N "<<M<<endl;break; }//when a variable is defined in a case statement, it needs to be enclosed in parentheses, otherwise it will be error box 2: {int sz[3] = {2, 2, 2}; Mat L (3, SZ, CV_8UC (1), Scalar::all (0)); cout<< "Create multidimensional matrix ..." <<endl; cout<< "L ="<<L<<endl; cannot print out here, because that only adapts to the two-dimensional array break; } Case 3: {Mat E = Mat::eye (4, 4, cv_64f); Mat O = Mat::ones (2, 3, cv_32f); Mat Z = Mat::zeros (3, 3, CV_8UC1); cout<< "using matlab stytle ..." <<endl; cout<< "E = \ n" <<E<<endl; cout<< "O = \ n" <<O<<endl; cout<< "Z = \ n" <<Z<<endl; Break }case 4:{mat C = (mat_<double> (3,3) <<0,-1,0,-1,5,-1,0,-1,0);//The method of directly assigning the initial value Mat Row_clone = C.row (1). Clone (); cout<< "Create 3*3 double-precision identity matrix ..." <<endl;cout<< "C = \ n" <<C<<endl; cout<< "Row_clone =" <<row_clone<<endl;break;} Case 5: {Mat R = Mat (3, 2, CV_8UC3); Randu (R, Scalar::all (0), Scalar::all (255)); cout<< "FillA matrix with Rand numbers ... "<<endl; cout<< "R (default) =" <<R<<endl; cout<< "Demonstrate the output formating options ..." <<endl; cout<< "R (python) = \ n" <<format (r, "Python") <<endl; cout<< "R (numpy) = \ n" <<format (R, "NumPy") <<endl;//numpy is a scientific calculation package implemented in Python cout<< " R (CSV) = \ n "<<format (R," CSV ") <<endl;//csv, comma delimiter cout<<" R (c) = \ n "<<format (R, "C") <<endl; Break }case 6:{cout<< "The point format output ..." <<endl; POINT2F P1 (5, 1);cout<< "point (2D) =" <<P1<<endl; point3f P2 (4, 5, 6);cout<< "point (3D) =" <<p2<<endl;vector<float>v;v.push_back ((float) CV_PI) ; V.push_back (2);//push_back adds a data to its tail v.push_back (3.01f);cout<< "vector of float: =" <<mat (v) << Endl;//vector data cannot be output separately, so you can output int N using the mat20;vector<point2f>vpoints (N);//vector can define its length with a variable, which works better than an array for (size_t E = 0; E < Vpoints.size (); ++e)//size_t is actually a unsigned int type vpoints[e] = point2f ((float) (e*5), (float) (e%7));cout<< "vpoints[" = \ n "<< vpoints<<endl;//but the vector point can be directly output, because this time the vector itself is//a multidimensional (at least 2 dimensions) mat break;} Default:break; }next_num++;} return 0;}
Operation Result:
Several methods of creating OPENCV basic image Container Mat