简体   繁体   中英

How to get previous frame of a video in opencv python

I want to detect obstacles from a video based on their increasing size.To do that first I applied SIFT on gray image to get feature points of current frame. Next to compare the feature points of current frame with the previous frame I want to apply Brute-Force algorithm. For that I want to get feature points in previous frame. How can I access previous frame in opencv python ? and how to avoid accessing previous frame when the current frame is the first frame of the video?

below is the code written in python to get feature points of current frame.

import cv2
import numpy as np


cap = cv2.VideoCapture('video3.mov')

  while(cap.isOpened()):

  ret, frame = cap.read()

  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

  #detect key feature points
  sift = cv2.xfeatures2d.SIFT_create()
  kp, des = sift.detectAndCompute(gray, None)

 #draw key points detected
 img=cv2.drawKeypoints(gray,kp,gray,flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

 cv2.imshow("grayframe",img)

 if cv2.waitKey(100) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

You could also get/set the zero-based frame index (CAP_PROP_POS_FRAMES), which might be useful if you wanted flexibility to step back through more than one frame, compare to a specific frame, etc. Note though that this would reset the position for the next read(), so if you really only ever want the previous frame, storing it in a variable per the other answers is probably better.

next_frame = cap.get(cv2.CAP_PROP_POS_FRAMES)
current_frame = next_frame - 1
previous_frame = current_frame - 1

if previous_frame >= 0:
  cap.set(cv2.CAP_PROP_POS_FRAMES, previous_frame)
  ret, frame = cap.read()

There is no specific function in OpenCV to access the previous frame. Your problem can be solved by calling cap.read() once before entering the while loop. Use a variable prev_frame to store the previous frame just before reading the new frame. Finally, as a good practice, you should verify that the frame was properly read, before doing computations on it. Your code could look something like:

import cv2
import numpy as np

cap = cv2.VideoCapture('video3.mov')
ret, frame = cap.read()

while(cap.isOpened()):
    prev_frame=frame[:]
    ret, frame = cap.read()
    if ret:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        #detect key feature points
        sift = cv2.xfeatures2d.SIFT_create()
        kp, des = sift.detectAndCompute(gray, None)

        #some magic with prev_frame

        #draw key points detected
        img=cv2.drawKeypoints(gray,kp,gray, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

        cv2.imshow("grayframe",img)
    else:
        print('Could not read frame')

    if cv2.waitKey(100) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Simply save the current frame to be the previous frame in the next iteration. Use a list, if you need more than 1.

import cv2
import numpy as np


cap = cv2.VideoCapture('video3.mov')
previousFrame=None

while(cap.isOpened()):

  ret, frame = cap.read()

  if previousFrame is not None:
      #use previous frame here
      pass

  #save current frame
  previousFrame=frame


  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

  #detect key feature points
  sift = cv2.xfeatures2d.SIFT_create()
  kp, des = sift.detectAndCompute(gray, None)

  #draw key points detected
  img=cv2.drawKeypoints(gray,kp,gray,flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

  cv2.imshow("grayframe",img)


  if cv2.waitKey(100) & 0xFF == ord('q'):
       break

cap.release()
cv2.destroyAllWindows()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM