OpenCV4.5.5 learning notes: common sense learning of OpenCV (API, automatic memory management, argc and argv)

Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it

preface

The author had the honor to contact OpenCV3 when he was an undergraduate The study of version 2.0 had to stop temporarily due to the pressure of the postgraduate entrance examination. Now the postgraduate entrance examination task is over, and the future tutor is also engaged in this direction. The author has started a new round of study. When I came back, I found that OpenCV had reached version 4.5.5, so I downloaded the new version again and decided to record this learning process. Due to the limited level of the author, there may be mistakes. Please forgive me and point out that let's learn together and make progress together.
First of all, I need to explain that I study according to the introduction to OpenCV3 programming written by master Mao Xingyun. I will try my best to convert the program of master Mao Xingyun into OpenCV4 Version 5.5. Elder Mao Xingyun died on December 11, 2021. He is an industry leader I admire very much. I started to contact OpenCV after reading his book.

1, API concept

All OpenCV classes and functions are placed in the cv namespace. Therefore, to access this function from the code, you need to use the instruction cv:: or the using namespace cv instruction:
For example:

#include " opencv2/core.hpp "

cv::Mat H = cv::findHomography (points1, points2, cv::RANSAC , 5);

Or:

#include " opencv2/core.hpp "

using namespace cv;
Mat H = findHomography (points1, points2, RANSAC , 5);

It should be noted that the second form may have the risk of conflict with other namespaces. For example, if a function exists in two namespaces at the same time, you must specify which space you are using.

2, Automatic memory management

OpenCV automatically handles all memory.
First, std::vector, cv::Mat and other data structures used by functions and methods have destructors that release the underlying memory buffer when needed. This means that the destructor does not always release the buffer as Mat does. They considered possible data sharing. The destructor decrements the reference counter associated with the matrix data buffer. The buffer is released when and only when the reference counter reaches zero, that is, when no other structure references the same buffer. Similarly, when the Mat instance is copied, no actual data is actually copied. Instead, the reference counter is incremented to remember another owner of the same data. There is also a Mat::clone method that can create a complete copy of the matrix data (this will be described in detail when learning Mat). See the example on the official website:

The code is as follows (example):

// create a big 8Mb matrix
Mat A(1000, 1000, CV_64F);
// create another header for the same matrix;
// this is an instant operation, regardless of the matrix size.
Mat B = A;
// create another header for the 3-rd row of A; no data is copied either
Mat C = B.row(3);
// now create a separate copy of the matrix
Mat D = B.clone();
// copy the 5-th row of B to C, that is, copy the 5-th row of A
// to the 3-rd row of A.
B.row(5).copyTo(C);
// now let A and D share the data; after that the modified version
// of A is still referenced by B and C.
A = D;
// now make B an empty matrix (which references no memory buffers),
// but the modified version of A will still be referenced by C,
// despite that C is just a single row of the original A
B.release();
// finally, make a full copy of C. As a result, the big modified
// matrix will be deallocated, since it is not referenced by anyone
C = C.clone();

3, argc and argv

In the process of learning OpenCV, especially in the official program and the official book Learning OpenCV 3, we can often see that argc and argv parameters often appear in functions and are passed as formal parameters, such as the first example program in the book:

arg in argc and argv represents "parameter", where argc often represents an integer of type int, while argv with * represents a string array.

summary

This part gives me a macro understanding of OpenCV and some very practical common sense.

Tags: C++ OpenCV Windows Visual Studio

Posted by radley on Tue, 19 Apr 2022 07:41:06 +0930