简体   繁体   English

OpenCV =运算符

[英]OpenCV = operator

I am working on OpenCV and I have a confusion. 我正在研究OpenCV,我有一个困惑。 I went through this link and I did not quite understand the concept of '=' operator in OpenCV. 我经历了这个链接 ,我不太了解OpenCV中'='运算符的概念。

Suppose I declare 3 Matrices as follows: 假设我声明3个矩阵如下:

Mat img1, img2, gray;

If I obtain the matrix gray from the image captured from the camera and assign it to img1 as mentioned below, what actually happens? 如果我从相机捕获的图像中获得矩阵gray并将其分配给img1 ,如下所述,实际发生了什么? Does the data in gray get copied to img1 or is it that data is shared between them? gray数据是否被复制到img1或者数据是否在它们之间共享?

img1 = gray;

OpenCV's Mat class is simply a header for the actual image data, which it contains a pointer to. OpenCV的Mat类只是实际图像数据的标题 ,它包含一个指针。 The = operator copies the pointer (and the other information in the header, like the image dimensions) so that both Mat s share the same data. =运算符复制指针(以及标题中的其他信息,如图像维度),以便两个Mat共享相同的数据。 This means that modifying the data in one Mat also changes it in the other. 这意味着修改一个Mat的数据也会在另一个中更改它。 This is called a "shallow" copy, since only the top layer (the header) is copied, not the lower layer (the data). 这被称为“浅”副本,因为只复制顶层(标题),而不是下层(数据)。

To make a copy of the underlying data (called a "deep copy"), use the clone() method. 要制作基础数据的副本(称为“深层副本”),请使用clone()方法。 You can find information about it on the page that you linked to. 您可以在链接到的页面上找到有关它的信息。

It explains in the link you provided. 它在您提供的链接中解释。

Mat& Mat::operator = (const Mat& m) Mat&Mat :: operator =(const Mat&m)

m :The assigned, right-hand-side matrix. m:指定的右侧矩阵。 Matrix assignment is O(1) operation, that is, no data is copied . 矩阵分配是O(1)操作,即没有数据被复制 Instead, the data is shared and the reference counter, if any, is incremented. 相反, 数据被共享 ,并且参考计数器(如果有的话)递增。 Before assigning new data, the old data is dereferenced via Mat::release . 在分配新数据之前,通过Mat :: release取消引用旧数据。

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

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