Ask Your Question
1

cv::merge execution time on CV_32S

asked 2016-11-25 11:09:38 -0600

strann gravatar image

updated 2016-11-25 11:14:01 -0600

Hello,

I used following code snippet to merge a few single channel images into a multichannel Mat:

vector<cv::Mat> images(numcomps);
for (int i = 0; i < numcomps; i++)
    images[i] = cv::Mat(h, w, CV_32SC1, (image->comps + comps[i])->data);
cv::merge(images, imageMat);

On my machine this code execution time is about 500 ms (!!!), regardless of number of elements in vector<cv::Mat> images (i.e. even if numcomps == 1). What can be the reasons behind such an overhead?

If I rewrite code as follows it reduces execution time to 2 ms:

cv::Mat result(h, w, CV_MAKETYPE(CV_32S, numcomps));
vector<cv::Mat> images(numcomps);
for (int i = 0; i < numcomps; i++)
{
    int from_to[] = { 0,i };
    images[i] = cv::Mat(h, w, CV_32SC1, (image->comps + comps[i])->data);
    cv::mixChannels(&images[i], 1, &result, 1, from_to, 1);
}
imageMat = result;

I'm using OpenCV 3.1.0 built with CUDA 7.5 (Ubuntu 14.04, Intel® Core™ i3-4170 CPU @ 3.70GHz × 4, GeForce GTX 960/PCIe/SSE2 ).

Cheers, Kate

edit retag flag offensive close merge delete

Comments

1

How did you measure this? In OpenCV 3 on first function call, OpenCL code will be initialized if existing, even if you use Mat instead of UMat. The execution time should be OK on the next calls.

In your first code snippet, I think merge(InputArrayOfArrays _mv, OutputArray _dst) is called, which contains OpenCL code.

Your second code snippet calls mixChannels( const Mat* src, size_t nsrcs, Mat* dst, size_t ndsts, const int* fromTo, size_t npairs ) which do not contain OpenCL code

try #include<opencv2/core/ocl.hpp> and cv::ocl::setUseOpenCL(false) before calling functions.

matman gravatar imagematman ( 2016-11-25 11:59:29 -0600 )edit

thank you, that helped!

strann gravatar imagestrann ( 2016-11-30 04:30:34 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2016-11-30 06:14:43 -0600

strann gravatar image

updated 2016-11-30 06:16:02 -0600

Thanks @matman for an answer!

The overhead was caused by OpenCL code initialization. I added #include<opencv2/core/ocl.hpp> and switched off OpenCL usage before calling merge(InputArrayOfArrays _mv, OutputArray _dst) . Now it works as I expected.

cv::ocl::setUseOpenCL(false)
vector<cv::Mat> images(numcomps);
for (int i = 0; i < numcomps; i++)
    images[i] = cv::Mat(h, w, CV_32SC1, (image->comps + comps[i])->data);
cv::merge(images, imageMat);
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-11-25 11:09:38 -0600

Seen: 319 times

Last updated: Nov 30 '16