我试图使用openCV从网络摄像头获取视频流,并使用matplotlib更新一个图表。
为了获取和显示帧,这里有一个基本的例子:
谢谢。
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow('frame',frame)
# When to exit loop - terminate program
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
使用matplotlib连续更新图表的示例(随机绘图):
import numpy as np
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# x goes from 0-9 numbers
# y goes from 0-100%
fig = plt.figure()
ax = plt.axes(xlim=(0, 9), ylim=(0, 100))
# line, = ax.plot([], [], lw=2)
rects = plt.bar(x, y, color='b')
def animate(i):
y = random.sample(xrange(100), 10)
for rect, yi in zip(rects, y):
rect.set_height(yi)
return rects
anim = animation.FuncAnimation(fig, animate,
frames=200, interval=20, blit=True)
plt.show()
所以我想要的是将两者结合起来。通过传递我从帧中获得的结果来更新图形。我面临的主要问题是让两个窗口同时并排更新。 plt.show() 似乎阻塞了其他所有操作。你有什么解决办法吗?谢谢。