基于OpenCV和python的视频逐帧切片

# 基于OpenCV和python的视频逐帧切片
import cv2
import os
videoPath = os.path.join(os.getcwd(),'video.mp4')    #获取视频
imageDirPath = os.path.join(os.getcwd(),'image_database')    #切片存放路径

print(videoPath)
vc = cv2.VideoCapture(videoPath)

if vc.isOpened():
    index = 0
    timeF = 10  #采样间隔,每隔timeF帧提取一张图片
    c=1
    success = True
    while(success):
        success, frame = vc.read()
        if(c%timeF == 0):
            # frame = cv2.resize(frame,(640,360))
            imagePath = os.path.join(imageDirPath,str(index) + '.jpg')
            cv2.imwrite(imagePath,frame)
            index = index+1
            print(index)
        c = c + 1
        cv2.waitKey(1)
    vc.release()
    print("open ok")
else:
    success = False
    print("open error")
    exit(0)

 

你可能感兴趣的:(CV,OpenCV,python)