OpenCV的密集光流(Farneback)功能输出什么?如何使用它在Python中构建光流图? [英] What is output from OpenCV's Dense optical flow (Farneback) function? How can this be used to build an optical flow map in Python?
问题描述
我正在尝试使用Opencv的密集光流函数的输出绘制运动矢量的颤动图,但无法找到该函数实际输出的内容.这是代码:
I am trying to use the output of Opencv's dense optical flow function to draw a quiver plot of the motion vectors but have not been able to find what the function actually outputs. Here is the code:
import cv2
import numpy as np
cap = cv2.VideoCapture('GOPR1745.avi')
ret, frame1 = cap.read()
prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
hsv = np.zeros_like(frame1)
hsv[...,1] = 255
count=0
while(1):
ret, frame2 = cap.read()
next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)
flow = cv2.calcOpticalFlowFarneback(prvs,next,None, 0.5, 3, 15, 3, 10, 1.2, 0)
mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
hsv[...,0] = ang*180/np.pi/2
hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
rgb = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
if count==10:
count=0
print "flow",flow
cv2.imshow('frame2',rgb)
count=count+1
k = cv2.waitKey(30) & 0xff
if k == 27:
break
elif k == ord('s'):
prvs = next
cap.release()
cv2.destroyAllWindows()
这实际上与关于密集光流的OpenCv教程中给出的代码相同.我从打印功能收到以下输出:
This is effectively the same code as given in the OpenCv tutorial on dense optical flow. I receive the following output from the print function:
flow [[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
...,
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
...,
[[ -3.54891084e-14 -1.38642463e-14]
[ -2.58058853e-14 -1.54020863e-14]
[ -5.56561768e-14 -1.88019359e-14]
...,
[ -7.59403916e-15 1.16633225e-13]
[ 7.22156371e-14 -1.61951507e-13]
[ -4.30715618e-15 -4.39530987e-14]]
[[ -3.54891084e-14 -1.38642463e-14]
[ -2.58058853e-14 -1.54020863e-14]
[ -5.56561768e-14 -1.88019359e-14]
...,
[ -7.59403916e-15 1.16633225e-13]
[ 7.22156371e-14 -1.61951507e-13]
[ -4.30715618e-15 -4.39530987e-14]]
我想知道这些值到底是什么?原始的X,Y坐标?最终的X,Y坐标?距离移动了吗?
I would like to know what exactly these values are? Original X,Y coordinates? Final X,Y coordinates? Distance moved?
我计划尝试使用下一页中的代码找到初始坐标和最终坐标以绘制颤动图: https://www.getdatajoy.com/examples/python-plots/vector-fields 这是因为在python中,我不知道有可以为您绘制光流图的函数.
I plan to try and find the initial and final coordinates to make a quiver plot using code from the following page: https://www.getdatajoy.com/examples/python-plots/vector-fields This is because in python there is no function that i am aware of that plots an optical flow map for you.
提前谢谢!
推荐答案
您快到了.首先让我们看一下 calcOpticalFlowFarneback文档在那里:
You were almost there. Lets first take a look at the calcOpticalFlowFarneback Documentation it says there:
flow
–计算后的流图像,其大小与prev
相同,并键入CV_32FC2
.
flow
– computed flow image that has the same size asprev
and typeCV_32FC2
.
因此,您实际上得到的是一个与输入框大小相同的矩阵.
该flow
矩阵中的每个元素都是一个点,代表该像素从prev
帧开始的位移.这意味着您将获得一个具有x和y值(以像素为单位)的点,该点将为您提供上一帧的增量x和增量y.
So what you are actually getting is a matrix that has the same size as your input frame.
Each element in that flow
matrix is a point that represents the displacement of that pixel from the prev
frame. Meaning that you get a point with x and y values (in pixel units) that gives you the delta x and delta y from the last frame.
这篇关于OpenCV的密集光流(Farneback)功能输出什么?如何使用它在Python中构建光流图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!