从视频帧中采样并将它们保存为图像 - python openCV

Sampling from video frames and save them as an image - python openCV

我有一个视频文件,我知道如何保存所有帧,但我的问题是如何只将每 20 帧中的一帧保存为图像?谢谢

这基于本教程:https://theailearner.com/2018/10/15/extracting-and-saving-video-frames-using-opencv-python/

import cv2

# Opens the Video file
cap= cv2.VideoCapture('C:/New/Videos/Play.mp4')
i=0
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == False:
        break
    if i % 20 == 0: # this is the line I added to make it only save one frame every 20
        cv2.imwrite('kang'+str(i)+'.jpg',frame)
    i+=1

cap.release()
cv2.destroyAllWindows()

或尝试使用除法:

import cv2
capture = cv2.VideoCapture('C:/New/Videos/Play.mp4')
c=0
while capture.isOpened():
    r, f = capture.read()
    if r == False:
        break
    if c / 20 == c // 20:
        cv2.imwrite('kang'+str(c)+'.jpg',frame)
    c+=1

capture.release()
cv2.destroyAllWindows()