[英]Deep Copy of OpenCV cv::Mat
The behaviour of copying cv::Mat
is confusing me.复制
cv::Mat
的行为让我很困惑。
I understand from the documentation that Mat::copyTo()
is deep copy while the assignment operator is not.我从文档中了解到
Mat::copyTo()
是深拷贝,而赋值运算符不是。 My questions:我的问题:
what should I do to return a cv::Mat
from a function, such as: cv::Mat func()
?我应该怎么做才能从函数返回
cv::Mat
,例如: cv::Mat func()
?
According to the documentation, if I return a cv::Mat
it'll have no use, because after the function returns the local copy of the cv::Mat
in that function will be destroyed and therefore the one accepting the returned value outside the function should be pointing to some random address.根据文档,如果我返回一个
cv::Mat
它将没有用,因为在函数返回该函数中cv::Mat
的本地副本后将被销毁,因此接受外部返回值的那个函数应该指向某个随机地址。 The weird thing is that (most of times) it works correctly.奇怪的是(大多数时候)它可以正常工作。 For example, the following works:
例如,以下工作:
cv::Mat CopyOneImage(const cv::Mat& orgImage) { cv::Mat image; orgImage.copyTo(image); return image; } int main() { std::string orgImgName("a.jpg"); cv::Mat orgImage; orgImage = cv::imread(orgImgName); cv::Mat aCopy; aCopy = CopyOneImage(orgImage); return 1; }
But why?但为什么? It's not a deep copy.
这不是深拷贝。
Question 3. And also sometimes the assignment operator seems to be deep copy, too:问题 3. 有时赋值运算符似乎也是深拷贝:
int main()
{
std::string orgImgName("a.jpg");
cv::Mat orgImage;
orgImage = cv::imread(orgImgName);
cv::Mat aCopy;
orgImage.copyTo(aCopy);
cv::Mat copyCopy1;
copyCopy1 = aCopy;
cv::namedWindow("smallTest", 1);
cv::imshow("smallTest", copyCopy1);
uchar key = (uchar)cv::waitKey();
cv::Mat orgImage2 = cv::imread("b.jpg");
orgImage2.copyTo(aCopy);
cv::imshow("smallTest", copyCopy1);
return 1;
}
Then the two displays shows the same image, a.jpg.然后两个显示器显示相同的图像,a.jpg。 Why?
为什么? And some other times it doesn't work.
有时它不起作用。 (The original code is too long but it can be also simplified to the above case).
(原代码太长但也可以简化为上述情况)。 In those times the assignment operator seem to be actually 'shallow' copying.
在那些时候,赋值运算符似乎实际上是“浅”复制。 Why?
为什么?
Thanks a lot!非常感谢!
I think, that using assignment is not the best way of matrix copying.我认为,使用赋值并不是矩阵复制的最佳方式。 If you want new full copy of the matrix, use:
如果您想要矩阵的新完整副本,请使用:
Mat a=b.clone();
If you want copy matrix for replace the data from another matrix (for avoid memory reallocation) use:如果您想要复制矩阵来替换另一个矩阵中的数据(以避免内存重新分配),请使用:
Mat a(b.size(),b.type());
b.copyTo(a);
When you assign one matrix to another, the counter of references of smart pointer to matrix data increased by one, when you release matrix (it can be done implicitly when leave code block) it decreases by one.当您将一个矩阵分配给另一个矩阵时,指向矩阵数据的智能指针的引用计数器增加一,当您释放矩阵时(可以在离开代码块时隐式完成)它减少一。 When it becomes equal zero the allocated memory deallocated.
当它变为零时,分配的内存被释放。
If you want get result from the function use references it is faster:如果你想从函数使用引用中得到结果,它会更快:
void Func(Mat& input,Mat& output)
{
somefunc(input,output);
}
int main(void)
{
...
Mat a=Mat(.....);
Mat b=Mat(.....);
Func(a,b);
...
}
I've been using OpenCV for a while now and the cv::Mat
confused me too, so I did some reading.我已经使用 OpenCV 一段时间了,
cv::Mat
也让我感到困惑,所以我做了一些阅读。
cv::Mat
is a header that points to a *data
pointer which holds the actual image data. cv::Mat
是一个标头,指向一个*data
指针,该指针包含实际的图像数据。 It also implements reference counting.它还实现了引用计数。 it holds the number of
cv::Mat
headers currently pointing to that *data
pointer.它保存当前指向该
*data
指针的cv::Mat
标头的数量。 So when you do a regular copy such as:因此,当您进行常规复制时,例如:
cv::Mat b;
cv::Mat a = b;
a
will point to b
's data and the reference count for it will be incremented. a
将指向b
的数据,并且它的引用计数将增加。 At the same time, the reference count for the data previously pointed to by b
will be decremented (and the memory will be freed if it is 0 after decrementing).同时,
b
之前指向的数据的引用计数会递减(递减后为0则释放内存)。
Question 1: It depends on your program.问题 1:这取决于您的程序。 Please refer to this question for more details: is-cvmat-class-flawed-by-design
有关更多详细信息,请参阅此问题: is-cvmat-class-flawed-by-design
Question 2: the function returns by value.问题2:函数按值返回。 That means
return image
will copy the Mat and increase the ref count(now ref_count = 2
) and return the new Mat.这意味着
return image
将复制 Mat 并增加引用计数(现在ref_count = 2
)并返回新的 Mat。 When the function ends, the image will be destroyed and ref_count will be reduced by one.当函数结束时,图像将被销毁并且 ref_count 将减一。 But the memory will not be freed since the
ref_count
is not 0. So the returned cv::Mat
is not pointing to random memory location.但是内存不会被释放,因为
ref_count
不是 0。所以返回的cv::Mat
没有指向随机内存位置。
Question 3: A similar thing happens.问题3:类似的事情发生了。 When you say
orgImage2.copyTo(aCopy);
当你说
orgImage2.copyTo(aCopy);
The ref_count for the data pointed to by aCopy
will be decreased. aCopy 指向的数据的
aCopy
将减少。 Then new memory is allocated to store the new data that will be copied.然后分配新的内存来存储将要复制的新数据。 So That is why
copyCopy1
was not modified when you did this.这就是为什么在您执行此操作时
copyCopy1
原因。
Take a look at c++11 std::shared_ptr effectively works in the same way, by using a reference counter cv::Mat cleverly remembers every time the pointer is referenced, once the count reaches 0 it is automatically released ie memory is deallocated and cv::Mat is no longer available.看看 c++11 std::shared_ptr以同样的方式有效地工作,通过使用引用计数器 cv::Mat 巧妙地记住每次引用指针时,一旦计数达到 0,它就会自动释放,即内存被释放并且 cv::Mat 不再可用。 This is effectively a "shallow copy" and saves resources in allocating/deallocating large amounts of memory.
这实际上是一个“浅拷贝”,并在分配/取消分配大量内存时节省了资源。
On the other hand cv::Mat::clone will provide a "deep copy" that allocates a whole new block of memory for the matrix to reside in, this can be useful if you are making transformations to an image that you may want to undo however, more memory allocating/deallocating increases the amount of resources required.另一方面, cv::Mat::clone 将提供一个“深拷贝”,为矩阵分配一个全新的内存块,如果您正在对您可能想要的图像进行转换,这可能很有用然而,撤消更多的内存分配/解除分配会增加所需的资源量。
Hope this helps someone.希望这可以帮助某人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.