Getting the current frame number in OpenCV

How can I get the current frame number of a video using OpenCV? Does OpenCV have a built-in function to get the current frame, or do I need to do this manually?

+7
source share
3 answers

You can use the get method of your capture object, as shown below:

capture.get(CV_CAP_PROP_POS_FRAMES); // retrieves the current frame number

and:

capture.get(CV_CAP_PROP_FRAME_COUNT); // retuns the number of total frames 

Btw, these methods return a double value.

You can also use the cvGetCaptureProperty method.

cvGetCaptureProperty (CvCapture * capture, int property_id);

Property_id parameters below with definitions:

CV_CAP_PROP_POS_MSEC 0

CV_CAP_PROP_POS_FRAME 1

CV_CAP_PROP_POS_AVI_RATIO 2

CV_CAP_PROP_FRAME_WIDTH 3

CV_CAP_PROP_FRAME_HEIGHT 4

CV_CAP_PROP_FPS 5

CV_CAP_PROP_FOURCC 6

CV_CAP_PROP_FRAME_COUNT 7

POS_MSEC - , . POS_FRAME - . POS_AVI_RATIO - , 0 1 ( , -, ). FRAME_WIDTH FRAME_HEIGHT - ( ). FPS , ; , . FOURCC - , , . FRAME_COUNT , . ( OpenCV)

+19

python OpenCV :

import cv2
cam = cv2.VideoCapture(<filename>);
print cam.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
+3

In openCV version 3.4, the correct flag is:

cap.get(cv2.CAP_PROP_POS_FRAMES)
0
source

All Articles