opencv计算视频和摄像头的帧数及帧率(FPS)

1、计算总帧数

python代码

import cv2

video_cap = cv2.VideoCapture('video1.mp4')

frame_count = 0
all_frames = []
while (True):
    ret, frame = video_cap.read()
    if ret is False:
        break
    all_frames.append(frame)
    frame_count = frame_count + 1
    print(frame_count)
video_cap.release()
# The value below are both the number of frames

print('====>',len(all_frames))

也可以调用opencv内部计数器

import cv2

video_cap = cv2.VideoCapture('video1.mp4')

frame_count = 0
all_frames = []
NUM = video_cap.get(cv2.CAP_PROP_FRAME_COUNT)
print("test:",NUM)

c++ 代码

#include "stdafx.h"
#include   
#include   
 
 
int main(int argc, char* argv[])
{
	IplImage *nFrames = NULL;
	CvCapture* pCapture = NULL;
 
 
	if( !(pCapture = cvCaptureFromAVI("video1.mp4")))
	{
		fprintf(stderr, "Can not open camera.\n");
		return -1;
	}
	int numFrames = (int) cvGetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_COUNT);  
		printf("vedio's nums = %d",  numFrames);
 	
 
	getchar();
	return 0;
}

2、计算帧率(FPS)

python代码

视频帧率

# 视频帧率
import cv2

if __name__ == '__main__':

    video = cv2.VideoCapture("video1.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()

摄像头帧率

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()

C++代码

视频帧率

#include "stdafx.h"
#include   
#include   
 
 
int main(int argc, char* argv[])
{
	IplImage *nFrames = NULL;
	CvCapture* pCapture = NULL;
 
 
	if( !(pCapture = cvCaptureFromAVI("video1.mp4")))
	{
		fprintf(stderr, "Can not open camera.\n");
		return -1;
	}
	int numFrames = (int) cvGetCaptureProperty(pCapture, CV_CAP_PROP_FPS);  
		printf("vedio's nums = %d",  numFrames);
 	
 
	getchar();
	return 0;
}

摄像头帧率

#include "opencv2/opencv.hpp"
#include 
 
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;
}

参考文献:https://cloud.tencent.com/developer/article/1446281

你可能感兴趣的:(opencv成长记)