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()
原创文章 9 获赞 9 访问量 1308

猜你喜欢

转载自blog.csdn.net/weixin_42709563/article/details/103941042