OpenCV/Python: read specific frame using VideoCapture

后端 未结 5 977
温柔的废话
温柔的废话 2021-01-30 16:34

Is there a way to get a specific frame using VideoCapture() method?

My current code is:

import numpy as np
import cv2

cap = cv2.VideoCaptur         


        
5条回答
  •  孤城傲影
    2021-01-30 17:10

    Yes, it's very straight forward:

    import cv2
    cap = cv2.VideoCapture(videopath)
    cap.set(cv2.CV_CAP_PROP_POS_FRAMES, frame_number-1)
    res, frame = cap.read()
    

    'frame_number' is an integer in range 0...amount_of_frames. Notice: you should set 'frame_number-1' to force reading frame 'frame_number'. It's not documented well but test shows that behavior of VideoCapture module.

    'res' is boolean result of operation, one may use it to check if frame was successfully read. One may obtain amount of frames by:

    amount_of_frames = cap.get(cv2.CV_CAP_PROP_FRAME_COUNT)
    

提交回复
热议问题