我是Python和Matplotlib的新手。我的计算机连接到两台USB摄像机,我打算使用matplotlib中的子图(1,2,1)和子图(1,2,2)按时间顺序绘制两台摄像机的帧。当我使用我的代码执行此操作时,我要么只绘制一个帧,要么在绘图区域中获得黑屏。
我的代码如下所示
#import
import cv2
import matplotlib.pyplot as plt
#Initiate the two cameras
cap1 = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(1)
#Capture the frames from camera 1 and 2 and display them over time using matplotlib
while True:
#grab frame from camera 1 and 2
ret1,frame1 = cap1.read()
ret2,frame2 = cap2.read()
plt.subplot(1,2,1), plt.imshow(cv2.cvtColor(frame1,cv2.COLOR_BGR2RGB))
plt.subplot(1,2,2), plt.imshow(cv2.cvtColor(frame2,cv2.COLOR_BGR2RGB))
#draw the plot
plt.show(False)
#Result is black screen. If plt.show() is called, I see the frames but then it freezes.
答案 0 :(得分:4)
在matplotlib中更新绘图的一种方法是使用交互模式(plt.ion()
)。
然后,您不应为捕获的每个帧重新创建新的子图,而是使用图像创建一次图并在之后更新它。
import cv2
import matplotlib.pyplot as plt
def grab_frame(cap):
ret,frame = cap.read()
return cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
#Initiate the two cameras
cap1 = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(1)
#create two subplots
ax1 = plt.subplot(1,2,1)
ax2 = plt.subplot(1,2,2)
#create two image plots
im1 = ax1.imshow(grab_frame(cap1))
im2 = ax2.imshow(grab_frame(cap2))
plt.ion()
while True:
im1.set_data(grab_frame(cap1))
im2.set_data(grab_frame(cap2))
plt.pause(0.2)
plt.ioff() # due to infinite loop, this gets never called.
plt.show()
另一种选择当然是使用matplotlib内置的FuncAnimation
,它专门用于动画图。
import cv2
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def grab_frame(cap):
ret,frame = cap.read()
return cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
#Initiate the two cameras
cap1 = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(1)
#create two subplots
ax1 = plt.subplot(1,2,1)
ax2 = plt.subplot(1,2,2)
#create two image plots
im1 = ax1.imshow(grab_frame(cap1))
im2 = ax2.imshow(grab_frame(cap2))
def update(i):
im1.set_data(grab_frame(cap1))
im2.set_data(grab_frame(cap2))
ani = FuncAnimation(plt.gcf(), update, interval=200)
plt.show()
为了关闭按键事件的窗口,您可以添加回调,如此
#... other code
ani = FuncAnimation(plt.gcf(), update, interval=200)
def close(event):
if event.key == 'q':
plt.close(event.canvas.figure)
cid = plt.gcf().canvas.mpl_connect("key_press_event", close)
plt.show()
# code that should be executed after window is closed.