OpenCV 2.4.9 Study Notes (2)--OPENCV memory Automatic Management

Source: Internet
Author: User
Tags image processing library

OPENCV Automatic memory management

The current version of OPENCV is automatically processing all of its own memory, although that is not very rigorous. OpenCV introduced a new C + + interface in version 2.0, using automatic memory management to give a new way to solve the problem. With this approach, developers don't have to tangle with managing memory, and your code becomes concise.

In mat, for example , there is no need to manually (1) Open space for it (2) to release the space immediately when it is not needed. But it's still possible to do it manually: most OPENCV functions will still manually open up space for the output data. When passing an existing Mat object, the well-established matrix space is reused. In other words, we use the exact size of the memory each time to complete the task. Basically, Mat is a class consisting of two data parts: a matrix header (containing information such as matrix size, storage method, storage address, etc.) and a pointer to a matrix that stores all the pixel values (depending on the different matrices of the selected storage method). The size of the matrix head is a constant value, but the size of the matrix itself varies according to the image and is usually a number of orders of magnitude larger than the size of the matrix head. Therefore, when you pass an image in a program and create a copy, the large overhead is caused by the matrix, not the information header. OpenCV is an image processing library that includes a large number of image processing functions, and in order to solve the problem it is common to use multiple functions in the library, so passing images in a function is commonplace. And don't forget that we're talking about a computationally significant image processing algorithm, so we shouldn't copy large images unless we have to, because it slows down the program.

To solve this problem, OpenCV uses a reference counting mechanism. The idea is to have each Mat object have its own information header, but share the same matrix. This is accomplished by pointing the matrix pointer to the same address. The copy constructor copies only the information header and matrix pointers , not the matrix.

1 Mat A, C;                                 // Create an Information header section only 2 A = Imread (argv[1//  here for the matrix to open up memory 3 Mat B (A);                                 // using copy Constructors 4 C = A;                                    // Assignment Operators

All the mat objects in the above code end up pointing to the same and only one data matrix. Although their information headers are different, changes made by any one object can affect other objects as well. In fact, different objects are just different ways to access the same data. Here's a great feature to mention: You can create information headers that reference only part of the data. For example, to create an area of interest ( ROI ), you only need to create an information header that contains the boundary information:

1 Mat D (A, Rect (ten)//  using A rectangle2< /c10> Mat E = A (Range:all (), Range (1,3//  using row and column boundaries

Now you might ask if the matrix belongs to more than one Mat object, who will be responsible for cleanup when it is no longer needed? The simple answer is: The last object to use it. Implemented by a reference counting mechanism. Whenever someone copies the information header of a Mat object, it increases the number of references to the matrix, whereas when a header is released, the count is reduced by one; When the count is zero, the matrix is cleared. But at some point you still want to copy the matrix itself (not just information headers and matrix pointers), you can use the function clone () or copyTo () .

1 Mat F = a.clone (); 2 Mat G; 3 A.copyto (G);

changing F or G now does not affect the matrix that the Mat information header points to.

Summarize

1. The memory allocation of the output image in the OpenCV function is done automatically (if not specifically specified).

2, the use of OpenCV C + + interface does not need to consider the memory release problem.

3, assignment operators and copy constructors ( ctor ) only copy information headers.

4. Use the function clone () or CopyTo () to copy the matrix of an image.

The above sections refer to the Chinese course of OPENCV. (http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/core/mat%20-%20the%20basic%20image%20container/ Mat%20-%20the%20basic%20image%20container.html#matthebasicimagecontainer)

OpenCV 2.4.9 Study Notes (2)--OPENCV memory Automatic Management

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.