问题描述
OpenCV函数 cvtColor 可以转换矩阵(例如,从RGB到灰度).该函数的C ++签名是
The OpenCV function cvtColor converts the color space of a matrix (e.g. from RGB to grayscale). The function's C++ signature is
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )
是否可以使用此函数转换矩阵,即与src
相同的对象与dst
相同?
Can this function be used convert a matrix in place, i.e. with src
the same object as dst
?
cv::Mat mat = getColorImage();
cvtColor(mat, mat, CV_RGB2GRAY);
(我知道,无论哪种方式,由于目标的通道数量与源的通道数量不同,因此仍然需要为目标分配新的内存块.)
(I'm aware that either way, as the destination has a different number of channels than the source, it will still need to allocate a new block of memory for the destination.)
更笼统地说,OpenCV API中是否有约定来确定何时可以以这种方式使用功能?
More generally, is there a convention within the OpenCV API to determine when a function may be used in this way?
推荐答案
也许回答还为时已晚,但是我想说我不同意这里写的一些东西.
即使目标矩阵没有相同数量的通道",或者即使尚未创建目标矩阵,也可以将与源和目标相同的Mat放在一样的位置,没有问题.
OpenCV程序员对其进行了彻底的设计.
该函数的所有用户要做的是,注意源Mat在通道数和数据类型方面是正确的,并记住在函数调用之后它们可能会更改.
Maybe it’s too late for answering, but I want to say that I don’t agree with some things that have been written here.
You can place the same exact Mat as source and destination, with no problem, even if the destination matrix has not "the same number of channel", or even if the dest matrix has not been created yet.
OpenCV programmers designed it thoroughly.
All users of this function have to do is to take care that the source Mat is correct, as to number of channels and type of data, and remember that after the function call they could change.
证据来自查看源代码,在2406行上,只是cv::cvtColor(…)
函数内的第一行,
The proof comes from looking at source code, on line 2406, just the first line inside the cv::cvtColor(…)
function,
Mat src = _src.getMat();
被调用,然后创建Mat dst(和dst = _dst = _scr).
因此,在进行就地调用时,cv::cvtColor(…)
内的情况如下:src指向旧矩阵,_src,_dst,dst都指向同一新分配的矩阵,这将成为目标矩阵.
这意味着现在新的变量src和dst(不是来自函数调用_src和_dst的变量)已准备好传递给实际的转换函数.
完成函数void cv::cvtColor(…)
后,将放置src并将_src,_dst和dst都指向相同的Mat,并且_dst的引用计数将变为1.
is called, then Mat dst is created (and dst=_dst=_scr).
So the situation inside cv::cvtColor(…)
is the following when making in-place calling: src points to the old matrix, _src, _dst, dst all point to the same new allocated matrix, which will be the destination matrix.
This means than now new variables src and dst (not the ones from function call _src and _dst) are ready to be passed to the real converting function.
When function void cv::cvtColor(…)
is done, src is disposed and _src, _dst and dst all point to the same Mat and refcount of _dst will go to 1.
这篇关于OpenCV函数cvtColor可以用于转换矩阵吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!