1

我是通过以下方式做到的

Mat temp, rp;    
for (int i = 0; i <= descriptors1.rows - 1; i++)
{
    normalize(descriptors1.row(i), temp);
    rp = normd1.rowRange(i, i+1);
    temp.copyTo(rp);
}

上面的描述符 1 是一个 Mat。

但是使用循环非常耗时。有没有更好的方法来做到这一点?

问候。

4

1 回答 1

1

There is nothing wrong with loops. They're not slow in C++ unlike what you might have heard about Matlab! If this is going to be used in an extremely time sensitive application then you can optimize the loop to remove the i <= descriptors1.rows - 1 check in each iteration. Using temp is also unnecessary, you can pass normd1.rowRange to normalize directly to save time and do it in just one line.

Edit: You can use this method normalize(A.row(i), _OutputArray(B.ptr(i), B.cols)), or the template version of _OutputArray. In the way you've written the loop in each iteration descriptors1.rows - 1 should be evaluated as the compiler sees rows is not constant and most likely doesn't understand the functions in the loop do not change it. So I would change it to something like:

for (i = descriptors1.rows - 1; i >= 0; i--)

Though, you need to check the code generated by your compiler to make sure it's optimized, and also consider loop unrolling. Google can find you a lot of material on optimizing loops if this is really the bottleneck of your code.

于 2012-05-21T09:17:44.377 回答