视频分帧
下面代码的速度大约为:400帧 17s,20帧2.8s。
代码:
import cv2
from PIL import Image
import numpy as np
import time
time_start = time.time()
cap = cv2.VideoCapture('F:/photo_stitching/video_duct/7.84_49.mp4') # 获取视频对象
isOpened = cap.isOpened # 判断是否打开
# 视频信息获取
fps = cap.get(cv2.CAP_PROP_FPS)
imageNum = 0
sum=0
# timef=15 #隔15帧保存一张图片
timef=20 #隔15帧保存一张图片
while (isOpened):
sum+=1
(frameState, frame) = cap.read() # 记录每帧及获取状态
if frameState == True and (sum % timef==0):
# 格式转变,BGRtoRGB
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 转变成Image
frame = Image.fromarray(np.uint8(frame))
frame = np.array(frame)
# RGBtoBGR满足opencv显示格式
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
imageNum = imageNum + 1
fileName = 'F:/photo_stitching/video_duct/7.84_49_test/' + str(imageNum) + '.jpg' # 存储路径
cv2.imwrite(fileName, frame, [cv2.IMWRITE_JPEG_QUALITY, 100])
# print(fileName + " successfully write in") # 输出存储状态
elif frameState == False:
break
print('finish!')
time_end = time.time()
d_time = time_end - time_start
print("图像分帧的耗时为 %.8s S"%d_time)
cap.release()
|