4

I've started to use OpenCV with Visual C++ 2010 Express, because it was supposed to be faster than MATLAB.

In order to do a fair comparison between both, I'm running a program where I convert a RGB image to its gray scale correspondent and I calculate the conversion image space operation elapsed time.

Using cvtColor command to do the task in C++ Release, it takes me around 5 ms, average. Doing the same operation in MATLAB, takes me more or less the same average time (the codes are bellow).

I already tested, and both programs are working fine.

Does anybody have any idea if I can improve the OpenCV speed?

C++ code.

#include <opencv2/highgui/highgui.hpp> 
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <windows.h>

using namespace cv;
using namespace std;

double PCFreq = 0.0;
__int64 CounterStart = 0;

void StartCounter()
{
    LARGE_INTEGER li;
    if(!QueryPerformanceFrequency(&li))
    cout << "QueryPerformanceFrequency failed!\n";

    PCFreq = double(li.QuadPart)/1000.0;

    QueryPerformanceCounter(&li);
    CounterStart = li.QuadPart;
}

double GetCounter()
{
    LARGE_INTEGER li;
    QueryPerformanceCounter(&li);
    return double(li.QuadPart-CounterStart)/PCFreq;
}

int main()
{
    double time;
    Mat im, result;
    im = imread("C:/Imagens_CV/circles_rgb.jpg");
    StartCounter();
    cvtColor(im,result, CV_BGR2GRAY);
    time = GetCounter();
    cout <<"Process time: "<< time << endl;
} 

MATLAB code

tic
img_gray = rgb2gray(img_rgb);
toc
5
  • FYI: in terms of timing, it's better to compute the average time over a number of times. Commented Nov 3, 2014 at 13:49
  • Yes, I have done this. 5 ms is the average time. I added this information in the question. Commented Nov 3, 2014 at 13:50
  • 6
    Why should matlab be slower in this case? If it provides a function, it likely has an optimized implementation of it. What is slow is programming in matlab, doing explicit loops, etc. Commented Nov 3, 2014 at 13:58
  • The matlab code you are using, eventually uses a Mex file (ore something like that), in such case the speed would be more or less the same. You can also compare with OpenCV in Python ;) Commented Nov 3, 2014 at 14:38
  • you'll be happy to know that MathWorks is making it easier to call OpenCV from MATLAB if you wish (by writing MEX-functions): mathworks.com/help/vision/ug/opencv-interface.html (this is new in R2014b CSVT-toolbox). There is also the excellent mexopencv package. Commented Nov 5, 2014 at 3:10

2 Answers 2

1

Color conversion in OpenCV will make extensive use of Intel IPP if it is available at compile time. See modules\imgproc\src\color.cpp. More info from Intel. Note that this code has no OpenMP pragmas or TBB code, so that won't help here.

The exciting bit is that Intel has granted OpenCV the right to use a subset of IPP for free, incuding these functions. See the third item in the release summary for more info. But you need to use at least OpenCV 3.0 to get this free functionality; otherwise you need to compile with your own copy of IPP.

enter image description here

Clearly, cvtColor (far left) does not benefit much, but it gets a little boost. Other functions do much better.

Sign up to request clarification or add additional context in comments.

Comments

1

If you follow the call to rgb2gray function in MATLAB (edit rgb2gray.m), you'll find that it eventually calls a private MEX-function imapplymatrixc.mexw64 implemented in C++.

In fact if you load this shared library into a tool like "Dependency Walker", you'll see it has a dependency on tbb.dll which indicates the function is multi-threaded using Intel TBB library.

While it doesn't seem to be the case for color conversion functions, the Image Processing Toolbox does use Intel IPP library for some of its image arithmetic functions (there is a setting you can control to enable/disable the use of "hardware optimization" ippl: iptsetpref('UseIPPL', true)).

In addition, there is a version of the function that runs on the GPU (CUDA) when using gpuArray input arrays (edit gpuArray>rgb2gray.m). This requires the Parallel Computing Toolbox.

So it is safe to say the function is well optimized!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.