OpenCV deep learning (2)-Mat construction Initialization

Source: Internet
Author: User
Tags scalar

It mainly refers to the translation of this Part in the manual;

There are many methods to create Mat objects, mainly including:
1. Use the create (nrow, ncols, type) function, or a similar constructor Mat (nrow, ncols, type [, fillValue]). A new array of the specified size and format will be allocated,
Type is the same as cvCreateMat function. For example, CV_8UC1 indicates creating an array of eight-bit single-channel channels, while CV_32FC2 indicates a two-channel floating-point array. For example:

// Create a 7*7 complex matrix and initialize it to 1 + 3j; cv: Mat M (, CV_32FC2, Scalar )); // then convert M to the 8-bit matrix of the 15 channels of 100*60; M. create (100,60, CV_8UC (15 ));

As described at the beginning, the create () function allocates a new array only when the current array size or type is different from the specified one.
2. Similar to 1, you can also create a multi-dimensional array:

// Create an 8-Bit Array of 100*100*100 int sz [] = {100,100,100}; cv: Mat bigCube (3, sz, CV_8U, Scalar :: all (0 ));

Even if the dimensions parameter passed to the constructor is 1, the created array is a two-dimensional array, that is, the dims parameter is 2, the function regards the matrix as a matrix with only multiple rows and one column. Therefore, Mat: dims is always greater than or equal to 2 (when the array is empty, it is 0 );
3. Use the copy constructor or the copy operator, as shown in the following figure. The complexity of the value assignment operation is O (1, because only copying the object header and increasing the reference count (the data is not actually copied), you can use the Mat function when necessary. clone ()
To obtain the complete copy of the array.
4. Create a new header for other Mat arrays. The Mat array can be a row, a column, multiple rows, multiple columns, or a rectangular area or diagonal line in the matrix array, the complexity of these operations is also O (1), because the new header points to the same data,
You can use this feature to change part of the array, for example:

// Add three times the number of rows to the number of rows. row (3) = M. row (3) + M. row (5) * 3; // copy 7th columns to 1st columns, using M. col (1) = M. col (7) won't work. Use the following method: Mat M1 = M. col (1); M. col (7 ). copyTo (M1); // create a 320x240 image Mat img (Size (320,240), CV_8UC3); // select a ROIMat roi (img, Rect (10, 10,100,100); // fill the ROI color in green and the original img image will change. Roi = Scalar (0,255, 0 );

Because of the help of datastart and dataend, you can use the locateROI () function to calculate the relative position of the sub-block in the main array;

Mat A = Mat: eye (10, 10, CV_32S); // extract column [) of A, left closed and right open; Mat B = A (Range: all (), range: (1, 3); // extract the rows of B [5, 9). In this way, C is the rows 5-8 of A and columns 1-2. Mat C = B (Range: :( 5, 9 ), range: all (); Size size; Point ofs; C. locateROI (size, ofs); // size is (10, 10), ofs (x = 1, y = 5)

If necessary, you can use the clone () function to copy the extracted child matrix.
5. Allocate an object header to the user's external data. This method is useful in the following scenarios:
(1) Use OpenCV to process external data (for example, a self-implemented DirectShow filter or a gstreamer processing module), such

Void process_video_frame (const unsigned char * pixels, int width, int height, int step) {Mat img (height, width, CV_8UC3, pixels, step ); // note the relationship between height and width and cols and rows; GaussianBlur (img, img, Size (1.5), 1.5 );}

(2) Quickly initialize a small matrix and \ or obtain the super-fast Element Acquisition speed;

double m[3][3] = {{a,b,c},{d,e,f},{g,h,i}};Mat M = Mat(3,3, CV_64F, m).inv();

It is not commonly used, but it is very common to use user data distribution when converting from CvMat or IplImage to Mat [partial yet very common cases of this... case ],
For this reason, there is a dedicated Mat constructor that points to the CvMat or IplImage pointer and optional marker flag indicating whether data replication is performed.
The reverse conversion from Mat to CvMat or IplImage is completed by the overloaded operators Mat: operator CvMat () and Mat: operator IplImage (). This operation will not replicate data!

IplImage *img = cvLoadImage("greatewave.jpg",1);Mat mtx(img); //IplImage*->MatCvMat oldmat = mtx; //Mat->CvMatCV_Assert(oldmat.cols==img->width && oldmat.rows==img->height && oldmat.data.ptr==(uchar *)img->imageData && oldmat.step==img->widthStep);

6. Use MATLAB-style matrix initialization functions, such as zeros (), ones (), and eys:
// Create a dual-precision unit matrix and add it to M;
M + = Mat: eye (M. rows, M. cols, CV_64F );

7. Separate the values with commas (,) for initialization;
// Create a 3 × 3 Dual-precision unit array;
Mat M = (Mat _ <double> () <, 1 );
Using this method, you can first call a Mat _ class constructor with a given suitable parameter, and then use the <operator to input values separated by commas (,). These values can be constants, variables, expressions, etc. Note the extra brackets to avoid compilation errors;
[At this point, the commonly used methods for creating the Mat matrix are quite complete. Let's take a look at several constructor implementation methods in the constructor. The parameters are Vec, Matx, and vector;
8. Use the template constructor;

const int dimss=40;vector<float> stl_vec;Vec<short, dimss> vecs;Matx<double, 30, 50> matxs;for (int i=0; i<dimss; ++i){vecs[i] = i*3;stl_vec.push_back(i*10.f);}Mat vecMat(vecs);Mat matxMat(matxs);Mat stlvecMat(stl_vec,true);

This method is mainly used to convert vector representation to Mat Matrix Representation for easy processing. The key is vector construction, such as the contour obtained using findContour,
Point vector can be converted to Mat for processing;

]
--------------------------------------------------------------------------------
The construction and initialization of Mat are basically the same. Next, we will summarize how to obtain the Mat element.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.