我是靠谱客的博主 阔达大山,最近开发中收集的这篇文章主要介绍OpenCV概念基础 4.5.3,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

API Concepts

cv Namespace

All the OpenCV classes and functions are placed into the cv namespace. Therefore, to access this functionality from your code, use the cv:: specifier or using namespace cv; directive:

 

#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 );

...

Some of the current or future OpenCV external names may conflict with STL or other libraries. In this case, use explicit namespace specifiers to resolve the name conflicts:

 

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 handles all the memory automatically.

First of all, std::vector, cv::Mat, and other data structures used by the functions and methods have destructors that deallocate the underlying memory buffers when needed. This means that the destructors do not always deallocate the buffers as in case of Mat. They take into account possible data sharing. A destructor decrements the reference counter associated with the matrix data buffer. The buffer is deallocated if and only if the reference counter reaches zero, that is, when no other structures refer to the same buffer. Similarly, when a Mat instance is copied, no actual data is really copied. Instead, the reference counter is incremented to memorize that there is another owner of the same data. There is also the Mat::clone method that creates a full copy of the matrix data. See the example below:

// 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();

You see that the use of Mat and other basic structures is simple. But what about high-level classes or even user data types created without taking automatic memory management into account? For them, OpenCV offers the cv::Ptr template class that is similar to std::shared_ptr from C++11. So, instead of using plain pointers:

T* ptr = new T(...);

you can use:

Ptr<T> ptr(new T(...));

or:

Ptr<T> ptr = makePtr<T>(...);

Ptr<T> encapsulates a pointer to a T instance and a reference counter associated with the pointer. See the cv::Ptr description for details.

Automatic Allocation of the Output Data

OpenCV deallocates the memory automatically, as well as automatically allocates the memory for output function parameters most of the time. So, if a function has one or more input arrays (cv::Mat instances) and some output arrays, the output arrays are automatically allocated or reallocated. The size and type of the output arrays are determined from the size and type of input arrays. If needed, the functions take extra parameters that help to figure out the output array properties.

Example:

#include "opencv2/imgproc.hpp"

#include "opencv2/highgui.hpp"

using namespace cv;

int main(int, char**)

{

VideoCapture cap(0);

if(!cap.isOpened()) return -1;

Mat frame, edges;

namedWindow("edges", WINDOW_AUTOSIZE);

for(;;)

{

cap >> frame;

cvtColor(frame, edges, COLOR_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;

}

最后

以上就是阔达大山为你收集整理的OpenCV概念基础 4.5.3的全部内容,希望文章能够帮你解决OpenCV概念基础 4.5.3所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部