Opencv learning notes () -- Talk about the data structure of opencv, mat is so powerful

Source: Internet
Author: User

Or a program with real-time requirements recently put me on fire. To speed up code running, I also learned something new. Because vector is used in my code, I searched for it on the Internet. Some people say that the vector is slow, and some people say that the sort sorting speed in STL is something that I can't do for ordinary people. Some people say that we need less resize, this re-allocation of memory is very time-consuming, and some people say they should use swap to completely delete unwanted memory space. With so many questions, I really don't know why my program is slow. As a result, I have read objective STL. This book is quite deep. I want to understand the above problems that I am eager to figure out. Then I looked at the program and made some optimizations. The speed was not improved much, And the header started to grow.

My solution is to use the gettickcount and gettickfrequency functions mentioned above to analyze the running time of my code. The problem is that the following sentence is very time-consuming.

Vector <vector <int> test = vector <int> (10000, vector <int> (10, 0 ));

10000*10 is just the space I want to open up. I don't know how to use the reserve function to open up. This statement runs for about 100 ms. Let's look at this code again today and find that I am too stupid. Why should we use vector <int>? We can use the mat provided by opencv to solve this problem. So we can rewrite the above sentence as follows:

Mat test1 = mat _ <int>: zeros (10000, 10 );

The result only takes 0.2 ms !!! Comrades, STL seems so powerless in front of mat! I decided to spend a few days reading the core of the reference to thoroughly understand the opencv data structure. We also feel that mat is compatible with STL, and there are also operations such as push_back and pop_back. So, comrades, never use mat only as a display image, it is a very powerful data structure. With it, you can get twice the result with half the effort. Who knows it! ~~

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.