我想用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