1

I am facing a slow execution time of the FIRST usage of the opencv (3.0) resize funtion (using visual studio on Windows). The following simple program shows the problem:

int _tmain(int argc, _TCHAR* argv[])
{
   DECLARE_TIMING(ttt);
   START_TIMING(ttt);
   cv::Mat tmp1=cv::Mat::ones(100,100, CV_8UC3);
   cv::Mat res1=cv::Mat::zeros(100*0.25, 100*0.25, CV_8UC3);
   cv::resize(tmp1, res1, cv::Size(0,0), 0.25f, 0.25f, CV_INTER_AREA);
   STOP_TIMING(ttt);
   double runTime = GET_TIMING(ttt);
   std::cout << "First resize run time = " << runTime << " mSec\n"; 

   START_TIMING(ttt);
   cv::Mat tmp2=cv::Mat::ones(100,100, CV_8UC3);
   cv::Mat res2=cv::Mat::zeros(100*0.25, 100*0.25, CV_8UC3);
   cv::resize(tmp2, res2, cv::Size(0,0), 0.25f, 0.25f, CV_INTER_AREA);
   STOP_TIMING(ttt);
   runTime = GET_TIMING(ttt);
   std::cout << "Second resize run time = " << runTime << " mSec\n";    

   return 0;

}

The result is:

First resize run time = 259.575 mSec
Second resize run time = 0.0769735 mSec

Now why does the first resize usage takes 259 msec while the second takes way less ?? (note, I am aware that the pre allocations of res1 and res2 is not needed, this was part of my effort to overcome the issue)

Uzi
  • 119
  • 1
  • 7

1 Answers1

1

I suspect that this has to do with the static variables initialization inside the function cv::resize.

static ResizeFunc linear_tab[] = ...
static ResizeFunc cubic_tab[] = ...
static ResizeFunc lanczos4_tab[] =
static ResizeAreaFastFunc areafast_tab[] = ...
static ResizeAreaFunc area_tab[] = ...

The static variables are initialized the first time execution hits their declaration.

This is a snippet equivalent to your, without macro or Windows related stuff. You can see that if you de-comment the first dummy call to resize, the execution time is nearly the same for the following calls.

#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    // Dummy call to initialize static variables.
    //resize(Mat1b(1, 1), Mat1b(1, 1), Size(1, 1));

    cv::Mat tmp1 = cv::Mat::ones(100, 100, CV_8UC3);
    cv::Mat res1 = cv::Mat::zeros(100 * 0.25, 100 * 0.25, CV_8UC3);

    double tic1 = double(getTickCount());
    cv::resize(tmp1, res1, cv::Size(0, 0), 0.25f, 0.25f, CV_INTER_AREA);
    double toc1 = (double(getTickCount()) - tic1) * 1000.0 / getTickFrequency();
    std::cout << "First resize run time = " << toc1 << " ms" << std::endl;


    cv::Mat tmp2 = cv::Mat::ones(100, 100, CV_8UC3);
    cv::Mat res2 = cv::Mat::zeros(100 * 0.25, 100 * 0.25, CV_8UC3);

    double tic2 = double(getTickCount());
    cv::resize(tmp2, res2, cv::Size(0, 0), 0.25f, 0.25f, CV_INTER_AREA);
    double toc2 = (double(getTickCount()) - tic2) * 1000.0 / getTickFrequency();
    std::cout << "Second resize run time = " << toc2 << " ms" << std::endl;

    getchar();

    return 0;
}
Community
  • 1
  • 1
Miki
  • 40,887
  • 13
  • 123
  • 202
  • Thanks. This dummy resize takes 260 msec to run!.... I think there is a need to look at a solution to this issue within opencv. – Uzi Feb 28 '16 at 12:54
  • This is not an OpenCV issue, but how static variables works in C++. Remember that OpenCV is not meant to be a real time library. Also, remember to run in release mode. For me, it takes just a few milliseconds for the first call. Not a big deal. – Miki Feb 28 '16 at 12:58