Location>code7788 >text

Getting Started with OpenCV in C++ 004: Using Mat Objects

Popularity:239 ℃/2024-11-08 15:00:39
1 #include <opencv2/> 2 #include <iostream> 3 #include <> 4 5 using namespace std; 6 using namespace cv; 7 8 int main() 9 { 10 //1、Mat object and IplImage object 11 // 1.1 The IplImage object has existed since the release of OpenCV in 2001. It is a data structure with C language style, which requires developers to allocate and manage the memory by themselves, and it is easy to lead to memory leakage in large projects. 12 // 1.2 Mat object is an image data structure introduced after OpenCV 2.0, which is an object-oriented data structure with automatic memory allocation and no memory leakage. The Mat object is divided into two parts, the header and the data part. 13 // 1.3. Mat type constructors and common methods. 14 // 15 // 1.3.1, constructor 16 // 1.3.1.1、Mat() 17 // 1.3.1.2、Mat(int rows, int cols, int type); 18 // 1.3.1.3、Mat(Size size, int type); 19 // 1.3.1.4、Mat(int rows, int cols, int type, const Scalar& s); 20 // 1.3.1.5、Mat(Size size, int type, const Scalar& s); 21 // 1.3.1.6、Mat(int ndims, const int* sizes, int type); 22 // 1.3.1.7、Mat(int ndims, const int* sizes, int type, const Scalar& s); 23 // There are many more, I won't list them 24 // 25 // 1.3.2 Commonly used methods 26 // 1.3.2.1、void copyTo( OutputArray m ) const; 27 // 1.3.2.2、void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const; 28 // 1.3.2.3, Mat clone() const; is an exact copy, which gives a completely new Mat object. 29 // 1.3.2.4、int channels() const; 30 // 1.3.2.5、int depth() const; 31 // 1.3.2.6、bool empty() const; 32 // 1.3.2.7、uchar* ptr(int i0=0); 33 // There are many more, I won't list them 34 // 35 // 36 //2、Mat object use 37 // 2.1 Partial Copy: In general, only the head and pointer part of the Mat object will be copied, the data part will not be copied. 38 // Mat a=imread(imagefilePath); 39 // Mat b(a); by copy construction there will be no data part. 40 // 2.2. Full copy: If you want to copy the header and data part of Mat object together, you can use Mat's clone() or copyTo() method. 41 // 2.3 Four Points for Using Mat Objects 42 // A. The memory for the output image is automatically allocated. 43 // B. Using OpenCV's C++ interface, there is no need to consider memory allocation. 44 // C. The assignment operation and the copy constructor will only copy the header and pointer parts, not the data parts. 45 // D. Use the clone and copyTo functions to achieve full copying. 46 // 47 //3. Mat Define Array 48 //Creating multidimensional data Mat::create 49 // int sz[3]={2,2,2}; 50 // Mat a(3,sz,CV_8UC1,Scalar::all(0)); 51 52 53 Mat src; 54 src = imread("F:\\TestImage\\ZZImage\\", IMREAD_UNCHANGED); 55 if (()) 56 { 57 cout << "There is an error loading the image!" << endl; 58 return -1; 59 } 60 61 namedWindow("DemoWindow", WINDOW_AUTOSIZE); 62 imshow("DemoWindow", src); 63 64 65 66 /*Mat dst; 67 dst = Mat((),()); 68 dst = Scalar(0,255,0);*/ 69 70 71 //Mat dst; 72 //dst = Mat((), ());//This statement can be omitted 73 //dst = (); 74 75 /*Mat dst; 76 (dst,());*/ 77 78 /*Mat dst; 79 (dst);*/ 80 81 /*Mat dst; 82 83 cvtColor(src, dst, COLOR_BGR2GRAY); 84 cout << "Original map channel:" << () << endl; 85 cout << "Target map channel:" << () << endl; 86 87 int height = ; 88 int width = ; 89 cout << "Lines:" << height << endl; 90 cout << "Number of columns:" << width << endl; 91 92 const uchar* firstRow = <uchar>(0); 93 cout << "Value of first row:" << *firstRow << endl;*/ 94 95 /*Mat dst(3, 3, CV_8UC1, Scalar(0)); 96 97 cout << "dst=" << endl << dst << endl;*/ 98 99 //Mat dst; 100 //((),()); 101 ////Copying is not possible with the create method. 102 //dst = Scalar(0,0,255); 103 104 105 /*Mat dst = Mat::zeros((),());*/ 106 107 Mat dst = Mat::eye(2,2,CV_8UC1); 108 cout << "dst=" << endl << dst << endl; 109 110 namedWindow("DemoWindow2", WINDOW_AUTOSIZE); 111 imshow("DemoWindow2", dst); 112 113 waitKey(0); 114 115 return 0; 116 }