0

I'm writing a Qt GUI program in conjuction with OpenCV to create a people tracking application. OpenCV has a lot of functions that take a matrix as input and output, for example a color conversion one:

cvtColor(inputArray src, outputArray dst, int code, int dstCn=0);

Mat is the default matrix class of OpenCV, and assuming I have a Mat object called frame, I would implement the function like this to change its properties:

cvtColor(frame, frame, CV_RGB2HSV,0);

Is there any downside on using the same variable as input and output on any function? or should I create a copy first?

or should I look in every function documentation?

4

2 回答 2

0

这是个人选择。如果您以后不需要输入图像,那么一定要这样做。实际上我已经尝试过了,它大部分时间都有效,但其他时候效果不佳。这取决于您使用的功能。

于 2013-04-03T08:26:26.110 回答
0

我认为这是个人选择。以下是一些个人样本:

获取只读输入、对输出的引用,并在处理输入后将其推送到输出。可选的错误结果友好。

bool function(const type& input, type& output){
    output = input;
    return true;
}

获取只读输入,将其分配给输出,更改输出并返回它(C++11 移动优化)。

type function(const type& input){
    type output = input;
    return output;
}
type output = function(input);

在这里,您强制对象的新副本作为参数,您可以使用它并返回它(当您不想更改副本时最好使用 C++11)。另请参阅:想要速度,按价值传递.

type function(type input){
    // modify input
    return input;
}
type output = function(input);
  1. 在同一个参数中输入和输出。谨慎使用。可以返回错误。

    bool function(type& put){ return true; }

这取决于您的设计。不需要错误?...使用可移动的。需要错误吗?...使用一个让您可以访问返回值的错误。或者,只需遵循现有做法,但要知道每种做法如何帮助或惩罚您的表现,例如

// this makes a pointless copy for the input (use const ref here)
bool function(type input, type& output){
    output = input;
    return true;
}

(正确的方法是)

bool function(const type& input, type& output){
    output = input;
    return true;
}

PS : 自学 C++ 开发人员的个人意见 :)

于 2013-03-14T18:50:23.850 回答