执行cv2.imshow()时遇到的问题

时间:2017-12-17 06:01:11

标签: python matplotlib

我最近一直在做opencv。安装在我的Ubuntu 16.04上。我认为它有一些问题。每当我尝试运行该功能时

cv2.imshow('frame',frame)

它造成了这个错误

OpenCV Error: Unspecified error (The function is not implemented. 
Rebuild the library with Windows, GTK+ 2.x or Carbon support. 
If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvShowImage, file /tmp/build/80754af9/opencv_1512687413662/work/modules/highgui/src/window.cpp, line 611
Traceback (most recent call last): 
     File "hands.py", line 12, in <module>
     cv2.imshow('frame',frame)
     cv2.error: /tmp/build/80754af9/opencv_1512687413662/work/modules/highgui/src/window.
cpp:611: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. 
    If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvShowImage

我试图运行的代码是

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

cap = cv2.VideoCapture(0)
bg = cv2.bgsegm.createBackgroundSubtractorMOG()

while True:
    ret,frame = cap.read()
    vid = bg.apply(frame)

    cv2.imshow('frame',frame)
    cv2.imshow('vid',vid)
    key = cv2.waitKey(0) & 0xff
    if key == 27:
        break

cap.release()
cap.destroyAllWindows()

我试图谷歌一切可能的事情并尽一切可能,但仍然无法解决问题。

我也尝试过使用

matplotlib.pyplot.imshow('frame',frame) 
matplotlib.pyplot.show()

而不是

cv2.imshow('frame',frame)

但这会产生错误

TypeError: unhashable type: 'numpy.ndarray'

显示从我的网络摄像头捕获的视频。 它要么只显示图像,要么在cv2.waitkey()中显示错误,而不是视频或此错误。 有没有办法解决这个错误?或实现cv2 GUI功能?

1 个答案:

答案 0 :(得分:0)

好像你的cv2安装有问题。我无法解决这个问题,但我建议重新安装并确保使用正确的系统版本(匹配操作系统,位深度和python版本)。

作为一种解决方法,您可能确实使用matplotlib来显示图像。最小的例子是

import cv2
import matplotlib.pyplot as plt

cap = cv2.VideoCapture(0)

plt.ion()
fig, ax = plt.subplots()
ret,frame = cap.read()
im = ax.imshow(frame)

while True:
    ret,frame = cap.read()
    im.set_data(frame)
    plt.pause(0.5)

plt.ioff()
plt.show()

另见update frame in matplotlib with live camera preview

相关问题