python中视频文件每隔3帧取1帧并保存

视频分帧在图像处理中是一个非常常见的问题,在这里博主希望能够以简洁的代码给大家提供帮助。

视频连续分帧

import cv2
cap = cv2.VideoCapture('person.mp4')
count = 1
while True:
    success,image = cap.read()
    if success:
        cv2.imwrite("fenzhen/frame%d.jpg" %count,image)
        if cv2.waitKey(10) == 27:
            break                    
        count += 1
    else:
        break
cap.release()
cv2.destroyAllWindows()

视频每隔3帧取1帧

import cv2
cap = cv2.VideoCapture('person.mp4')
count = 1
while True:
    success,image = cap.read()
    if success:
        if count%3==1:
            cv2.imwrite("fenzhen1/frame%d.jpg" %count,image)
            if cv2.waitKey(10) == 27:
                break                    
        count += 1
    else:
        break
cap.release()
cv2.destroyAllWindows()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章