How to get previous frame of a video in opencv python

只愿长相守 提交于 2019-12-06 04:24:34

问题


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()

回答1:


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()



回答2:


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()



回答3:


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()


来源:https://stackoverflow.com/questions/48025689/how-to-get-previous-frame-of-a-video-in-opencv-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!