PL01
PL01

Reputation: 113

How to extract velocity vectors of pixels from OpenCV calcOpticalFlowFarneback - Python version)

I want to extract the optical flow velocity vectors of every pixels between two frames. I am using the OpenCV function as follow:

flow = calcOpticalFlowFarneback(Previous_Gray, Current_Gray, Optical_Flow, 0.5, 3, 15, 3, 5, 1.2, 0);

Previous_Gray = previous frame 
Current_Gray = current frame 

I would like to know what is the format of flow and how to extract it.

Thanks ahead for any help! :-)

P.S. I know my question is almost identical to this one: How to extract velocity vectors of a pixels from calcOpticalFlowFarneback . However, I am coding in Python, and would like a solution in Python, please.

Upvotes: 1

Views: 3230

Answers (1)

koshy george
koshy george

Reputation: 680

Given below is the python code snippet for calculating optical flow every frame by Farneback method. Eventhough what given below is not the complete working code, (there is enough examples of this around the web), it shows how the flow is calculated.

#use this params
farneback_params={
        'pyr_scale':0.5,
        'levels':3,
        'winsize':15,
        'iterations': 3,
        'poly_n': 5,
        'poly_sigma':1.2,
        'flags':cv2.OPTFLOW_USE_INITIAL_FLOW
    }

#do initializations
while True:
    #for every frame,
    #get current_frame_gray
    flow = cv2.calcOpticalFlowFarneback(prev_frame_gray, current_frame_gray, flow, **farneback_params)
    prev_frame_gray = current_frame_gray

If you assume each frame is H x W size, the following assertions hold.

assert(flow.shape == (H, W, 2))
assert(flow.dtype == numpy.float32)

If you examine the documentation on Farneback method below,
http://docs.opencv.org/3.0-beta/modules/video/doc/motion_analysis_and_object_tracking.html, the following statement holds

for r in range(H):
    for c in range(W):
        prev_frame_gray[r,c] corresponds to current_frame_gray[r + flow[r,c, 1], c+ flow[r,c,0]]

So the velocity component of each pixel of the previous frame (prev_frame_gray) is

flow[r,c,0] in x- direction (columns)
flow[r,c,1] in y- direction (rows)

As various code examples on this shows, you can easily express the flow in polar form (magnitude, angle) by the following simple command

mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])

The mag and ang will be of shape (H, W) and dtype numpy.float32. The ang result gives angles in the range 0-2pi.

Upvotes: 3

Related Questions