1

我有这个非常奇怪的问题,我想我可能做错了什么,但我有一个用于 Pyramidal Lucas Kanade 的 opencv1 实现和一个 opencv2 实现。不同之处在于 opencv2 与 opencv1 相比需要更长的时间来运行(特别是 goodFeaturesToTrack 函数)。此外,在 opencv1 实现中包含 opencv2 库和头文件也会导致该库变得非常慢(我们说的是每两个图像 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;
}
4

2 回答 2

0

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

于 2014-06-18T12:49:51.083 回答
-1

你为什么要调用 goodFeaturesToTrack 两次?

调用一次得到cornersA,然后使用LK识别imgB中相同的corner/features。

于 2013-10-04T19:58:19.733 回答