相关文章推荐
烦恼的哑铃  ·  中国民航局批复《南通新机场》选址二甲_场址·  7 月前    · 
近视的烈马  ·  云音乐ACG游戏榜 - 排行榜 - 网易云音乐·  10 月前    · 
坏坏的斑马  ·  【论文答辩】机械与动力工程学院2021年6月 ...·  12 月前    · 
仗义的柠檬  ·  exchange server 2019 ...·  1 年前    · 
重感情的西装  ·  Glide - 加载一个GIF作为占位符·  1 年前    · 
Code  ›  OpenCV - 计算相机和视频的帧速率FPS开发者社区
opencv 帧率
https://cloud.tencent.com/developer/article/1446281
粗眉毛的松树
1 年前
作者头像
AIHGF
0 篇文章

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

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > AIUAI > OpenCV - 计算相机和视频的帧速率FPS

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

作者头像
AIHGF
发布 于 2019-06-14 20:36:16
17.3K 0
发布 于 2019-06-14 20:36:16
举报

原文: 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 实现

#!/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++ 实现

#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 实现

#!/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++ 实现

#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);
 
推荐文章
烦恼的哑铃  ·  中国民航局批复《南通新机场》选址二甲_场址
7 月前
近视的烈马  ·  云音乐ACG游戏榜 - 排行榜 - 网易云音乐
10 月前
坏坏的斑马  ·  【论文答辩】机械与动力工程学院2021年6月毕业硕士研究生查重、盲审及答辩安排-通知公告-在校生-研究生教学网
12 月前
仗义的柠檬  ·  exchange server 2019 ECP打开邮件送达报告报错400 - Microsoft Q&A
1 年前
重感情的西装  ·  Glide - 加载一个GIF作为占位符
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号