OpenCV - 計算相機和視頻的幀速率FPS

原文: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);
 
    cout << "Frames per second using video.get(CV_CAP_PROP_FPS) : " << fps << endl;
     
    video.release(); 
    return 0;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章