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

问题描述 投票:0回答:1

我最近一直在做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功能?

python matplotlib
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.