OpenCV =运算子 [英] OpenCV = operator
问题描述
我正在开发OpenCV,但感到困惑.我通过了链接,但我不太理解OpenCV中'='运算符的概念.
I am working on OpenCV and I have a confusion. I went through this link and I did not quite understand the concept of '=' operator in OpenCV.
假设我声明3个矩阵,如下所示:
Suppose I declare 3 Matrices as follows:
Mat img1, img2, gray;
如果我从相机拍摄的图像中获取矩阵gray
并将其分配给img1
,如下所述,实际上会发生什么?是将gray
中的数据复制到img1
还是在它们之间共享数据?
If I obtain the matrix gray
from the image captured from the camera and assign it to img1
as mentioned below, what actually happens? Does the data in gray
get copied to img1
or is it that data is shared between them?
img1 = gray;
推荐答案
OpenCV的Mat
类只是实际图像数据的 header ,它包含一个指向的指针. =
运算符复制指针(以及标题中的其他信息,如图像尺寸),以便两个Mat
共享相同的数据.这意味着修改一个Mat
中的数据也会更改另一个Mat
中的数据.之所以称为浅"副本,是因为仅复制顶层(标头),而不复制底层(数据).
OpenCV's Mat
class is simply a header for the actual image data, which it contains a pointer to. 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. This means that modifying the data in one Mat
also changes it in the other. This is called a "shallow" copy, since only the top layer (the header) is copied, not the lower layer (the data).
要复制基础数据(称为深层复制"),请使用clone()
方法.您可以在链接到的页面上找到有关它的信息.
To make a copy of the underlying data (called a "deep copy"), use the clone()
method. You can find information about it on the page that you linked to.
这篇关于OpenCV =运算子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!