gpt4 book ai didi

python - 在 Python OpenCV 中创建 FlowMap

转载 作者:太空宇宙 更新时间:2023-11-03 22:50:47 26 4
gpt4 key购买 nike

更新的问题:

谁能给我指出任何可以帮助我在 python 中绘制光流图的 Material 的方向?理想情况下,我想找到可以提供与此处显示的视频类似输出的内容: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 箭袋图的行为,但我确信这是可能的。

另一种方法是创建一个函数,根据计算出的光流在原始图像的顶部画线。以下代码应实现此目的:

def dispOpticalFlow( Image,Flow,Divisor,name ):
"Display image with a visualisation of a flow over the top. A divisor controls the density of the quiver plot."
PictureShape = np.shape(Image)
#determine number of quiver points there will be
Imax = int(PictureShape[0]/Divisor)
Jmax = int(PictureShape[1]/Divisor)
#create a blank mask, on which lines will be drawn.
mask = np.zeros_like(Image)
for i in range(1, Imax):
for j in range(1, Jmax):
X1 = (i)*Divisor
Y1 = (j)*Divisor
X2 = int(X1 + Flow[X1,Y1,1])
Y2 = int(Y1 + Flow[X1,Y1,0])
X2 = np.clip(X2, 0, PictureShape[0])
Y2 = np.clip(Y2, 0, PictureShape[1])
#add all the lines to the mask
mask = cv2.line(mask, (Y1,X1),(Y2,X2), [255, 255, 255], 1)
#superpose lines onto image
img = cv2.add(Image,mask)
#print image
cv2.imshow(name,img)
return []

这段代码只创建线条而不是箭头,但通过一些努力可以修改它以显示箭头。

关于python - 在 Python OpenCV 中创建 FlowMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38172370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com