goodFeaturesToTrack OpenCV 2.4与Opencv1相比速度极慢

时间:2013-06-13 02:33:43

标签: c++ visual-studio-2010 debugging opencv

我有这个非常奇怪的问题,我想我可能做错了,但我有一个针对Pyramidal Lucas Kanade的opencv1实现和一个opencv2实现。不同之处在于opencv2需要花费更长的时间来运行(特别是goodFeaturesToTrack函数)与opencv1相比。此外,在opencv1 implmentation中包含opencv2 libs和header会导致一个变得非常慢(我们说的是每两个图像0.002秒而每两个图像1秒)。我做错了吗?

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;
}

2 个答案:

答案 0 :(得分:0)

您可能正在使用调试库(* d.lib)而不是发布库。我有同样的问题,每次调用goodFeaturesToTrack()约1-2次,切换到发布解决了它。

答案 1 :(得分:-2)

为什么要两次调用goodFeaturesToTrack?

调用一次以获得角落A,然后使用LK识别imgB中的相同角落/功能。