2013-06-13 119 views
1

我有這個非常奇怪的問題,我想我可能會做一些錯誤的事情,但是我有一個用於Pyramidal Lucas Kanade和opencv2實現的opencv1實現。不同之處在於opencv2比opencv1需要更長的時間運行(尤其是goodFeaturesToTrack函數)。另外,在opencv1中包含opencv2庫和頭文件會導致該文件變得非常慢(我們談論的是每兩張圖像0.002秒,而每張兩張圖像只有1秒)。難道我做錯了什麼?goodFeaturesToTrack OpenCV 2.4與Opencv1相比非常慢

Windows 7,64位。這是opencv2代碼,運行速度非常慢,大約每秒1幀。正如我所說的,採用opencv1實現和切換庫版本會導致相同的減慢10倍或更多。我認爲這很奇怪,谷歌沒有提供任何信息!謝謝!!!

#include <opencv2/opencv.hpp> 
#include <iostream> 
#include <vector> 
#include <cmath> 

using namespace cv; 
using namespace std; 

int64 now, then; 
double elapsed_seconds, tickspersecond=cvGetTickFrequency() * 1.0e6; 
int main(int argc, char** argv) 
{ 
    // Load two images and allocate other structures 
    Mat imgA = imread("0000.png", CV_LOAD_IMAGE_GRAYSCALE); 
    Mat imgB = imread("0001.png", CV_LOAD_IMAGE_GRAYSCALE); 
    Size img_sz = imgA.size(); 
    Mat imgC(img_sz,1); 

    int win_size = 15; 
    int maxCorners = 100; 
    double qualityLevel = 0.05; 
    double minDistance = 2.0; 
    int blockSize = 3; 
    double k = 0.04; 
    std::vector<cv::Point2f> cornersA; 
    cornersA.reserve(maxCorners); 
    std::vector<cv::Point2f> cornersB; 
    cornersB.reserve(maxCorners); 

then = cvGetTickCount(); 
    goodFeaturesToTrack(imgA,cornersA,maxCorners,qualityLevel,minDistance,cv::Mat(),blockSize,true); 
    goodFeaturesToTrack(imgB,cornersB,maxCorners,qualityLevel,minDistance,cv::Mat(),blockSize,true); 

now = cvGetTickCount(); 
cout << (double)(now - then)/tickspersecond; 


    cornerSubPix(imgA, cornersA, Size(win_size, win_size), Size(-1, -1), 
        TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03)); 

    cornerSubPix(imgB, cornersB, Size(win_size, win_size), Size(-1, -1), 
        TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03)); 

    // Call Lucas Kanade algorithm 

    CvSize pyr_sz = Size(img_sz.width+8, img_sz.height/3); 

    std::vector<uchar> features_found; 
    features_found.reserve(maxCorners); 
    std::vector<float> feature_errors; 
    feature_errors.reserve(maxCorners); 

    calcOpticalFlowPyrLK(imgA, imgB, cornersA, cornersB, features_found, feature_errors , 
     Size(win_size, win_size), 5, 
     cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.3), 0); 

    // Make an image of the results 

    for(int i=0; i < features_found.size(); i++){ 
     // cout<<"Error is "<<feature_errors[i]<<endl; 
      //continue; 

     //cout<<"Got it"<<endl; 
     Point p0(ceil(cornersA[i].x), ceil(cornersA[i].y)); 
     Point p1(ceil(cornersB[i].x), ceil(cornersB[i].y)); 
     line(imgC, p0, p1, CV_RGB(255,255,255), 2); 
    } 

    namedWindow("ImageA", 0); 
    namedWindow("ImageB", 0); 
    namedWindow("LKpyr_OpticalFlow", 0); 

    imshow("ImageA", imgA); 
    imshow("ImageB", imgB); 
    imshow("LKpyr_OpticalFlow", imgC); 

    cvWaitKey(0); 

    return 0; 
} 
+0

你的cv1實現有什麼不同?順便說一句'cvGoodFeaturesToTrack()'只是'cv :: goodFeaturesToTrack()'的一個包裝。 – Bull

+0

cv1 implmentation是相似的,但使用在線找到的另一個常見示例:cvGoodFeaturesToTrack有一些虛擬輸入,如eig_image(來自http://robots.stanford.edu/cs223b05/notes/CS%20223-B%20T1%20stavens_opencv_optical_flow.pdf),它使用IplImage而不是MAT。但正如我所說,實現似乎無關緊要,因爲從opencv1將包含庫更改爲opencv2,仍然使用opencv1實現(向後兼容)會導致相同的緩慢。 –

+0

獅子座你有沒有找到解決這個問題的辦法?我遇到類似的事情 –

回答

-2

你爲什麼要調用goodFeaturesToTrack兩次?

調用一次獲取角點然後使用LK識別imgB中的相同角/特徵。

0

您可能正在使用調試庫(* d.lib)而不是發行版。對於goodFeaturesToTrack(),每次調用〜1-2s都有同樣的問題,並且切換到發佈版解決了它。

相關問題