简体   繁体   English

opencv :: Mat是什么类型的对象? 这是shared_ptr还是auto_ptr? 是指针吗?

[英]What type of object is a opencv::Mat? It is a shared_ptr or maybe an auto_ptr ? is it a pointer?

I used OpenCv and I used it in code similar to the following code: 我使用了OpenCv,并在类似于以下代码的代码中使用了它:

Mat Create()
{ 
     Mat myMat(10, 10, CV8U_C1);    
     int x=myMat.Rows; // I am accessing Mat like an object not a pointer. 
     Return myMat;
 }

 Main()
 {
       Mat aMat=Create(); // created inside this function
       int x=aMat.Rows; // accessing it using . notation
       // do some work
       return;   //I did not delete Mat, as it would release its memory.
 }

How can create similar objects n my c++ code? 如何在我的C ++代码中创建类似的对象?

I am using STL, but if needed I can use Boost too. 我正在使用STL,但是如果需要,我也可以使用Boost。

First of all, std::vector , Mat , and other data structures have destructors that deallocate the underlying memory buffers when needed. 首先, std::vectorMat和其他数据结构具有析构函数,这些析构函数可以在需要时释放基础内存缓冲区。 This means that the destructors do not always deallocate the buffers as in case of Mat . 这意味着析构函数并不总是像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. 类似地,当复制Mat实例时,不会真正复制任何实际数据。 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. 还有Mat::clone()方法可创建矩阵数据的完整副本。

The other answers are correct -- cv::Mat is a resource-owning object like std::vector<> that can be passed around and returned by value. 其他答案是正确的cv::Mat是一个资源拥有的对象,如std::vector<> ,可以传递并按值返回。 However, there is one very important way in which cv::Mat behaves like a pointer, and that's in regards to its aliasing behavior. 但是, cv::Mat有一种非常重要的方式表现为指针,这与其别名行为有关。 For most types T , the following is true: 对于大多数类型T ,以下内容是正确的:

T orig = initial_value;
T copy = orig;
mutate(copy);
assert(orig == initial_value);

That is, for most types, copies are independent. 也就是说,对于大多数类型,副本是独立的。 You can mutate one without affecting the other. 您可以变异一个而不影响另一个。 This is not true for cv::Mat . 对于cv::Mat并非如此。 Instead, you have to watch out for stuff like this: 相反,您必须当心这样的事情:

cv::Mat orig = /* ... matrix of all zeros ... */
cv::Mat copy = orig; // NOTE! Creates an alias, not a copy.
copy(2,2) = 42;
assert(orig(2,2) == 42); // Huh.

See? 看到? changing the copy changed the original. 更改副本更改了原件。 Speaking technically, cv::Mat is not a regular type , which is a pity because most modern APIs including the STL assume that types are regular. 从技术上讲, cv::Mat不是常规类型 ,这很遗憾,因为包括STL在内的大多数现代API都假定类型为常规类型。 With cv::Mat , things silently alias, leading to hard-to-find, spooky-action-at-a-distance kinds of bugs. 使用cv::Mat ,事物默默地混叠,从而导致难以发现的,距离遥远的怪异动作。 Use cv::Mat with extreme care. 极其小心地使用cv::Mat And not ever with STL algorithms and containers. 而且从来没有使用STL算法和容器。

Your question is answered by just a quick look at the API reference 只需快速查看API参考即可回答您的问题

class CV_EXPORTS Mat
{
public:
    // ... a lot of methods ...
    ...

The best thing to do would be to read the OpenCV tutorial on Mat 最好的办法是阅读Mat上OpenCV教程

The first thing you need to know about Mat is that you no longer need to manually allocate its memory and release it as soon as you do not need it. 您需要了解Mat的第一件事是您不再需要手动分配它的内存并在不需要时立即释放它。 ... ...

Mat is basically a class with two data parts: the matrix header (containing information such as the size of the matrix, the method used for storing, at which address is the matrix stored, and so on) and a pointer to the matrix containing the pixel values ... Mat本质上是具有两个数据部分的类:矩阵头(包含诸如矩阵大小,用于存储的方法,用于存储矩阵的地址等信息)以及指向包含该矩阵的矩阵的指针像素值...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM