当前位置: 首页 > 面试题库 >

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

越英韶
2023-03-14
问题内容

我是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()

为了在按键事件时关闭窗口,您可以像这样添加回调

#... 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.


 类似资料:
  • 问题内容: 我遇到问题,我想在UIView边界内解决,有什么 办法可以将Camera Preview添加到UIView吗?并在 UIView 顶部添加其他内容(按钮,标签等)? 我尝试使用AVFoundation框架,但没有足够的文档 的斯威夫特。 问题答案: 更新为SWIFT 5 您可以尝试如下操作: 在这里,我使用一个UIView称为PreviewView的相机来启动摄像机,然后在Previe

  • 我已经和https://github.com/commonsguy/cwac-camera 创建了我的自定义CameraFragment和自定义CameraHost。 我已经重写了getPictureSize和getPreviewSize。通过这两种方法,我能够找出可以使用的大小。我的总体想法是拍摄一幅方形图像,并在拍摄前预览它。 我一直在测试,没有设备返回正方形格式的大小,所以我想最好的方法是选

  • 我正在准备一个小的Android应用程序,捕捉照片和录音从用户。类似用户会用他的相机拍一张照片,然后在观看该照片时添加音频评论。

  • 可以在 Adobe Bridge 的“预览”面板中,以全屏预览和审阅模式预览图像。“预览”面板会显示多达九张缩览图图像供您快速比较。全屏预览会以全屏方式显示图像。审阅模式以全屏视图显示图像,允许您在图像中导航,优化选择,标记、旋转图像和给图像评级,以及在 Camera Raw 中打开图像。 以幻灯片放映的方式查看图像 使用“幻灯片放映”命令可以全屏幕幻灯片放映的方式查看缩览图。在处理一个文件夹中所

  • 我已经创建了一个自定义相机应用程序,我正在使用,。我已经成功地完成了它,现在我正在尝试应用过滤器(乌贼模式,空白n白色等)的相机预览。如何在摄影机预览上应用效果。 这是我的视频。渲染器 }

  • 我想在WebRTC上使用IP摄像机。然而,webrtc似乎只支持网络摄像头。所以我尝试将IP摄像机的流转换为虚拟网络摄像机。 我找到了像IP摄像机适配器这样的软件,但它们不太好用(每秒2-3帧,延迟2秒),它们只在Windows上工作,我更喜欢使用Linux(如果可能的话)。 我尝试FFMPEG/AVCONV: > 首先,我使用v4l2loopback创建了一个虚拟设备(命令为:)。虚拟设备会被检