Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving the current frame number in OpenCV

How can I retrieve the current frame number of a video using OpenCV? Does OpenCV have any built-in function for getting the current frame or I have to do it manually?

like image 362
Ruhi Akaboy Avatar asked May 06 '12 23:05

Ruhi Akaboy


People also ask

How do I find my Opencv frame number?

You can also use cvGetCaptureProperty method (if you use old C interface). POS_MSEC is the current position in a video file, measured in milliseconds. POS_FRAME is the position of current frame in video (like 55th frame of video).

How do I count the number of frames in a video using Opencv?

The slow way to count frames with OpenCV and Python VideoCapture . We then initialize the total number of frames read from the video file, loop over the frames until we have reached the end of the video, and increment the total counter along the way. The total is then returned to the calling function.

What is frame count in python?

The system variable frameCount contains the number of frames displayed since the program started. Inside setup() the value is 0 and and after the first iteration of draw it is 1, etc.


3 Answers

You can use the "get" method of your capture object like below :

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

and also :

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

Btw, these methods return a double value.

You can also use cvGetCaptureProperty method (if you use old C interface).

cvGetCaptureProperty(CvCapture* capture,int property_id);

property_id options are 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 is the current position in a video file, measured in milliseconds.
  • POS_FRAME is the position of current frame in video (like 55th frame of video).
  • POS_AVI_RATIO is the current position given as a number between 0 and 1 (this is actually quite useful when you want to position a trackbar to allow folks to navigate around your video).
  • FRAME_WIDTH and FRAME_HEIGHT are the dimensions of the individual frames of the video to be read (or to be captured at the camera’s current settings).
  • FPS is specific to video files and indicates the number of frames per second at which the video was captured. You will need to know this if you want to play back your video and have it come out at the right speed.
  • FOURCC is the four-character code for the compression codec to be used for the video you are currently reading.
  • FRAME_COUNT should be the total number of frames in video, but this figure is not entirely reliable.

(from Learning OpenCV book )

like image 110
Horizon1710 Avatar answered Sep 20 '22 11:09

Horizon1710


In openCV version 3.4, the correct flag is:

cap.get(cv2.CAP_PROP_POS_FRAMES)
like image 25
DankMasterDan Avatar answered Sep 19 '22 11:09

DankMasterDan


The way of doing it in OpenCV python is like this:

import cv2
cam = cv2.VideoCapture(<filename>);
print cam.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
like image 21
eculeus Avatar answered Sep 17 '22 11:09

eculeus