70

I'm trying to get OpenCV working with Python on my Ubuntu machine. I've downloaded and installed OpenCV, but when I attempt to run the following python code (which should capture images from a webcam and push them to the screen)

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
capture = cv.CaptureFromCAM(0)

def repeat():
    frame = cv.QueryFrame(capture)
    cv.ShowImage("w1", frame)
    time.sleep(10)

while True:
    repeat()

I get the following 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

So I do what they ask: install the packages, move to the folder from whence I installed OpenCV, and run

sudo make uninstall
make
sudo make install

But when I try to run the python, it gives me the same error. Am I missing something?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Dan
  • 2,952
  • 4
  • 23
  • 29
  • have you tried the newer `cv2` module? Also, both opencv and the python bindings are in the repositories, there's no need to download or compile anything – loopbackbee Feb 01 '13 at 22:42

8 Answers8

81

If it's giving you errors with gtk, try qt.

sudo apt-get install libqt4-dev
cmake -D WITH_QT=ON ..
make
sudo make install

If this doesn't work, there's an easy way out.

sudo apt-get install libopencv-*

This will download all the required dependencies(although it seems that you have all the required libraries installed, but still you could try it once). This will probably install OpenCV 2.3.1 (Ubuntu 12.04). But since you have OpenCV 2.4.3 in /usr/local/lib include this path in /etc/ld.so.conf and do ldconfig. So now whenever you use OpenCV, you'd use the latest version. This is not the best way to do it but if you're still having problems with qt or gtk, try this once. This should work.

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 (e.g., at the line of cv2.namedWindow(name) with error: cv2.error: OpenCV(3.4.2). The function is not implemented.). 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
tomahawk
  • 3
  • 2
Froyo
  • 17,947
  • 8
  • 45
  • 73
43

Don't waste your time trying to resolve this issue, this was made clear by the makers themselves. Instead of cv2.imshow() use this:

img = cv2.imread('path_to_image')
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
plt.show()
Community
  • 1
  • 1
purna15111
  • 704
  • 7
  • 9
  • thank you, your solution makes the more sense here, why are people opting for hacks? – asosnovsky Jul 20 '18 at 13:22
  • 3
    What about interaction? You can't use `cv.waitKey()` either. – Tengerye Oct 23 '18 at 03:47
  • #cv2.imshow('Video', frame) img = cv2.imread('Video',frame) `error:TypeError: only size-1 arrays can be converted to Python scalars` – Gank Jan 26 '19 at 12:48
  • How to change from `cv2.imshow('Video', frame) ` to your code?Thanks! – Gank Jan 27 '19 at 00:49
  • 1
    @Gank I guess you'd have figured out by now. For those in hurry: # cv2.imshow('Faces', frame) img = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 200, 0), 2) plt.imshow(img, cmap='gray') plt.show() – emeralddove Aug 30 '19 at 11:08
  • ah plt is matplotlib.pyplot! Was wondering. Will leave comment for others. – John Curry Sep 12 '20 at 18:04
  • terrible advice. OpenCV is perfectly capable of showing an image in a bare window. you just have to make sure OpenCV was built with GUI facilities **and** you didn't install multiple OpenCV packages. – Christoph Rackwitz Jan 17 '23 at 21:36
38

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
15

It's because of 'opencv-python-headless'. Uninstall it!

pip uninstall opencv-python-headless
  • 2
    I am on Windows and only this method worked for me. Also take a look on Github: https://github.com/opencv/opencv-python/issues/18#issuecomment-752701731 – Daniel Chin Jan 22 '22 at 19:48
  • 1
    Excuting `pip uninstall opencv-python; pip install opencv-python` after uninstalling `opencv-python-headless` worked for me. As this reply said: https://github.com/opencv/opencv-python/issues/18#issuecomment-929313428 – Even Wonder Mar 30 '23 at 15:46
8

I hope this answer is still useful, despite problem seems to be quite old.

If you have Anaconda installed, and your OpenCV does not support GTK+ (as in this case), you can simply type

conda install -c menpo opencv=2.4.11

It will install suitable OpenCV version that does not produce a mentioned error. Besides, it will reinstall previously installed OpenCV if there was one as a part of Anaconda.

Artem S
  • 310
  • 3
  • 5
  • 2
    This doesn't work for me with python 3.5.2. Neither does the install libopencv-* recommended by others. I made a 2.7 virtual environment, and this works within that environment. Thanks! But I wish everything worked with python3... we were all supposed to upgrade years ago lol –  Jan 29 '17 at 17:52
  • 3
    @JulianCienfuegos, I have conda 4.3.11 with python 3.5.2 in one of its virtual environments, and it installs OpenCV 3.1.0 by typing `conda install opencv`. If your conda is older, it is possible that conda update will fix it. – Artem S Feb 26 '17 at 16:44
  • 1
    outdated answer. OpenCV has **official packages on PyPI** (different flavors, all come with main packages). nobody should bother with anything from conda, except maybe the (unofficial!) packages from `conda-forge` – Christoph Rackwitz Jan 17 '23 at 21:38
1

This is due to some kind of overlap in opencv-python against opencv-contrib-python, opencv-python-headless packages. Simply uninstall both headless & contrib.

pip uninstall opencv-contrib-python

pip uninstall opencv-python-headless

Then uninstall opencv-python.

pip uninstall opencv-python

it's a clean slate here. Then install only OpenCV-python which will clear you out of this issue.

pip install opencv-python
0

Before installing libgtk2.0-dev and pkg-config or libqt4-dev. Make sure that you have uninstalled opencv. You can confirm this by running import cv2 on your python shell. If it fails, then install the needed packages and re-run cmake .

user618677
  • 4,909
  • 6
  • 23
  • 24
0

This strange problem started to happen to me as well, in one virtual environment out of several that I'm using. What solved it is to uninstall opencv and then reinstall it:

pip uninstall opencv-python
pip install opencv-python
TheTomer
  • 143
  • 3
  • 11