opencv学习27——从视频文件获取图像
程序员文章站
2022-03-03 09:57:41
...
import cv2
cap = cv2.VideoCapture("video01.avi") # 获取视频对象
isOpened = cap.isOpened # 判断是否打开
# 视频信息获取
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
imageNum = 0
while(isOpened):
if imageNum/fps == 2: # 获取两秒内视频图片
break
else:
imageNum = imageNum + 1
(frameState, frame) = cap.read() # 记录每帧及获取状态
fileName = 'image' + str(imageNum) + '.jpg' # 存储路径
if frameState == True:
cv2.imwrite(fileName,frame,[cv2.IMWRITE_JPEG_QUALITY,100])
print(fileName+"successfully write in") # 输出存储状态
print('finish!')