概述
通过帧数间隔截取视频帧
import cv2
cap = cv2.VideoCapture("./query_video/test_video_0.mp4")
c = 1
frameRate = 100 # 帧数截取间隔(每隔100帧截取一帧)
while(True):
ret, frame = cap.read()
if ret:
if(c % frameRate == 0):
print("开始截取视频第:" + str(c) + " 帧")
# 这里就可以做一些操作了:显示截取的帧图片、保存截取帧到本地
cv2.imwrite("./capture_image/" + str(c) + '.jpg', frame) # 这里是将截取的图像保存在本地
c += 1
cv2.waitKey(0)
else:
print("所有帧都已经保存完成")
break
cap.release()
通过时间间隔截取视频帧
import cv2
cap = cv2.VideoCapture("./query_video/test_video_0.mp4")
c = 1
timeRate = 10 # 截取视频帧的时间间隔(这里是每隔10秒截取一帧)
while(True):
ret, frame = cap.read()
FPS = cap.get(5)
if ret:
frameRate = int(FPS) * timeRate # 因为cap.get(5)获取的帧数不是整数,所以需要取整一下(向下取整用int,四舍五入用round,向上取整需要用math模块的ceil()方法)
if(c % frameRate == 0):
print("开始截取视频第:" + str(c) + " 帧")
# 这里就可以做一些操作了:显示截取的帧图片、保存截取帧到本地
cv2.imwrite("./capture_image/" + str(c) + '.jpg', frame) # 这里是将截取的图像保存在本地
c += 1
cv2.waitKey(0)
else:
print("所有帧都已经保存完成")
break
cap.release()
cap = cv2.VideoCapture 的属性
get方法参数按顺序对应下表(从0开始编号
propId –
Property identifier. It can be one of the following:
- CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds or video capture timestamp.
- CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
- CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
- CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
- CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
- CV_CAP_PROP_FPS Frame rate.
- CV_CAP_PROP_FOURCC 4-character code of codec.
- CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
- CV_CAP_PROP_FORMAT Format of the Mat objects returned by
retrieve()
. - CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
- CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
- CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
- CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
- CV_CAP_PROP_HUE Hue of the image (only for cameras).
- CV_CAP_PROP_GAIN Gain of the image (only for cameras).
- CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
- CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
- CV_CAP_PROP_WHITE_BALANCE_U The U value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently)
- CV_CAP_PROP_WHITE_BALANCE_V The V value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently)
- CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
- CV_CAP_PROP_ISO_SPEED The ISO speed of the camera (note: only supported by DC1394 v 2.x backend currently)
- CV_CAP_PROP_BUFFERSIZE Amount of frames stored in internal buffer memory (note: only supported by DC1394 v 2.x backend currently)
最后
以上就是糟糕悟空为你收集整理的视频处理-按帧或者按秒截取图像的全部内容,希望文章能够帮你解决视频处理-按帧或者按秒截取图像所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复