【问题标题】:OpenCV 3.1.0 imshow in Linux does not work for webcam (Python)Linux 中的 OpenCV 3.1.0 imshow 不适用于网络摄像头 (Python)
【发布时间】:2018-03-24 21:25:18
【问题描述】:

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

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 或 Carbon 支持重建库。如果你在 Ubuntu 或 Debian 上,安装 libgtk2.0-dev 和 pkg-config,然后重新运行 cmake 或在函数 cvShowImage 中配置脚本

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

如果您在任何时候使用 opencv-python pip 包安装 OpenCV,请注意以下注释,取自 https://pypi.python.org/pypi/opencv-python

重要提示 MacOS 和 Linux 轮子目前有一些限制:

  • 不支持视频相关功能(未使用 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?

【问题讨论】:

    标签: python linux opencv anaconda webcam


    【解决方案1】:

    我拼凑了一个快速而肮脏的 sn-p,它使用 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() 函数中更新。

    编辑:当我在 Jupyter 笔记本中工作时,我添加了以下内容以便在新窗口中查看视频:

    import matplotlib
    matplotlib.use('qt5agg')
    

    【讨论】:

      猜你喜欢
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 2016-03-05
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      • 2017-02-11
      • 1970-01-01
      相关资源
      最近更新 更多