Ask Your Question
0

Dense Optical Flow(Farneback) Output

asked 2016-07-04 13:24:10 -0600

I have another question regarding the output matrix of OpenCVs Dense optical Flow function (Farneback). I have asked a question similar to this recently

(http://stackoverflow.com/questions/38...)

And from that i now know that the values stored in the matrix are the X and Y distances that that specific pixel moved relative to the previous frame.(Please correct me if i'm wrong).

I am using a 640x480 pixel video feed to calculate the optical flow on, and the shape of the flow matrix is shown in the printed results below, npte that i used a break after reading the first 2 frames just to show the structure of the array.

import cv2
import numpy as np

cap = cv2.VideoCapture("T5.avi")

ret, frame1 = cap.read()
prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)

while (1):
    ret, frame2 = cap.read()
    next = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)

    flow = cv2.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 2, 5, 1.2, 0)
    print flow.shape
    print "Flow : : 0"
    print flow[:][:][0]
    print "Flow : : 1"
    print flow[:][:][1]
    break

This has the following output:

(480, 640, 2)
Flow : : 0
[[ 0.01214151  0.22083586]
 [ 0.01184586  0.18637304]
 [ 0.01057486  0.15194368]
 ..., 
 [ 0.00064609 -0.00283471]
 [ 0.00046074  0.0047204 ]
 [ 0.000404   -0.00282944]]
Flow : : 1
[[ 0.0152726   0.35010788]
 [ 0.01538487  0.28910625]
 [ 0.01413684  0.22534071]
 ..., 
 [ 0.00082013 -0.00668656]
 [ 0.00060558  0.00633681]
 [ 0.00056752 -0.00331147]]

I am now wondering now is why there are 2 values stored in each of those places? Are there two X and Y values being stored? Possibly initial and final positions? Or do the components have imaginary components?

I've done quite alot of searching but haven't been able to find anything that explains this.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-07-05 01:11:24 -0600

berak gravatar image

imho, your way to acess the flow array is broken.

a 'correct' numpy slice would look like:

flow[:,:,0] # 1st channel (dx)
flow[:,:,1] # 2nd channel (dy)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-07-04 13:24:10 -0600

Seen: 1,083 times

Last updated: Jul 04 '16