利用opencv逐帧提取视频

代码一

import cv2
import os
#要提取视频的文件名,隐藏后缀
sourceFileName='../wurenji_putao/DJI_0009'
#在这里把后缀接上
video_path = os.path.join("", "", sourceFileName+'.MP4')
times=0
#提取视频的频率,每25帧提取一个
frameFrequency=25
#输出图片到当前目录vedio文件夹下
outPutDirName=''+sourceFileName+'/'
if not os.path.exists(outPutDirName):
    #如果文件目录不存在则创建目录
    os.makedirs(outPutDirName) 
camera = cv2.VideoCapture(video_path)
while True:
    times+=1
    res, image = camera.read()
    if not res:
        print('not res , not image')
        break
    if times%frameFrequency==0:
        cv2.imwrite(outPutDirName + str(times)+'.jpg', image)
        print(outPutDirName + str(times)+'.jpg')
print('图片提取结束')
camera.release()

代码二:

import cv2
vidcap = cv2.VideoCapture('../wureji_putao/DJI_0009.MP4')
success,img = vidcap.read()
count = 0 
success = True
while success :
    success,image = vidcap.read()
    cv2.imwrite("frame%d.jpg" % count, image)
    if cv2.waitKey(10) == 27:
        break
    count += 1

代码三:

import cv2
vc = cv2.VideoCapture('../wurenji_putao/DJI_0010.MP4')  # 读入视频文件,命名cv
n = 1  # 计数
  
if vc.isOpened():  # 判断是否正常打开
    rval, frame = vc.read()
else:
    rval = False
  
timeF = 10  # 视频帧计数间隔频率
  
i = 259
while rval:  # 循环读取视频帧
    rval, frame = vc.read()
    if (n % timeF == 0):  # 每隔timeF帧进行存储操作
        i += 1
        print(i)
        cv2.imwrite('../wurenji_putao/DJI_0010/{}.jpg'.format(i), frame)  # 存储为图像
    n = n + 1
    cv2.waitKey(1)
vc.release()

本博客转载至CSDN,原文链接:https://blog.csdn.net/weixin_45392081/article/details/107773999

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容