代码之家  ›  专栏  ›  技术社区  ›  Germán

Linux中的OpenCV 3.1.0 imshow不适用于网络摄像机(Python)

  •  0
  • Germán  · 技术社区  · 7 年前

    我正在尝试使用openCV官方教程中的代码来显示网络摄像头中的视频 cv2.imshow() 在Ubuntu/Python 3.6中:

    import numpy as np
    import cv2
    cap = cv2.VideoCapture(0)
    
    while(True):
        # Capture frame-by-frame
        ret, frame = cap.read()
    
        # Our operations on the frame come here
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
        # Display the resulting frame
        cv2.imshow('frame',gray)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()
    

    我得到以下错误 cv2.imshow() :

    该功能未实现。使用Windows、GTK+2重建库。x或碳支架。如果您在Ubuntu或Debian上,请安装libgtk2.0-dev和pkg config,然后在函数cvShowImage中重新运行cmake或配置脚本

    在搜索错误时,我偶然发现了这篇文章,作为类似问题的替代答案:

    https://pypi.python.org/pypi/opencv-python

    重要提示:MacOS和Linux wheels目前有一些局限性:

    • 不支持视频相关功能(未使用FFmpeg编译)
    • 例如cv2。imshow()将不起作用(未使用GTK+2.x或Carbon支持编译)

    还要注意,要从另一个源安装,首先必须删除opencv python包

    OpenCV error: the function is not implemented

    OpenCV not working properly with python on Linux with anaconda. Getting error that cv2.imshow() is not implemented

    大多数其他openCV函数工作正常。

    有替代方案吗 cv2.imshow() 它使用标准的anaconda库,所以我不必重新编译openCV或使用Python 2.7?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Germán    7 年前

    我拼凑了一个快速而肮脏的片段,其中使用了 matplotlib.animation 与cv2相似。imshow()预计用于网络摄像机视频:

    import cv2
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    cap = cv2.VideoCapture(0)
    ret, frame = cap.read()
    # The following is the replacement for cv2.imshow():
    fig = plt.figure() 
    ax = fig.add_subplot(111)
    im = ax.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), animated=True)
    def updatefig(*args):
        ret, frame = cap.read()
        im.set_array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
        return im
    ani = animation.FuncAnimation(fig, updatefig, interval=10)
    plt.show()
    

    我发现这非常有用,因为我能够在网络摄像机视频上输出许多重叠的绘图,这要归功于以下事实: matplotlib.animation.FuncAnimation 可以为连接到的任意多个绘图设置动画 ax 对象,只要它们在 updatefig() 上述功能。

    import matplotlib
    matplotlib.use('qt5agg')