在 C++ OpenCV 中使用与输入和输出相同的变量是否安全? [英] Is it safe to use the same variable as input and output in C++ OpenCV?

查看:40
本文介绍了在 C++ OpenCV 中使用与输入和输出相同的变量是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多OpenCV函数被定义为

A lot of OpenCV functions are defined as

function(InputArray src, OutputArray dst, otherargs..)

所以如果我想处理和覆盖同一张图片,我可以这样做吗:

So if I want to process and overwrite the same image, can I do this:

function(myImg, myImg);

这样做安全吗?

谢谢

我要求 OpenCV 中的标准函数,如 thresholdblur 等.所以我认为它们应该已经相应地实现了,对吧?

I'm asking for the standard functions in OpenCV like threshold, blur etc. So I think they should have been implemented accordingly, right?

推荐答案

是的,在 OpenCV 中是安全的.

Yes, in OpenCV it is safe.

在内部,类似这样的函数:

Internally, a function like:

void somefunction(InputArray _src, OutputArray _dst);

会做类似的事情:

Mat src = _src.getMat();
_dst.create( src.size(), src.type() );
Mat dst = _dst.getMat();

// dst filled with values

所以,如果 srcdst 是:

So, if src and dst are:

  • 相同图像,create实际上不会做任何事情,并且修改实际上是就地.如果操作不能就地进行(例如 OpenCV > 3.2 中的 findConturs),某些函数可能会在内部 clone src 图像以保证正确的行为.
  • 不同的图像,create会在不修改src的情况下创建一个新的矩阵dst.
  • the same image, create won't actually do anything, and the modifications are effectively in-place. Some functions may clone the src image internally if the operation cannot be in-place (e.g. findConturs in OpenCV > 3.2) to guarantee the correct behavior.
  • different images, create will create a new matrix dst without modifying src.

文档说明此默认行为不适用.

Documentation states where this default behavior doesn't hold.

一个值得注意的例子是 findContours,它修改了 src 矩阵.您通常会在输入中传递 src.clone() 来应对这种情况,这样只会修改克隆的矩阵,而不会修改您从中克隆的矩阵.

A notable example is findContours, that modify the src matrix. You cope with this usually passing src.clone() in input, so that only the cloned matrix is modified, but not the one you cloned from.

从 OpenCV 3.2 开始,findContours 不会修改输入图像.

From OpenCV 3.2, findContours doesn't modify the input image.

感谢 Fernando Bertoldi 查看答案

Thanks to Fernando Bertoldi for reviewing the answer

这篇关于在 C++ OpenCV 中使用与输入和输出相同的变量是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆