2010-07-28 102 views
2

我試圖順利顯示25fps的視頻文件,沒有任何延遲。下面的代碼是這樣做的,但只能達到10fps左右,大約需要0.1ms才能執行。使用cvWaitKey(1)我會得到0.03到0.04毫秒左右,這將是完美的,但命名窗口只是保持灰色,並不顯示視頻!使用cvShowImage滯後視頻顯示()

這是因爲cvShowImage()太慢?有沒有其他方法可以加速代碼並順利輸出視頻?

看到我的代碼如下。

非常感謝提前,

阿德里安

#include <cv.h> 
#include <iostream> 
#include <highgui.h> 
#include <cxcore.h> 
#include <cvaux.h> 
#include <sstream> 
#include <time.h> 
using namespace std; 
using namespace cv; 

int main(int argc, char** argv) 
{ 
    CvCapture* vid = 0; 

    IplImage* input; //Input image 

    int fps; 
    int i=0; 

    clock_t start, end; 

    /*Creates the GUI to output the processed images*/ 
    cvNamedWindow("Video input", 0); 

    /*Open input video*/ 
    if (argc!=2) 
    { 
cout << "Please specify an input video." << endl; 
return -1; 
    } 
    else 
    { 
vid=cvCreateFileCapture(argv[1]); 
if (!vid) 
{ 
    cout << "Could not extract frame." << endl; 
    return -1; 
} 
    } 

    input = cvQueryFrame(vid); 

    fps = (int)cvGetCaptureProperty(vid, CV_CAP_PROP_FPS); 
    cout << fps << endl; 

    cout << "Video found." << endl; 
    /*Extraction loop */ 
    while (input) 
    { 
start = clock(); 

cout << flush; 
cout << i << "\r"; 

i++; 

/*Show image*/ 
cvShowImage("Video input", input); 

cvWaitKey(2); //Wait is needed or else we see a grey box 

input = cvQueryFrame(vid); //Read next frame from video 
end = clock(); 
cout << (double)(end-start)/CLOCKS_PER_SEC << " s" << endl; 
    }; 

    /*Release the allocated memory for the frames */ 
    cvReleaseImage(&input); 
    cvDestroyWindow("Video input"); 
    return 1; 
} 

回答

1

您是否嘗試過這個沒有所有的COUT的東西?

Microsoft STL的調試版本有cout處理,這幾乎是令人難以置信的緩慢。

+0

呀,這並加快步伐很多!順便說一句,我沒有使用微軟STL,但g ++。 有沒有人有任何想法,爲什麼我不能使用cvWaitKey(1)? – Adrian 2010-07-28 12:55:14

+0

你可以......我剛剛在我正在研究的程序中做過。 – 2010-07-28 19:38:37

+0

絕對不適合我,我只是在使用cvWaitKey(1)時出現灰色屏幕。我想它必須與這個特定的構建做... – Adrian 2010-07-29 11:04:10

1

嘗試調用cvWaitKey 1000/fps的希望,你的情況:

cvWaitKey(1000至1025年)

+0

如果我沒有做任何處理,這將工作正常。但是既然我是這樣,這會讓它變得更慢。 – Adrian 2010-07-29 11:07:07

0

你可以嘗試這樣的:

char key = cvWaitKey(10); //waits 10 milliseconds 

if (key == 27)   //and if ESC is pressed, get out of the loop 
    break;