我拼凑了一个快速而肮脏的片段,其中使用了
matplotlib.animation
与cv2相似。imshow()预计用于网络摄像机视频:
import cv2
import matplotlib.pyplot as plt
import matplotlib.animation as animation
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
# The following is the replacement for cv2.imshow():
fig = plt.figure()
ax = fig.add_subplot(111)
im = ax.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), animated=True)
def updatefig(*args):
ret, frame = cap.read()
im.set_array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
return im
ani = animation.FuncAnimation(fig, updatefig, interval=10)
plt.show()
我发现这非常有用,因为我能够在网络摄像机视频上输出许多重叠的绘图,这要归功于以下事实:
matplotlib.animation.FuncAnimation
可以为连接到的任意多个绘图设置动画
ax
对象,只要它们在
updatefig()
上述功能。
import matplotlib
matplotlib.use('qt5agg')