本文主要是介绍opencv视频抽帧保存图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
opencv视频抽帧保存图片video2pic
文章目录
- opencv视频抽帧保存图片video2pic
- opencv视频按帧率抽帧
- opencv视频按帧数抽帧
opencv视频按帧率抽帧
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+=1print(f"Saved frame: {i}") i += 1
# 释放视频对象
cap.release()
opencv视频按帧数抽帧
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+=1i+=1if cv2.waitKey(1) & 0xFF == ord('q'):breakelse:break# 释放资源
cap.release()
这篇关于opencv视频抽帧保存图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!