python - 实时结合 cv2.imshow() 和 matplotlib plt.show()

标签 python opencv matplotlib

我正在尝试使用 openCV 合并来自网络摄像头的提要,然后使用 matplotlib 更新图表。

获取和显示框架的一个基本示例:

import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Display the resulting frame
    cv2.imshow('frame',frame)

    # When to exit loop - terminate program
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

使用 matplotlib 连续更新图形(随机绘制)的示例:

import numpy as np
import matplotlib.pyplot as plt

x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

# x goes from 0-9 numbers
# y goes from 0-100%
fig = plt.figure()
ax = plt.axes(xlim=(0, 9), ylim=(0, 100))
# line, = ax.plot([], [], lw=2)
rects = plt.bar(x, y, color='b')

def animate(i):
    y = random.sample(xrange(100), 10)
    for rect, yi in zip(rects, y):
        rect.set_height(yi)
    return rects

anim = animation.FuncAnimation(fig, animate,
                           frames=200, interval=20, blit=True)

plt.show()

所以我想要的是将两者结合在一起。该图应该通过传递我从帧中获得的结果来更新。我面临的主要问题是让两个窗口并排同时更新。 plt.show() 似乎阻止了其他一切。

关于如何解决的任何想法?

干杯

最佳答案

下面是一个将 plt.figure() 转换为 np.array 并使用 cv2.imshow 将其显示在相机源中的示例>/p>

import matplotlib
matplotlib.use('TkAgg')

import numpy as np
import cv2
import matplotlib.pyplot as plt

fig = plt.figure()
cap = cv2.VideoCapture(0)


x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)

y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)


line1, = plt.plot(x1, y1, 'ko-')        # so that we can update data later

for i in range(1000):
    # update data
    line1.set_ydata(np.cos(2 * np.pi * (x1+i*3.14/2) ) * np.exp(-x1) )

    # redraw the canvas
    fig.canvas.draw()

    # convert canvas to image
    img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8,
            sep='')
    img  = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))

    # img is rgb, convert to opencv's default bgr
    img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)


    # display image with opencv or any operation you like
    cv2.imshow("plot",img)

    # display camera feed
    ret,frame = cap.read()
    cv2.imshow("cam",frame)

    k = cv2.waitKey(33) & 0xFF
    if k == 27:
        break

关于python - 实时结合 cv2.imshow() 和 matplotlib plt.show(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43099734/

相关文章:

python - Tkinter 错误 : cannot use geometry manager grid inside . 已经有由包管理的从站

python - .withColumn和.agg在pyspark中并行计算吗?

python - 为什么对 ndarray 进行切片会 reshape 它的形状?

python - 使用艺术家动画每秒拍摄快照

通过 socks(代理)的 Python ssh 客户端

python - 阿普塔纳工作室 3 : How to apply themes to python editor

Python 的 OpenCV cv2.imread 总是返回 None 并且 cvFeatDetector 使 python 崩溃

ios - ld : file too small for architecture i386

OpenCV 在不同机器上加载视频文件时出错

python - 在python matplotlib中将轴添加到colorbar