更新问题:
有人能给我指出任何可以帮助我用python绘制光流图的材料的方向吗?理想情况下,我想找到一个类似于这里显示的视频输出的东西:http://study.marearts.com/2014/04/opencv-study-calcopticalflowfarneback.html。或者有类似功能输出的东西
我已经实现了密集光流算法(cv2.calcOpticalFlowFarneback)。从这个角度,我可以在图像的特定点取样。 输入的视频源是640x480,我已经将采样点设置为垂直和水平每五个像素。在
import cv2
import numpy as np
import matplotlib.pyplot as plt
cap = cv2.VideoCapture("T5.avi")
ret, frame1 = cap.read()
prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
hsv = np.zeros_like(frame1)
hsv[..., 1] = 255
[R,C]=prvs.shape
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, 2, 5, 1.2, 0)
mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
RV=np.arange(5,480,5)
CV=np.arange(5,640,5)
# These give arrays of points to sample at increments of 5
if count==0:
count =1 #so that the following creation is only done once
[Y,X]=np.meshgrid(CV,RV)
# makes an x and y array of the points specified at sample increments
temp =mag[np.ix_(RV,CV)]
# this makes a temp array that stores the magnitude of flow at each of the sample points
motionvectors=np.array((Y[:],X[:],Y[:]+temp.real[:],X[:]+temp.imag[:]))
Ydist=motionvectors[0,:,:]- motionvectors[2,:,:]
Xdist=motionvectors[1,:,:]- motionvectors[3,:,:]
Xoriginal=X-Xdist
Yoriginal=Y-Ydist
plot2 = plt.figure()
plt.quiver(Xoriginal, Yoriginal, X, Y,
color='Teal',
headlength=7)
plt.title('Quiver Plot, Single Colour')
plt.show(plot2)
hsv[..., 0] = ang * 180 / np.pi / 2
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imshow('frame2', bgr)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
prvs = next
cap.release()
cv2.destroyAllWindows()
我想我已经计算了像素的原始和最终的X,Y位置和移动的距离,并将它们放入matplotlib震颤图中。在
我得到的结果与稠密光流的hsv图(我知道这是正确的,因为它是从OpenCV教程中获取的)不一致,并且箭筒图每次只显示一个帧,在下一个显示之前必须退出该图。在
有人能看到我在计算中哪里出错了吗?我如何使绘图在每一帧中自动更新?在
我不知道如何改变matplotlib震颤图的行为,但我确信这是可能的。在
另一种方法是创建一个函数,根据计算的光流在原始图像的顶部绘制线。以下代码应实现这一点:
这段代码只创建行而不是箭头,但是经过一些努力,它可以被修改为显示箭头。在
相关问题 更多 >
编程相关推荐