我面临着第一次使用opencv(3.0)调整大小功能的缓慢执行时间(在Windows上使用visual studio)。以下简单程序显示了问题:
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;
}
结果是:
First resize run time = 259.575 mSec
Second resize run time = 0.0769735 mSec
现在为什么第一次调整大小的时间需要259毫秒,而第二次调整则需要更少? (注意,我知道不需要res1和res2的预分配,这是我努力克服这个问题的一部分)
答案 0 :(得分:1)
我怀疑这与函数static
内的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
这是一个相当于您的代码段,没有宏或Windows相关的东西。您可以看到,如果您将第一个虚拟调用取消注释为resize
,则以下调用的执行时间几乎相同。
#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;
}