简体   繁体   中英

Python Opencv imshow error

my code:

import cv2
img = cv2.imread("dU4fHDw-Ho.jpg", cv2.IMREAD_GRAYSCALE)

cv2.imshow('image', img)
k = cv2.waitKey(0)
if k == 27:
    cv2.destroyWindow()

And i get this error:

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 /io/opencv/modules/highgui/src/window.cpp, line 583
Traceback (most recent call last):
  File "simpleopencv.py", line 4, in <module>
    cv2.imshow('image', img)
cv2.error: /io/opencv/modules/highgui/src/window.cpp:583: 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

I'm using linux mint KDE What might be the problem?

Update1: Opencv works fine with python2.7 but not working for python3.5

Update2: As it turns out running the script as Jack Gold suggested actually worked but since i already had cv2 folder in my /usr/local/lib/python3.5/dist-packages/ (which was installed using PIP i think) conflicted with the one install using the script and python3 kept using the PIP version so, I deleted that cv2 folder and restarted and everything worked fine.

Nothing to worry buddy,I suffered the same and this is how I got rid of this error, see if you have installed opencv via pip or pip3 or apt-get, first you gotta uninstall it

sudo pip3 uninstall (what you wrote while installing)

or sudo pip uninstall (what you wrote while installing)

or sudo apt-get remove (what you wrote while installing)

and afterwards build the opencv package from source code from github.

git clone https://github.com/opencv/opencv.git

cd ~/opencv

mkdir release

cd release

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

make

sudo make install

This will solve the problem. All the best.

Not sure about the problem since it works fine for me. But just as an alternative, use plt.imshow() from Matplotlib, it also works fine.

Did you install OpenCV using pip? If you did, note that as mentioned here , this is the unofficial package for OpenCV and does not have support for GUI windows ( cv2.imshow() , etc)

I would recommend using Anaconda to install opencv using conda install -c conda-forge opencv=3.2.0 or conda install -c menpo opencv3=3.2.0

Easier solution is provided by conda

conda install -c loopbio -c conda-forge -c pkgw-forge ffmpeg-feature ffmpeg gtk2 opencv

You may see the following error after conda install

Traceback (most recent call last):
    File "./test.py", line 21, in <module>
      import cv2
ImportError: libopenblas.so.0: cannot open shared object file: No such file or directory

Just install openblas library would solve

yum install openblas-devel -y

Reference

[sdvillal | github] https://github.com/conda-forge/opencv-feedstock/issues/43

You should use absolute path to read your image.

For me on Mac OS:

img = cv2.imread('/Users/nicholasleung/Documents/after effect/AL.png')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM