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()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章