C++ 在 OpenCV 中检索当前帧数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10475198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving the current frame number in OpenCV
提问by Ruhi Akaboy
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?
如何使用 OpenCV 检索视频的当前帧号?OpenCV 是否有任何内置函数来获取当前帧,还是我必须手动完成?
回答by Horizon1710
You can use the "get" method of your capture object like below :
您可以使用捕获对象的“get”方法,如下所示:
capture.get(CV_CAP_PROP_POS_FRAMES); // retrieves the current frame number
and also :
并且 :
capture.get(CV_CAP_PROP_FRAME_COUNT); // retuns the number of total frames
Btw, these methods return a double value.
顺便说一句,这些方法返回一个双精度值。
You can also use cvGetCaptureProperty method.
您还可以使用 cvGetCaptureProperty 方法。
cvGetCaptureProperty(CvCapture* capture,int property_id);
cvGetCaptureProperty(CvCapture* capture,int property_id);
property_id options are below with definitions:
property_id 选项与定义如下:
CV_CAP_PROP_POS_MSEC 0
CV_CAP_PROP_POS_MSEC 0
CV_CAP_PROP_POS_FRAME 1
CV_CAP_PROP_POS_FRAME 1
CV_CAP_PROP_POS_AVI_RATIO 2
CV_CAP_PROP_POS_AVI_RATIO 2
CV_CAP_PROP_FRAME_WIDTH 3
CV_CAP_PROP_FRAME_WIDTH 3
CV_CAP_PROP_FRAME_HEIGHT 4
CV_CAP_PROP_FRAME_HEIGHT 4
CV_CAP_PROP_FPS 5
CV_CAP_PROP_FPS 5
CV_CAP_PROP_FOURCC 6
CV_CAP_PROP_FOURCC 6
CV_CAP_PROP_FRAME_COUNT 7
CV_CAP_PROP_FRAME_COUNT 7
POS_MSEC is the current position in a video file, measured in milliseconds. POS_FRAME is the current position in frame number. POS_AVI_RATIO is the 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 specifi c 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 the video, but this fi gure is not entirely reliable. (from Learning OpenCV book )
POS_MSEC 是视频文件中的当前位置,以毫秒为单位。POS_FRAME 是帧编号中的当前位置。POS_AVI_RATIO 是指定为 0 到 1 之间的数字的位置(当您想要定位轨迹栏以允许人们浏览您的视频时,这实际上非常有用)。FRAME_WIDTH 和 FRAME_HEIGHT 是要读取(或要在相机的当前设置下捕获)的视频各个帧的尺寸。FPS 专用于视频文件,表示每秒捕获视频的帧数;如果您想播放视频并以正确的速度播放,您需要知道这一点。FOURCC 是用于您当前正在阅读的视频的压缩编解码器的四字符代码。FRAME_COUNT 应该是视频中的总帧数,但这个数字并不完全可靠。(来自学习 OpenCV 书)
回答by eculeus
The way of doing it in OpenCV python is like this:
在 OpenCV python 中这样做的方式是这样的:
import cv2
cam = cv2.VideoCapture(<filename>);
print cam.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
回答by DankMasterDan
In openCV version 3.4, the correct flag is:
在 openCV 3.4 版中,正确的标志是:
cap.get(cv2.CAP_PROP_POS_FRAMES)