简体   繁体   中英

OpenCV Dense Optical Flow Matrix

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

( What is output from OpenCV's Dense optical flow (Farneback) function? How can this be used to build an optical flow map in Python? )

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.

Fast answer: For every pixel, you are getting the displacement value on both X and Y axes.

I think you are mixing two different things here:
As I mentioned in your last post and as we see here, your matrix dimensions are (480, 640, 2) . Don't mix between the X,Y value that represents a specific pixel place in the frame, and the DeltaX,DeltaY displacement values inside every pixel place.

For example, flow[20][20][:] represents the pixel at location 20 x 20 and it is actually a point with 2 float values in it - the DeltaX,DeltaY we talked about before.
So, flow[20][20][0] is actually the DeltaX at pixel 20x20 and flow[20][20][1] is the DeltaY at the same pixel( 20x20 ).

Hope this is clear now.

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