Introduction Opencv (OpenSourceComputerVisionLibrary: opencv.org) is an open-source library containing thousands of computer vision algorithms. This document describes the so-called OpenCV2.X interface, which is different from the OpenCV1.X Interface Based on C language, this is a C-based programming interface. Opencv is the library structure, that is, Opencv
Opencv (Open Source Computer Vision Library: http://opencv.org) is an Open Source Library containing thousands of Computer Vision algorithms, this document describes the so-called OpenCV 2.X interface, and C language-based OpenCV 1. the X interface is different. This is a C-based programming interface. Opencv is the library structure, that is, Opencv
Introduction
Opencv (Open Source Computer Vision Library: http://opencv.org) is an Open Source Library containing thousands of Computer Vision algorithms, this document describes the so-called OpenCV 2.X interface, and C language-based OpenCV 1. X interfaces are different. This is a C ++-based programming interface.
Opencv is the organizational structure of libraries, that is, Opencv contains shared or static libraries. The available libraries are as follows:
Core-a database that defines the most basic data structure, including intensive multi-dimensional matrix Mat and some basic functions used by other databases
Imgproc-image processing library, including linear nonlinear image filtering, image ry Transformation (resize, affine transformation, perspective transformation, basic table-based re ing), color space conversion, histogram, etc.
Video-video analysis library, including motion estimation, background difference, and Object Tracking Algorithms
Calib3d-basic multi-angle Geometric algorithms, single camera and Stereo Camera calibration, object pose estimation, three-dimensional matching algorithm and element 3D reconstruction.
Features2d-feature probe sub-, description sub-, and description sub-match
Objdetect-detection of instances or objects of predefined classes (such as faces, eyes, mugs, people, cars, etc)
Highgui-simple and easy-to-use video capture interface, image 'video codecs, and simple UI Functions
Gpu-GPU-based acceleration algorithms in different opencv Libraries
...-Other help libraries, such as FLANN and Google test wrappers, and python binding
The functions of each module will be described in the subsequent sections, but you should be familiar with the most basic API concepts throughout the library at the beginning.
1.1 API Concept
Cv namespace
All opencv classes and functions are placed in the cv namespace. Therefore, to use them in your program, use cv: to specify or directly use using namespace cv;
#include "opencv2/core/core.hpp"...
Cv: Mat H = cv: findHomography (points1, points2, CV_RANSAC, 5);... or
#include "opencv2/core/core.hpp"using namespace cv;...Mat H = findHomography(points1, points2, CV_RANSAC, 5);...
Some existing or future external names in opencv may conflict with STL (standard library) or other libraries. In this case, clear namespace instructions are needed to solve the conflict:
Mat a(100, 100, CV_32F);randu(a, Scalar::all(1), Scalar::all(std::rand()));cv::log(a, a);a /= std::log(2.);
Automatic Memory Management
Opencv automatically processes all the memory.
First, std: vector, Mat, and other data structures used by functions or methods will release the relevant memory destructor when necessary, and the Destructor will not always release the memory, taking Mat for example, considering possible data sharing, the Destructor will decrease the reference counter of the matrix data buffer, and only when the reference counter is 0, that is, when no other structure points to the buffer zone, the buffer zone will be released. Similarly, when a Mat instance is copied, the actual data is not actually copied. On the contrary, the Reference Counter of the buffer increases progressively to indicate that the buffer is shared by another owner. Of course, Mat: The clone method creates the entire copy of the matrix data. See the following example:
// create a big 8Mb matrixMat 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 eitherMat C = B.row(3);// now create a separate copy of the matrixMat 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 AB.release();// finally, make a full copy of C. As a result, the big modified// matrix will be deallocated, since it is not referenced by anyoneC = C.clone();
You can see that the application of Mat and other basic structures is very simple, but what about the high-level classes and user data types that do not have an automatic memory management mechanism, opencv provides Ptr <> template class similar to std: shared_ptr, so it replaces pure pointer usage:
T* ptr = new T(...);you can use:Ptr
ptr = new T(...);
Ptr Ptr encapsulates the pointer to the T instance and the reference counter related to the pointer.
Automatic Memory Allocation of output data
Opencv Automatically releases the memory. In most cases, the memory is automatically allocated to the output function parameters. Therefore, if a function has one or more input arrays (cv: Mat instance) and some output arrays, the output array will be automatically allocated or re-allocated memory. The size and type of the output array are determined by the input array. If needed, the function also accepts additional parameters to indicate the attributes of the output array.
Example:
#include "cv.h"#include "highgui.h"using namespace cv;int main(int, char**){VideoCapture cap(0);if(!cap.isOpened()) return -1;Mat frame, edges;namedWindow("edges",1);for(;;){cap >> frame;cvtColor(frame, edges, CV_BGR2GRAY);GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);Canny(edges, edges, 0, 30, 3);imshow("edges", edges);if(waitKey(30) >= 0) break;}return 0;}
The frame array is automatically allocated by the operator> Based on the resolution and depth of the video frame. The edges array is automatically allocated by the cvtColor function, its size and bit depth are the same as the input array. The number of channels is 1 because the conversion code CV_BGR2GRAY (indicating the conversion of color to grayscale ). Remember that frame and edges will only be allocated with memory during the first loop, because the next video frame has the same resolution and depth. If you change the video resolution, these arrays will be automatically reassigned.
The key component of this technique is the Mat: create method, which accepts the expected size and type. If the array already has a specific size and type, this method does nothing, otherwise, it will release the previously allocated data (progressively referencing the counter and comparing it with 0), and then apply to allocate a new designated size event buffer. Most functions call the Mat: create method for each output array, which also explains the automatic memory allocation mechanism of the output array.
Note that cv: mixChannels, cv: RNG: fill and other functions and methods do not allocate memory to the output array. Therefore, you need to allocate memory in advance.
Saturated Arithmetic
As a computer vision library, opencv often processes compact image pixels, eight or only 16 bytes of data in each channel. In this way, it is required to limit the data value range, further, image-specific operations (such as color space conversion, brightness/contrast adjustment, sharpening, complex interpolation (bi-cubic, Lanczos) will generate beyond the acceptable data range, if you can only store 8-bit or 16-bit results, this may lead to visual defects or further affect subsequent analysis and processing. To solve this problem, the so-called saturated arithmetic comes in handy. For example, in order to store r (8-bit image data), the following operations will allow you ~ Find the value closest to the actual value within 255.
I (x; y) = min (max (round (r); 0); 255)
The same rule is also used to process 8-Bit Signed, 16-bit signed, and unsigned types. Such semantics can be seen everywhere in the library, in the C ++ language, saturate_cast for the type conversion operation in class standard C ++ is used. See the implementation of the above equation below:
I. (Y, x) = saturate_cast (R );
Cv: uchar is an 8-bit unsigned type in opencv,
Fixed pixel type, limited use of templates
The template is a great feature of c ++ for implementing strong, efficient, and secure data structures and algorithms. However, excessive template usage leads to a longer Compilation Time and a larger amount of code. In addition, it is difficult to separate an interface and implementation when templates are used exclusively (??), Of course, this is no problem for basic algorithms, but it is not applicable to computer vision libraries with thousands of lines of code for an algorithm, because it also simplifies the binding development with other languages (such as python, jave, and matlab, there is no template language at all), the current opencv is based on the implementation of dynamic binding of polymorphism runtime, where dynamic binding is too slow (like pixel access operators), it is impossible (generic Ptr <> implementation), or it is inconvenient (saturate_cast <>, opencv introduces small template classes, methods, and functions, while other parts of opencv limit the use of templates.
Therefore, the data types that the computer vision database can operate on are a limited and fixed set of basic data types. That is, the array element must be of the following type:
? 8-bit unsigned integer (uchar)
? 8-bit signed integer (schar)
? 16-bit unsigned integer (ushort)
? 16-bit signed integer (short)
? 32-bit signed integer (int)
? 32-bit floating-point number (float)
? 64-bit floating-point number (double)
? The data type of the tuples. The element types in the tuples are the same. An Image array is a type of tuples. It is called a multi-channel array to distinguish a single-channel array. The maximum number of possible channels is defined by the macro CV_CN_MAX. The current version is set to 512.
For this basic type, the following enumeration can be used:
Enum {CV_8U = 0, CV_8S = 1, CV_16U = 2, CV_16S = 3, CV_32S = 4, CV_32F = 5, CV_64F = 6 };
Multi-channel types can be specified using the following options:
? CV_8UC1... CV_64FC4 constant (channel data 1-4)
? CV_8UC (n )... CV_64FC (n) or CV_MAKETYPE (CV_8U, n )... CV_MAKETYPE (CV_64F, n) when the number of channels is greater than 4 or when the compilation is unknown, the variables can use these macros.
Note: CV_32FC1 = CV_32F, CV_32FC2 = CV_32FC (2) = CV_MAKETYPE (CV_32F, 2), and
CV_MAKETYPE (depth, n) = (x & 7) <3) + (n-1 ).
Mat mtx(3, 3, CV_32F); // make a 3x3 floating-point matrixMat cmtx(10, 1, CV_64FC2); // make a 10x1 2-channel floating-point// matrix (10-element complex vector)Mat img(Size(1920, 1080), CV_8UC3); // make a 3-channel (color) image// of 1920 columns and 1080 rows.Mat grayscale(image.size(), CV_MAKETYPE(image.depth(), 1)); // make a 1-channel image of// the same size and same
Arrays of more complex elements cannot be constructed or processed by opencv, or each function or method can only process subsets of the preceding array types. The more complex an algorithm is, the smaller the type subset it supports, the following typical example:
Face detection algorithms can only process 8-bit gray or color images.
Linear Algebra functions and most machine learning algorithms can only process floating-point data.
Basic functions, such as cv: add, support all types
The color space conversion function supports 8-bit unsigned, 16-bit unsigned, and 32-bit floating point types.
A subset of the supported types of each function can be set based on the actual application and can be expanded in the future based on user requirements.
Input array and output Array
Many opencv functions can process two-dimensional or multi-dimensional numeric arrays. Generally, these functions use cpp: class: Mat as parameters, but in some cases, std :: vector <> (such as a point set) or Matx <> (such as 3x3 matrix ?) To avoid API duplication, special "proxy" classes are introduced. The OutputArray derived from InputArray is used to specify an output array for the function. Generally, you need to care about these intermediate types (but you do not need to explicitly declare these types of variables): These are automatically completed tasks. In addition to InputArray/OutputArray, you can use Mat, std: vector <>, Matx <>, Vec <> or Scalar. When a function has an optional input or output array, and you do not have to or do not want to define the array, You can input cv: noArray ().
Error Handling
Opencv uses exceptions to trigger key errors. When the input data is of the correct type and the data is within the specified range, but the algorithm fails for some reason, the function will return a specific error code (normally, is a Boolean variable)
Exceptions can be instantiated through the cv: Exception class. cv: Exception is derived from std: exception, so it is easily handled by the Standard C ++ library component.
try{... // call OpenCV}catch( cv::Exception& e ){const char* err_msg = e.what();std::cout << "exception caught: " << err_msg << std::endl;}
Multithreading and re-import
The current version of OPENCV fully supports re-entry, that is, the same function, the constant method of the same class instance, or different threads can call the very number method of different classes of instances. Of course, the same cv: Mat can also be called in different threads, because counter reference operations are Schema-based atomic commands