前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OpenCV - 计算相机和视频的帧速率FPS

OpenCV - 计算相机和视频的帧速率FPS

作者头像
AIHGF
发布2019-06-14 20:36:16
19.3K0
发布2019-06-14 20:36:16
举报
文章被收录于专栏:AIUAIAIUAI

原文:OpenCV - 计算相机和视频的帧速率FPS[译] - AIUAI

原文:How to find frame rate or frames per second (fps) in OpenCV ( Python / C++ ) ? - 2015.11.12

OpenCV 库中的 VideoCapture 类主要处理视频读取以及从连接的相机中获取图像帧.

基于VideoCapture 中的 get(PROPERTY_NAME) 方法可以获取到视频文件的很多信息. 其中,关于视频的最常用的属性是帧速率(frame rate),也叫每秒帧数(frames per second).

1. 计算相机的帧速率FPS

OpenCV 并不能很直接的得到所连接的相机(camera/webcam) 的帧速率.

在 OpenCV 的文档中,所述的是,get(CAP_PROP_FPS)get(CV_CAP_PROP_FPS) 方法给出了每秒帧数. 这对于视频文件而言是正确的,但是并不适用于 webcams. 对于webcams 以及许多其它 cameras,不得不手工计算每秒的帧数. 可以从视频中读取一定量的视频帧,然后根据所耗时间,计算得到每秒帧数.

1.1. Python 实现

代码语言:javascript
复制
#!/usr/bin/env python
#! --*-- coding:utf-8 --*--
import cv2
import time
 
if __name__ == '__main__' :
    # 启动默认相机
    video = cv2.VideoCapture(0);
     
    # 获取 OpenCV version
    (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
    
    # 对于 webcam 不能采用 get(CV_CAP_PROP_FPS) 方法 
    # 而是:
    if int(major_ver)  < 3 :
        fps = video.get(cv2.cv.CV_CAP_PROP_FPS)
        print("Frames per second using video.get(cv2.cv.CV_CAP_PROP_FPS): {0}".format(fps))
    else :
        fps = video.get(cv2.CAP_PROP_FPS)
        print("Frames per second using video.get(cv2.CAP_PROP_FPS) : {0}".format(fps))
     
    # Number of frames to capture
    num_frames = 120;
    print("Capturing {0} frames".format(num_frames))
 
    # Start time
    start = time.time()
    # Grab a few frames
    for i in xrange(0, num_frames):
        ret, frame = video.read()
    # End time
    end = time.time()
 
    # Time elapsed
    seconds = end - start
    print("Time taken : {0} seconds".format(seconds))
 
    # 计算FPS,alculate frames per second
    fps  = num_frames / seconds;
    print("Estimated frames per second : {0}".format(fps))
 
    # 释放 video
    video.release()

1.2. C++ 实现

代码语言:javascript
复制
#include "opencv2/opencv.hpp"
#include <time.h>
 
using namespace cv;
using namespace std;
 
int main(int argc, char** argv)
{
    // Start default camera
    VideoCapture video(0);
     
    // With webcam get(CV_CAP_PROP_FPS) does not work.
    // Let's see for ourselves.
     
    double fps = video.get(CV_CAP_PROP_FPS);
    // If you do not care about backward compatibility
    // You can use the following instead for OpenCV 3
    // double fps = video.get(CAP_PROP_FPS);
    cout << "Frames per second using video.get(CV_CAP_PROP_FPS) : " << fps << endl;
     
    // Number of frames to capture
    int num_frames = 120;
    
    // Start and end times
    time_t start, end;
    
    // Variable for storing video frames
    Mat frame;
 
    cout << "Capturing " << num_frames << " frames" << endl ;
 
    // Start time
    time(&start);
     
    // Grab a few frames
    for(int i = 0; i < num_frames; i++)
    {
        video >> frame;
    }
     
    // End Time
    time(&end);
     
    // Time elapsed
    double seconds = difftime (end, start);
    cout << "Time taken : " << seconds << " seconds" << endl;
     
    // Calculate frames per second
    fps  = num_frames / seconds;
    cout << "Estimated frames per second : " << fps << endl;
     
    // Release video
    video.release();
    return 0;
}

2. 计算视频的帧速率FPS

可以直接采用 OpenCV 的 get 方法计算视频文件的帧速率.

2.1. Python 实现

代码语言:javascript
复制
#!/usr/bin/env python
#! --*-- coding:utf-8 --*--
import cv2

if __name__ == '__main__' :
    video = cv2.VideoCapture("video.mp4");
     
    # Find OpenCV version
    (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
     
    if int(major_ver)  < 3 :
        fps = video.get(cv2.cv.CV_CAP_PROP_FPS)
        print("Frames per second using video.get(cv2.cv.CV_CAP_PROP_FPS): {0}".format(fps))
    else :
        fps = video.get(cv2.CAP_PROP_FPS)
        print("Frames per second using video.get(cv2.CAP_PROP_FPS) : {0}".format(fps))
     
   video.release(); 

2.2. C++ 实现

代码语言:javascript
复制
#include "opencv2/opencv.hpp"
 
using namespace cv; 
using namespace std;
 
int main(int argc, char** argv)
{
    // Open video file
    VideoCapture video("video.mp4");
 
    double fps = video.get(CV_CAP_PROP_FPS);
 
    // For OpenCV 3, you can also use the following
    // double fps = video.get(CAP_PROP_FPS);
 
    cout << "Frames per second using video.get(CV_CAP_PROP_FPS) : " << fps << endl;
     
    video.release(); 
    return 0;
} 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年05月30日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 计算相机的帧速率FPS
    • 1.1. Python 实现
      • 1.2. C++ 实现
      • 2. 计算视频的帧速率FPS
        • 2.1. Python 实现
          • 2.2. C++ 实现
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档