61

This is the exact error that I am getting. My OS is 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

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

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.

Job Martinez
  • 747
  • 1
  • 6
  • 9

16 Answers16

84

1.The easiest way:

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

or (for OpenCV 3.1) :

conda install -c menpo opencv3

2.And if u don't want to do this, you can try to use 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.

Update - 18th Jun 2019

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:

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

If you are using pip, you can try

pip install opencv-contrib-python
Prince
  • 166
  • 1
  • 12
Alex Glinsky
  • 3,346
  • 1
  • 17
  • 14
  • 2
    Or you can use `conda install --channel loopbio --channel conda-forge --channel pkgw-forge gtk2 ffmpeg ffmpeg-feature gtk2-feature opencv`. (Details [here](https://github.com/conda-forge/opencv-feedstock/issues/43).) – nekketsuuu May 04 '17 at 11:50
  • For conda (actually, [Miniconda](https://conda.io/miniconda.html)) it is better to use environments. Also, seems `opencv` installation temporaty fails for `conda` and Python 3.6, so for now it should be `conda create --name py3 python=3.5`, then `activate py3`, then `conda install -c menpo opencv3`. – shaman.sir Jul 12 '17 at 20:57
  • 3
    **UPDATE** : `opencv-python 3.4.0.12` has supported video related functions, so we can use `pip install opencv-python` as normal. – Khanh Le Jan 16 '18 at 15:10
  • @LittleZero: I still get this error: /io/opencv/modules/highgui/src/window.cpp:331: error: (-215) size.width>0 && size.height>0 in function imshow – Dang Manh Truong Feb 26 '18 at 17:53
  • 2
    for future readers: only this worked for me: `conda install -c menpo opencv3`. the first one does not fix the problem (`conda install --channel menpo opencv`) – Amir Hossein F Jan 22 '19 at 01:04
  • One solution is to use **virtualenv** and create a virtualenv and use `pip install numpy, pip install matplotlib and pip install opencv-contrib-python` , etc to install _after activating the environment_, **instead of using conda**, for me the above solutions didn't work, but using a virtual environment is a great solution. – aspiring1 Apr 17 '19 at 10:59
  • Why do you specify `--channel menpo` when installing `opencv`? I can install it without using that channel. (Surely, I'm not getting another package by using the default channel, am I?) – HelloGoodbye Jul 19 '19 at 12:29
  • @LittleZero 's comment is Gold! Been looking for hours for something that worked! – Ambareesh Aug 17 '19 at 03:12
  • 3
    Today is Jan 24, 2020. This does NOT work. However, the solution suggested by @Yedhrab below does work. – horaceT Jan 24 '20 at 19:22
37

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
divibisan
  • 11,659
  • 11
  • 40
  • 58
lorenzo
  • 481
  • 4
  • 4
13

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

IMPORTANT NOTE MacOS 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)

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

Nic Szerman
  • 1,834
  • 18
  • 25
13

Working method (‍ tested on April 19, 2019)

  • These error happen because of conda.
  • Open Anaconda Prompt and remove conda opencv if installed
  • conda remove opencv

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

  • ⏬ After install opencv via pip (click here to offical info)
  • pip install opencv-contrib-python

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

yEmreAk.com
  • 3,278
  • 2
  • 18
  • 37
  • 3
    I like all the emojis! – Nic Szerman Mar 13 '20 at 22:07
  • I got `Found conflicts! Looking for incompatible packages.` after removing opencv, but let's see if it can fix itself. (to be precise, it showed the message when trying to install again opencv, after uninstalling it) edit: Ok, it worked, thanks ! – Bersan Jun 08 '20 at 10:48
  • 01.01.2021!I am running ubuntu 18.04 on AWS. In my conda-env only numpy and opencv (installed as suggested are installed). It worked (more or less) as I get an different error (at least some progress): This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: xcb. – Druss2k Jan 01 '21 at 00:19
  • 12.04.2021 and this was the only solution for my conda environment with python 3.7. Seems like all the conda channels were not installing opencv correctly, and only pip did that well. – tjiagoM Apr 13 '21 at 14:17
4

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

  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)

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

F1iX
  • 823
  • 6
  • 12
3

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

  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
Community
  • 1
  • 1
Nitin
  • 2,572
  • 5
  • 21
  • 28
2

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

conda remove opencv3

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

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

user3177227
  • 315
  • 3
  • 3
  • $ conda install gtk2 pkg-config Solving environment: failed PackagesNotFoundError: The following packages are not available from current channels: - gtk2 – Mona Jalal Jun 29 '18 at 15:35
2

If you are running inside docker then you may get this error.Solution uninstall current and install the headless one

pip install opencv-python==3.4.5.20

pip install opencv-contrib-python==3.4.5.20

If you are writing to the image and displaying it, you may need the following

apt-get update && apt-get install -y libglib2.0-0 libsm6 libxext6 libxrender1

And if you are wondering how to get the display from Docker, it is possible via X11 in your host

Community
  • 1
  • 1
Alex Punnen
  • 5,287
  • 3
  • 59
  • 71
2

My ubuntu 18.04 machine is running at AWS. What helped me was (link):

pip uninstall opencv-python
pip install opencv-python==4.1.2.30

Afterwards I got the error: " : cannot connect to X server "

Finally, I managed to make it work by installing MobaXTerm (reference here).

FYI:

  1. I connect to AWS with WinSCP. If you connect to AWS via something like WinSCP, the MobaXTerm interface lets you connect to the "WinSCP temporary session" if you click at "Sessions", which is very convenient. The session screen basically replaces the console but allows to display the image via a pop-up window.
  2. In case you also experience the error ASSERT: “false” in file qasciikey.cpp, line 501 once cv.imshow() or similar is executed, go to the MobaXTerm interface, click Settings -> Configuration -> X11, uncheck "Unix-compatible keyboard" (reference here).
Druss2k
  • 275
  • 2
  • 5
  • 15
1

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

1) Remove the opencv package from conda:

>> conda remove opencv

2) To your env.yml file add this:

...
dependencies:
  - numpy
  - pytest
  ...
  - pip:
    - opencv-python
Jodo
  • 4,515
  • 6
  • 38
  • 50
0

Remove opencv from anaconda=

conda remove opencv

Then, reinstall opencv using pip:

pip install opencv

This is working for me.

0

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

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

Then restart the windows

0

Although this is already answered, for me conda-forge solution worked with a hack. My workstation is a centos 6 machine, and I use conda virtual environment (anaconda 2). Create an environment

conda create --name test python=2.7

and then activate it

conda activate test

Now install opencv from conda-forge

conda install -c conda-forge opencv

Now install matplotlib in this environment (and this is hack 1)

conda install matplotlib

Let's check now imshow works or not. In a terminal, activate test environment and start python. In the interpreter, do

import cv2
import matplotlib.pyplot as plt # hack 2
img = cv2.imread('your_image_file',0)
cv2.imshow('image',img)

This should pop up a window showing image. I did not further research how this solved the case.

Note 1: You may see some error related to xkb, then in your .bashrc file add

export QT_XKB_CONFIG_ROOT=/usr/share/X11/xkb

Note 2: You may see some error related to XDG_RUNTIME_DIR, then in your .bashrc file also add

export XDG_RUNTIME_DIR=.tmp/myruntime and define myruntime by mkdir -p .tmp/myruntime

bJust
  • 161
  • 1
  • 5
0

For my system (Ubuntu 18.04) the following was working.
First:

sudo apt-get update -y

sudo apt-get install -y libgtk2.0-dev

conda create -n py35 python=3.5

conda activate py35

Then configure the environment

pip install Cython

pip install scikit-build

conda install -c anaconda cmake

pip install dlib

pip install face_recognition

pip install imutils

And finally:

pip install opencv-contrib-python

Community
  • 1
  • 1
Serg
  • 1
0

Easy with Ubuntu 18.04. It works for me:

Remove opencv-python:

pip3 uninstall opencv-python

And then re-install opencv-python:

pip3 install opencv-python

Issue was resolved.

0

I was able to fix it by installing a previous version of opencv-contrib-python.

I'm using Ubuntu 18

Jamal Alkelani
  • 606
  • 7
  • 19