OpenCV 无法在带有 anaconda 的 Linux 上与 python 正常工作。获取未实现 cv2.imshow() 的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40207011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:16:58  来源:igfitidea点击:

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

pythonopencvubuntuanaconda

提问by Job Martinez

This is the exact error that I am getting. My OS is Ubuntu 16.10.

这是我得到的确切错误。我的操作系统是 Ubuntu 16.10。

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 /feedstock_root/build_artefacts/work/opencv-3.1.0/modules/highgui/src/window.cpp, line 545 Traceback (most recent call last): File "untitled.py", line 7, in cv2.imshow('image',img) cv2.error: /feedstock_root/build_artefacts/work/opencv-3.1.0/modules/highgui/src/window.cpp:545: 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

OpenCV Error: Unspecified error (该函数没有实现。使用Windows、GTK+ 2.x或Carbon支持重建库。如果你在Ubuntu或Debian上,安装libgtk2.0-dev和pkg-config,然后重新运行cmake或配置脚本)在 cvShowImage,文件 /feedstock_root/build_artefacts/work/opencv-3.1.0/modules/highgui/src/window.cpp,第 545 行回溯(最近一次调用):文件“untitled.py”,第 7 行, 在 cv2.imshow('image',img) cv2.error: /feedstock_root/build_artefacts/work/opencv-3.1.0/modules/highgui/src/window.cpp:545: error: (-2) 函数是未实现。使用 Windows、GTK+ 2.x 或 Carbon 支持重建库。如果您使用的是 Ubuntu 或 Debian,请安装 libgtk2.0-dev 和 pkg-config,然后重新运行 cmake 或在函数 cvShowImage 中配置脚本

my code is:

我的代码是:

import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('0002.png',0)

cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

0002.png is an image in the same directory as the program. I first installed anaconda with python 3.5, then I installed opencv by using the command

0002.png 是与程序在同一目录中的图像。我首先用python 3.5安装了anaconda,然后我使用命令安装了opencv

conda install -c conda-forge opencv

I installed libgtk2.0-dev just as the error said to but I still get the same error. Any help would be much appreciated. I've been trying to solve this for several hours.

我安装了 libgtk2.0-dev 就像错误所说的那样,但我仍然遇到同样的错误。任何帮助将非常感激。我一直试图解决这个问题几个小时。

回答by Alex Glinsky

1.The easiest way:

1.最简单的方法:

conda remove opencv
conda update conda
conda install --channel menpo opencv

or (for OpenCV 3.1) :

或(对于 OpenCV 3.1):

conda install -c menpo opencv3

2.And if u don't want to do this, you can try to use matplotlib.

2.如果你不想这样做,你可以尝试使用matplotlib

import cv2
import matplotlib.pyplot as plt

img = cv2.imread('img.jpg',0)

plt.imshow(img, cmap='gray')
plt.show()

3.Or try to build library by your own with option WITH_GTK=ON, or smth like that.

3.或者尝试使用 option 自己构建库WITH_GTK=ON,或者类似的。

Update- 18th Jun 2019

更新- 2019 年 6 月 18 日

I got this error on my Ubuntu(18.04.1 LTS) system for openCV 3.4.2, as the method call to cv2.imshow was failing. I am using anaconda. Just the below 2 steps helped me resolve:

我在 openCV 3.4.2 的 Ubuntu(18.04.1 LTS) 系统上遇到此错误,因为对 cv2.imshow 的方法调用失败。我正在使用蟒蛇。只是以下两个步骤帮助我解决:

conda remove opencv
conda install -c conda-forge opencv=4.1.0

If you are using pip, you can try

如果您正在使用 pip,您可以尝试

pip install opencv-contrib-python

回答by Lawrence Amadi

I have had to deal with this issue a couple of times, this is what has worked consistently thus far:

我不得不多次处理这个问题,这是迄今为止一直有效的方法:

conda remove opencv
conda install -c menpo opencv
pip install --upgrade pip
pip install opencv-contrib-python

回答by Nic Szerman

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 NOTEMacOS and Linux wheels have currently some limitations:

  • video related functionality is not supported (not compiled with FFmpeg)
  • for example cv2.imshow()will not work (not compiled with GTK+ 2.x or Carbon support)

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

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

Also note that to install from another source, first you must remove the opencv-python package

另请注意,要从其他来源安装,首先必须删除 opencv-python 包

回答by Yedhrab

Working method (? tested on April 19, 2019)

工作方法(?2019年4月19日测试)

  • These error happen because of conda.
  • Open Anaconda Promptand remove conda opencv if installed
  • ? conda remove opencv
  • 这些错误是由于 conda 发生的。
  • 打开Anaconda Prompt并删除 conda opencv(如果已安装)
  • ? conda remove opencv

If you have conda env, firstly activate it conda activate <your_env_name>

如果你有 conda env,首先激活它 conda activate <your_env_name>

  • ? After install opencv via pip (click hereto offical info)
  • ? pip install opencv-contrib-python
  • ? 通过pip安装opencv后(点击此处查看官方信息)
  • ? pip install opencv-contrib-python

if pip haven't installed, use conda install pipcommand.

如果 pip 尚未安装,请使用conda install pip命令。

回答by Nitin

I followed this tutorial (OpenCV GTK+2.x error) and did the following. It worked for me :

我按照本教程(OpenCV GTK+2.x 错误)进行了以下操作。它对我有用:

  1. install the packages : libgtk2.0-dev and pkg-config
  2. cd to your opencv directory
  3. mkdir Release
  4. cd Release
  5. Run the command : cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_GTK=ON -D WITH_OPENGL=ON ..
  6. make
  7. sudo make install
  1. 安装软件包:libgtk2.0-dev 和 pkg-config
  2. cd 到你的 opencv 目录
  3. mkdir 发布
  4. 光盘发行
  5. 运行命令: cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_GTK=ON -D WITH_OPENGL=ON ..
  6. 制作
  7. 须藤制作安装

回答by F1iX

For me (Arch Linux, Anaconda with Python 3.6), installing from the suggested channels menpoor loopbiodid not change anything. My solution (see related question) was to

对我来说(Arch Linux,Anaconda with Python 3.6),从建议的渠道安装menpoloopbio没有改变任何东西。我的解决方案(见相关问题)是

  1. install pkg-config (sudo pacman -Syu pkg-config),
  2. remove opencv from the environment (conda remove opencv) and
  3. re-install opencv from the conda-forge channel (conda install -c conda-forge opencv)
  1. 安装 pkg-config ( sudo pacman -Syu pkg-config),
  2. 从环境中删除 opencv ( conda remove opencv) 和
  3. 从 conda-forge 频道 ( conda install -c conda-forge opencv)重新安装 opencv

conda listnow returns opencv 3.3.0 py36_blas_openblas_203 [blas_openblas] conda-forgeand all windows launched using cv2 are working fine.

conda list现在返回opencv 3.3.0 py36_blas_openblas_203 [blas_openblas] conda-forge并且所有使用 cv2 启动的窗口都工作正常。

回答by user3177227

Notice that it is complaining for libgtk2.0-dev and pkg-config. Here is the solution. Uninstall your existing openCV installation.

请注意,它正在抱怨 libgtk2.0-dev 和 pkg-config。这是解决方案。卸载现有的 openCV 安装。

conda remove opencv3

conda 删除 opencv3

Install these packages before installing opencv- conda install gtk2 pkg-config

在安装 opencv-conda install gtk2 pkg-config 之前安装这些包

Now install opencv from menpo conda install -c https://conda.anaconda.org/menpoopencv3

现在从 menpo conda install -c https://conda.anaconda.org/menpoopencv3安装 opencv

回答by Jodo

I used pip to install opencv-python. (https://pypi.org/project/opencv-python/)

我使用 pip 安装opencv-python。( https://pypi.org/project/opencv-python/)

1) Remove the opencv package from conda:

1) 从 conda 中删除 opencv 包:

>> conda remove opencv

2) To your env.yml file add this:

2) 在您的 env.yml 文件中添加以下内容:

...
dependencies:
  - numpy
  - pytest
  ...
  - pip:
    - opencv-python

回答by vlbthambawita

Remove opencv from anaconda=

从 anaconda= 中删除 opencv

conda remove opencv

conda 删除 opencv

Then, reinstall opencv using pip:

然后,使用 pip 重新安装 opencv:

pip install opencv

pip 安装 opencv

This is working for me.

这对我有用。

回答by user10774310

My Envirment is Win10, and I added the anaconda path to the environment variables's PATH',the cv2.imshow worked

我的环境是 Win10,我将 anaconda 路径添加到环境变量的 PATH',cv2.imshow 工作

C:\Users\user\Anaconda3
C:\Users\user\Anaconda3\Scripts

Then restart the windows

然后重启windows