opencv视频抽帧保存图片video2pic
文章目录
opencv视频按帧率抽帧
python
import cv2
import os
video_path='C:/Users/Desktop/video/1.mp4'
name=os.path.basename(video_path).split(".")[0]
output_dir_folder = 'C:/Users/Desktop/pic/'
output_folder=os.path.join(output_dir_folder,name)
# 指定帧率
fps = 1 #1s取一帧
# 创建文件夹
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 打开视频
cap = cv2.VideoCapture(video_path)
# 计算每隔多少帧保存一帧
step = max(1, int(cap.get(cv2.CAP_PROP_FPS) / fps))
# 读取视频帧
i = 0
count=1
while True:
ret, frame = cap.read()
if not ret:
break
# 按指定的帧率保存帧
if i % step == 0:
filename = os.path.join(output_folder, name+f"_{count:03d}.jpg")
cv2.imwrite(filename, frame)
count+=1
print(f"Saved frame: {i}")
i += 1
# 释放视频对象
cap.release()
opencv视频按帧数抽帧
python
import cv2
import time
# 定义视频捕捉对象
cap = cv2.VideoCapture(1) # 参数0代表计算机的默认摄像头
count=0
i=0
while(cap.isOpened()):
ret, frame = cap.read() #
if ret:
#10帧保存一帧
if i%10==0:
cv2.imwrite("C:/Users/Desktop/0527_%s.jpg"%(count),frame)
count+=1
i+=1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# 释放资源
cap.release()