我是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.
Run Code Online (Sandbox Code Playgroud)