在OpenCV中,类videoccapture处理摄像头读取视频和抓取帧。通过使用videoccapture中的get(PROPERTY_NAME)
方法,你可以找到很多关于你正在播放的视频文件的信息。
如何在OpenCV中找到一个摄像头/网络摄像头的帧率?
在OpenCV中,查找连接的摄像头/网络摄像头的帧率不是直接的。文档说get(CAP_PROP_FPS)
或get(CV_CAP_PROP_FPS)
给出每秒的帧数。这对视频文件是正确的,但对网络摄像头不是。对于网络摄像头和许多其他连接摄像头,你必须手动计算每秒的帧数。您可以从视频中读取一定数量的帧,并查看计算每秒帧需要花费多少时间。
Python
#!/usr/bin/env python
import cv2
import time
if __name__ == '__main__' :
# Start default camera
video = cv2.VideoCapture(0);
# Find OpenCV version
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
# With webcam get(CV_CAP_PROP_FPS) does not work.
# Let's see for ourselves.
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 range(0, num_frames) :
ret, frame = video.read()
# End time
end = time.time()
# Time elapsed
seconds = end - start
print ("Time taken : {0} seconds".format(seconds))
# Calculate frames per second
fps = num_frames / seconds
print("Estimated frames per second : {0}".format(fps))
# Release video
video.release()
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(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;
}
如何在OpenCV中找到视频的帧率?
如果你正在读取一个视频文件,你可以简单地使用get方法来获得每秒的帧数。下面的例子展示了使用方法
Python
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()
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(CAP_PROP_FPS) : " << fps << endl;
video.release();
return 0;
}