Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error using cv2.imshow (Unspecified error)

I'm following this tutorial to make corner detection and I have to use cv2.imshow. Here is my code:

import cv2
import numpy as np

filename = '1.jpg'
img = cv2.imread(filename)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

gray = np.float32(gray)
dst = cv2.cornerHarris(gray,2,3,0.04)

#result is dilated for marking the corners, not important
dst = cv2.dilate(dst,None)

# Threshold for an optimal value, it may vary depending on the image.
img[dst>0.01*dst.max()]=[0,0,255]

cv2.imshow('dst',img)

I got 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 /root/mc-x86-2.7/conda-bld/opencv-3_1482254836916/work/opencv-3.1.0/modules/highgui/src/window.cpp, line 545
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: /root/mc-x86-2.7/conda-bld/opencv-3_1482254836916/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

So, I installed libgtk2.0-dev and pkg-config but it didn't solve the problem. The error message said to run cmake but where? There is no CMakeLists.txt file in my drive.

Then, I follow some answers on this website like this one: first, I downloaded OpenCV directly on the website and I run cmake, make and make install. All is okay but I still have the same error when I use Anaconda but it got another message when I open Python from /usr/bin/python:

init done 
opengl support available 

For the moment, I can't show my image. What I have to do?

like image 382
Pierre Avatar asked Oct 29 '22 14:10

Pierre


1 Answers

In my question, I had two problems:

  1. From /usr/lib/python, I was unable to show the image but I didn't have an error;

  2. With the Anaconda Framework, the function imshow wasn't implemented.

For the first problem, it was really easy because I didn't read the documentation correctly. It says, about the cv2.waitKey() function:

Besides binding keyboard events this function also processes many other GUI events, so you MUST use it to actually display the image.

So, I just have to call cv2.waitKey(1) after cv.imshow() to show the image.

For the second problem,

  • I used a manual method to solve it (I'm not sure it's the best but it works). I replaced all the libopencv* files in the /home/user/anaconda3/lib folder by the libopencv* files in the /usr/local/lib/ folder.

  • After that, I had to update the `/home/user/anaconda3/lib/libstdc++.so' file with the '/usr/lib/i386-linux-gnu/libstdc++.so' file.


There is also a cleaner alternative but you should restart the installation process to do it. When you run the cmake command, the parameter -D CMAKE_INSTALL_PREFIX have to refer to your anaconda folder (for me, it's /home/pierre/anaconda3/). After that, you just have to continue the installation as usual:

make
sudo make install

Now, you can use OpenCV with Anacaonda (but only with Anaconda; it doesn't work if you load /usr/bin/python).

like image 170
Pierre Avatar answered Nov 15 '22 07:11

Pierre