2016-02-28 42 views
1

我面臨第一次使用opencv(3.0)調整大小函數(使用Windows上的visual studio)的執行速度緩慢。下面的簡單程序顯示問題:緩慢的opencv調整大小函數執行時間

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

嫌疑這與內部的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; 
} 
+0

謝謝。這個虛擬調整大小需要260毫秒才能運行!....我認爲有必要在opencv中查看這個問題的解決方案。 – Uzi

+0

這不是一個OpenCV問題,但靜態變量如何在C++中起作用。請記住,OpenCV並不是一個實時庫。另外,請記住以發佈模式運行。對我來說,第一次通話只需要幾毫秒。沒什麼大不了的。 – Miki