小编典典

使用实时摄像机预览更新matplotlib中的帧

python

我是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.

阅读 206

收藏
2020-12-20

共1个答案

小编典典

互动模式

在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.
2020-12-20