OpenCV/Python: read specific frame using VideoCapture

后端 未结 5 985
温柔的废话
温柔的废话 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

    If you want an exact frame, you could just set the VideoCapture session to that frame. It's much more intuitive to automatically call on that frame. The "correct" solution requires you to input known data: like fps, length, and whatnot. All you need to know with the code below is the frame you want to call.

    import numpy as np
    import cv2
    cap = cv2.VideoCapture(video_name) #video_name is the video being called
    cap.set(1,frame_no); # Where frame_no is the frame you want
    ret, frame = cap.read() # Read the frame
    cv2.imshow('window_name', frame) # show frame on window
    

    If you want to hold the window, until you press exit:

    while True:
        ch = 0xFF & cv2.waitKey(1) # Wait for a second
        if ch == 27:
            break
    

提交回复
热议问题