使用实时相机预览更新 matplotlib 中的帧
update frame in matplotlib with live camera preview
我是 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()
函数动画
另一种选择当然是使用内置的 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()
为了在按键事件中关闭 window,您可以添加一个回调,像这样
#... 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 的新手。我的电脑连接了两个 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()
函数动画
另一种选择当然是使用内置的 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()
为了在按键事件中关闭 window,您可以添加一个回调,像这样
#... 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.