Opencv learning notes (40) -- next to opencv Data Structure mat

Source: Internet
Author: User
Tags scalar

I remember getting started with opencv because a two-dimensional dynamic array is needed in an algorithm. At that time, it was easy to look at the core part. With the increase in usage, we became more and more fond of the mat structure, I also thought it was necessary to learn about the new things, so I will take a look at Mat this time.

The biggest advantage of mat is similar to that of STL. It manages the memory dynamically without manual memory management. For some large-scale development projects, sometimes the time invested in lpimage memory management is even more time than focusing on Algorithm Implementation, which is obviously inappropriate. Except for some embedded applications that must use C language, I strongly recommend mat at any time.

The mat class has two parts of data. One is the matrix header. The size of this part is fixed, including the size of the matrix, the storage method, and the address of the matrix storage. The other part is a pointer to a matrix containing pixel values.

Mat A, C; // creates just the header partsA = imread(argv[1], CV_LOAD_IMAGE_COLOR); // here we’ll know the method used (allocate matrix)Mat B(A); // Use the copy constructorC = A; // Assignment operator

Note that the operations such as copy only copy the matrix header and the pointer of the matrix, instead of the matrix itself, which means that the data pointers of the two matrices point to the same address, developers must pay special attention to this issue. For example, in the above section, A, B, and C point to the same piece of data. Their headers are different, but operations on a also affect the results of B and C. I just raised the issue of automatic memory release, so I will release the memory when I no longer use a. It is not dangerous to operate B and C at that time. Don't worry. We have already considered this issue because we will release the memory only when the last mat is no longer used. We can just use it with confidence.

If you want to create a mat that does not affect each other, it is a real copy operation, you need to use the clone () or copyto () function ().

When it comes to data storage, this has always been a noteworthy issue. Mat _ <uchar> corresponds to cv_8u, And Mat _ <uchar> corresponds to cv_8u, mat _ <char> corresponds to cv_8s, mat _ <int> corresponds to cv_32s, mat _ <float> corresponds to cv_32f, And Mat _ <double> corresponds to cv_64f, the corresponding data depth is as follows:

• Cv_8u-8-bit unsigned integers (0 .. 255)

• Cv_8s-8-bit signed integers (-128 .. 127)

• Cv_16u-16-bit unsigned integers (0 .. 65535)

• Cv_16s-16-bit signed integers (-32768 .. 32767)

• Cv_32s-32-bit signed integers (-2147483648 .. 2147483647)

• Cv_32f-32-bit parameter oating-point numbers (-flt_max..flt_max, INF, Nan)

• Cv_64f-64-bit parameter oating-point numbers (-dbl_max..dbl_max, INF, Nan)

Note that many opencv functions support only 8-bit and 32-Bit Data depth. Therefore, cv_64f is not used, however, the vs compiler will automatically convert the float data to the double type, which is not very nice.

Another issue that needs to be paid attention to is the stream operator <for mat operations, only when mat is a two-dimensional operation.

It is also necessary to say that mat storage is Row-by-Row Storage.

In addition, there are two ways to create mat: 1. Call create (row, column, type) 2.mat( row, column, type (value )). For example:

// make a 7x7 complex matrix filled with 1+3j.Mat M(7,7,CV_32FC2,Scalar(1,3));// and now turn M to a 100x60 15-channel 8-bit matrix.// The old content will be deallocatedM.create(100,60,CV_8UC(15));

To create a matrix with a higher dimension, write the following method:

// create a 100x100x100 8-bit arrayint sz[] = {100, 100, 100};Mat bigCube(3, sz, CV_8U, Scalar::all(0));

For matrix row operations or column operations, follow these steps: (Note that you need to create a new mat during Column Operations. I think it should be related to the column address discontinuous)

// add the 5-th row, multiplied by 3 to the 3rd rowM.row(3) = M.row(3) + M.row(5)*3;// now copy the 7-th column to the 1-st column// M.col(1) = M.col(7); // this will not workMat M1 = M.col(1);M.col(7).copyTo(M1);

The following is violent. For external data, for example, you accept an image from another place, but it can be not mat structure, but only has one Data Pointer, let's take a look at how the following code works.

void process_video_frame(const unsigned char* pixels,int width, int height, int step){Mat img(height, width, CV_8UC3, pixels, step);GaussianBlur(img, img, Size(7,7), 1.5, 1.5);}

It's easy !!!

Another method to quickly initialize data is as follows:

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

You can also convert an image in the original iplimage format into a mat structure using the mat (iplimage) method, or call zeros (), ones (), and eye () Like Matlab () such a function is initialized.

If you need to release data pointers and memory in advance, you can call release ().

The format of at <float> (3, 3) is the best. I did not dare to introduce other methods.

The last point I want to mention is about mat expressions. There are many such expressions: addition, subtraction, multiplication, division, and transpose inverse. How can I remember my previous introduction. That's not much to say ~

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.