calculate optical flow between several consecutive frames

asked 2019-01-21 15:03:35 -0600

sarmad gravatar image

updated 2019-01-21 15:17:28 -0600

Hi

How can I calculate optical flow between several consecutive frames (e.g., every 10 frames), ? and then optical flow computed from each image is transformed into a “flow image" that contains the horizontal and vertical components: x flow values, y flow values, as well as the flow magnitude. I'm using below code to compute the optical flow for two images , but I don't know how to do:

  1. If I have a folder conatins for example 500 video frames as images , how to calculate optical flow between several consecutive frames (e.g., every 10 frames) ?

  2. optical flow computed from each image is transformed into a “flow image" that contains the horizontal and vertical components: x flow values, y flow values, as well as the flow magnitude

     import cv2
     import numpy as np
     import matplotlib.pyplot as plt
    
    frame1 = cv2.imread('C:/Users/Documents/images/frame_det_00_000001.bmp')
    frame2 = cv2.imread('C:/Users/Documents/images/frame_det_00_000001.bmp')
    prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
    next1 = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)
    
    
    flow = cv2.calcOpticalFlowFarneback(prvs, next1, None,0.5, 3, 15, 3, 5, 1.2, 0)
    
    horz = cv2.normalize(flow[...,0], None, 0, 255, cv2.NORM_MINMAX)
    vert = cv2.normalize(flow[...,1], None, 0, 255, cv2.NORM_MINMAX)
    horz = horz.astype('uint8')
    vert = vert.astype('uint8')
    
    
    plt.subplot(1, 4, 1)
    plt.title('Frame 1')
    plt.imshow(frame1)
    plt.subplot(1, 4, 2)
    plt.title('Frame 2')
    plt.imshow(frame2)
    plt.subplot(1, 4, 3)
    plt.title('Horz')
    plt.imshow(horz)
    plt.subplot(1,4,4)
    plt.title('Vert')
    plt.imshow(vert)`
    
edit retag flag offensive close merge delete