我想用OpenCV在C++中讀取視頻,但是當顯示視頻時,幀率非常慢,就像原始幀率的10%。opencv video reading slow framerate
整個代碼是在這裏:
// g++ `pkg-config --cflags --libs opencv` play-video.cpp -o play-video
// ./play-video [video filename]
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
// video filename should be given as an argument
if (argc == 1) {
cerr << "Please give the video filename as an argument" << endl;
exit(1);
}
const string videofilename = argv[1];
// we open the video file
VideoCapture capture(videofilename);
if (!capture.isOpened()) {
cerr << "Error when reading video file" << endl;
exit(1);
}
// we compute the frame duration
int FPS = capture.get(CV_CAP_PROP_FPS);
cout << "FPS: " << FPS << endl;
int frameDuration = 1000/FPS; // frame duration in milliseconds
cout << "frame duration: " << frameDuration << " ms" << endl;
// we read and display the video file, image after image
Mat frame;
namedWindow(videofilename, 1);
while(true)
{
// we grab a new image
capture >> frame;
if(frame.empty())
break;
// we display it
imshow(videofilename, frame);
// press 'q' to quit
char key = waitKey(frameDuration); // waits to display frame
if (key == 'q')
break;
}
// releases and window destroy are automatic in C++ interface
}
我試圖從GoPro的英雄3+的視頻,並與我的MacBook上的攝像頭的視頻,同樣的問題,這兩個視頻。這兩個視頻都可以通過VLC播放。
在此先感謝。
does .get(CV_CAP_PROP_FPS)給你正確的fps?你的waitKey太大了! imshow也需要相當多的cimputation(例如,不能通過imshow獲得100fps顯示)。並記住waitKey是不準確的,特別是如果你選擇了小的等待時間。更好地切換到一些「更好」的渲染,例如像使用openGL或directShow的qt。 openCV gui比最終用戶gui更適合測試! – Micka
是的,'.get(CV_CAP_PROP_FPS)'給了我正確的FPS。我的錯誤是,我沒有想到抓住框架並顯示它將需要這麼多的計算。感謝您指出了這一點! – vmarquet
我用下面的答案來計算獲取幀的時間(不包括顯示時間),大概是20ms。要以正常速度播放視頻,應該是8ms。有什麼解決方案來縮短這個時間嗎? – vmarquet