我尝试使用matplotlib.pyplot在处理帧时查看视频。下面的代码显示了一个绘图窗口,但是从不绘制图像。
import cv2
import matplotlib.pyplot as plt
class Viewer:
def __init__(self, video_filename):
self.cap = cv2.VideoCapture(video_filename)
self.ax1 = plt.subplot(1, 2, 1)
self.ax2 = plt.subplot(1, 2, 2)
# Grab the first frame.
frame = self.grabframe()
self.im1 = self.ax1.imshow(frame)
self.im2 = self.ax2.imshow(frame)
def grabframe(self):
ret, frame = self.cap.read()
if ret:
return frame
else:
print('[ERROR] grabframe failed')
return self.grabframe()
def update(self, frame):
"""Update the display."""
print('[INFO] updating...')
self.im1.set_data(frame)
self.im2.set_data(frame)
plt.draw()
def main():
"""Run it."""
cli = argparse.ArgumentParser()
cli.add_argument('-i', '--input', type=str, required=True)
args = cli.parse_args()
viewer = Viewer(args.input)
while True:
viewer.update(viewer.grabframe())
if __name__ == '__main__':
plt.ion()
plt.show()
main()
肯定会调用更新功能,我确信这些帧包含图像数据。我试着明确地创建一个图并将轴添加到该图中。我也试过了fig.canvas.draw
。我也尝试在主函数中调用plt.show()
。在任何这些情况下都没有任何改变。
为什么我不希望在图上绘制图像?