简体   繁体   English

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

[英]OpenCV 3.1.0 imshow in Linux does not work for webcam (Python)

I'm trying to use code from the official openCV tutorial for showing video from webcam using cv2.imshow() in Ubuntu/Python 3.6: 我正在尝试使用官方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()

And I get the following error for cv2.imshow() : 而且我得到cv2.imshow()的以下错误:

The function is not implemented. 该功能未实现。 Rebuild the library with Windows, GTK+ 2.x or Carbon support. 在Windows,GTK + 2.x或Carbon支持下重建库。 If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvShowImage 如果您使用的是Ubuntu或Debian,请安装libgtk2.0-dev和pkg-config,然后重新运行cmake或在函数cvShowImage中配置脚本

When searching for the error I stumbled upon this post as an alternate answer to similar questions: 当寻找错误时,我偶然发现了这篇文章,作为对类似问题的替代答案:

If you installed OpenCV using the opencv-python pip package at any point in time, be aware of the following note, taken from https://pypi.python.org/pypi/opencv-python 如果您在任何时候使用opencv-python pip软件包安装了OpenCV,请注意以下注释,摘自https://pypi.python.org/pypi/opencv-python

IMPORTANT NOTE MacOS and Linux wheels have currently some limitations: 重要说明MacOS和Linux轮子目前有一些限制:

  • video related functionality is not supported (not compiled with FFmpeg) 不支持与视频相关的功能(未使用FFmpeg编译)
  • for example cv2.imshow() will not work (not compiled with GTK+ 2.x or Carbon support) 例如cv2.imshow()将不起作用(未使用GTK + 2.x或Carbon支持编译)

Also note that to install from another source, first you must remove the opencv-python package 另请注意,要从其他来源进行安装,首先必须删除opencv-python软件包

OpenCV error: the function is not implemented OpenCV错误:该功能未实现

OpenCV not working properly with python on Linux with anaconda. 在带有anaconda的Linux上,OpenCV无法与python一起正常使用。 Getting error that cv2.imshow() is not implemented 收到未实现cv2.imshow()的错误

Most other openCV functions work properly. 其他大多数openCV功能均正常运行。

Is there an alternative to cv2.imshow() that uses standard anaconda libraries so I don't have to recompile openCV or use Python 2.7? cv2.imshow()是否有使用标准anaconda库的替代方法,因此我不必重新编译openCV或使用Python 2.7?

I hacked together a quick and dirty snippet that uses matplotlib.animation and resembles what cv2.imshow() is expected to do with webcam video: 我整理了一个快速且肮脏的代码段,该代码段使用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()

I found this very useful, as I was able to output many overlapping plots on top of the webcam video thanks to the fact that matplotlib.animation.FuncAnimation can animate as many plots as there are attached to the ax object as long as they are updated in the updatefig() function above. 我发现这非常有用,因为由于matplotlib.animation.FuncAnimation可以动画化尽可能多的绘图(只要更新了ax对象),因此我能够在网络摄像头视频上输出许多重叠的绘图。在上面的updatefig()函数中。

Edit: As I was working in a Jupyter notebook, I added the following in order to see the video in a new window: 编辑:当我在Jupyter笔记本中工作时,我添加了以下内容以便在新窗口中观看视频:

import matplotlib
matplotlib.use('qt5agg')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM