问题描述
很多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 中的标准函数,如 threshold
、blur
等.所以我认为它们应该已经相应地实现了,对吧?
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
所以,如果 src
和 dst
是:
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 mayclone
thesrc
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 matrixdst
without modifyingsrc
.
文档说明此默认行为不适用.
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 中使用与输入和输出相同的变量是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!