在C ++ OpenCV中使用与输入和输出相同的变量是否安全? [英] Is it safe to use the same variable as input and output in 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中的标准功能,例如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.
内部,像这样的函数:
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
是:
- 相同图像,
create
实际上不做任何事情,并且修改有效地是就地.如果无法进行适当的操作(例如OpenCV> 3.2中的findConturs
)以确保正确的行为,则某些功能可能会在内部clone
src
图像内部. - 不同的图像,
create
将创建一个新的矩阵dst
,而无需修改src
.
- 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.
Thanks to Fernando Bertoldi for reviewing the answer
这篇关于在C ++ OpenCV中使用与输入和输出相同的变量是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!