我对 Python 和 Matplotlib 都很陌生。我的计算机连接到两个USB摄像头,我打算使用matplotlib中的subplot(1,2,1)和subplot(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.
最佳答案
交互模式
在 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()
FuncAnimation
另一种选择当然是使用 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.
关于python - 使用实时相机预览更新 matplotlib 中的帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44598124/