1. 使用capture.get(CV_CAP_PROP_FPS)方法获取帧率为0!
  2. waitKey()函数有几个作用?改变它的参数大小会影响计算得到的FPS大小

分析:

  1. OpenCV中的fps只能在读取视频的时候获得。如果是摄像头,可根据fps的定义自己计算,网上也有人遇到过读取摄像头帧率为0的情况
  2. waitkey有两个作用,且在imshow之后如果没有waitKey语句则不能正常显示图像。在下面给的实验代码中计算fps意义不大,只可大致测试出摄像头图像处理算法的时间消耗,使用MFC或者QT这个时候的fps才有意义,这里的fps是指游戏中的图像刷新率 是衡量游戏性能的一个指标。
  1. It waits for x milliseconds for a key press. If a key was pressed during that time, it returns the key’s ASCII code. Otherwise, it returns -1.
  1. It handles any windowing events, such as creating windows with cv::namedWindow(), or showing images with cv::imshow().

waitkey函数的定义如下:

int cvWaitKey( int delay=0 )

返回值为int型,函数的参数为int型,当delay小于等于0的时候,如果没有键盘触发,则一直等待,此时的返回值为-1,否则返回值为键盘按下的码字;当delay大于0时,如果没有键盘的的触发,则等待delay的时间,此时的返回值是-1,否则返回值为键盘按下的码字。

附上代码:

#include <stdlib.h>
#include 
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include “opencv2/imgproc/imgproc.hpp”
int main(int argc, const char** argv)
{
cv::Mat frame;
// 可从摄像头输入视频流或直接播放视频文件
cv::VideoCapture capture(0);
//cv::VideoCapture capture(“vedio1.avi”);
double fps;
char string[10]; // 帧率字符串
cv::namedWindow(“Camera FPS”);
double t = 0;
while (1)
{
t = (double)cv::getTickCount();
if (cv::waitKey(1) == 1) { break; }
if (capture.isOpened())
{
capture >> frame;
// getTickcount函数:返回从操作系统启动到当前所经过的毫秒数
// getTickFrequency函数:返回每秒的计时周期数
// t为该处代码执行所耗的时间,单位为秒,fps为其倒数
t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
fps = 1.0 / t;
sprintf_s(string, “%.2f”, fps); // 帧率保留两位小数
std::string fpsString(“FPS:”);
fpsString += string; // 在"FPS:"后加入帧率数值字符串
printf(“fps: %.2f width:%d height:%d fps:%.2f\n”, fps, frame.cols, frame.rows, capture.get(CV_CAP_PROP_FPS));