一、前言
由于想知道自己电脑i7 使用opencv读取一帧速度,测试视频大小为480*640,发现当使用VideoCapture::read()读取一帧,并用imshow显示图像,总共需耗时16ms左右,相当于每秒66帧左右,分别对两个函数测试时,发现单读取一帧只需0.7~1.2ms,而使用imshow显示图像则函数较为严重,显示一帧需15ms左右。当然不同大小和格式的视频,读取时间则有所不同。
二、代码
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
#include <iostream>
#include "MyTimer.h"
int main()
{
//VideoCapture cap("umn_dataset/Crowd-Activity-All.avi");
VideoCapture cap("2.avi");
if (!cap.isOpened()){
std::cerr << "无法获取视频\n";
}
Mat srcImg ,grayImg;
MyTimer timer;
namedWindow("Demo", 1);
timer.Start();
while (cap.read(srcImg)){
timer.End();
std::cout << "获取一帧耗时:" << timer.costTime << "\n";
timer.Reset();
timer.Start();
imshow("Demo", srcImg);
waitKey(1);
timer.End();
std::cout << "imshow耗时:" << timer.costTime << "\n";
timer.Reset();
timer.Start();
}
return 0;
}
三、结果