Optimize matrix row copying in OpenCV

asked 2015-06-20 03:54:08 -0600

I have a [32678 x 10] matrix (w2c) and I want to copy 24700 rows of it to another matrix(out). I have the index of the rows to be copied in a vector(index). For doing this in matlab I do:

out = w2c(index_im,:);

It takes approximately 0.002622 seconds.

In OpenCV:

Mat out(index.cols, w2c.cols, w2c.type());
for (int i = 0; i < index.cols; ++i) {
    w2c.row(index.at<int>(i) - 1).copyTo(out.row(i));
}

It takes approximately 0.015121 seconds.

As you can see Matlab is 6 times faster. How can I make the OpenCV code efficient?

edit retag flag offensive close merge delete

Comments

is your index linear ? in that case you could just copy a ROI, instead of multiple rows.

berak gravatar imageberak ( 2015-06-20 04:11:49 -0600 )edit

no they are random numbers

sudomakeinstall2 gravatar imagesudomakeinstall2 ( 2015-06-20 04:35:58 -0600 )edit

Hm, maybe you could sort the random numbers in advance, then the process could easier be cached. If that is really a bottleneck for you you could also copy these memory parts yourself using memcpy... (internally OpenCV is of course using this but it additionally applies bounding checks).

Guanta gravatar imageGuanta ( 2015-06-20 08:02:53 -0600 )edit