How can i make OpenCV work fully on Raspberry Pi 4 (Raspbian Buster)?

Tags: , ,



I have tried many days to install OpenCV on my Raspberry Pi 4 with Raspbian Buster but i couldn’t get it done. The installation in most cases worked but when importing or using cv2 methods like cv2.imshow(), errors come up (see below).

Did somebody get OpenCV working on a Raspberry Pi 4 or has an idea on how to get it working? Thank you in advance for your help :-)!


Error after installing OpenCV with pip:

pip install opencv-python
pip install opencv-contrib-python

python
>>> import cv2

ImportError: libImath-2_2.so.12: cannot open shared object file: no such file or directory

I could not get this library “libImath-2_2.so.12” installed. The error persisted.


Error after installing OpenCV with conda:

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

python
>>> import cv2
>>> img = cv2.imread("image.png", 0)
>>> cv2.imshow("Frame", img)

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 /home/jhelmus/conda/conda-bld/opencv_1512174356192/work/modules/highgui/src/window.cpp, line 605
Traceback (most recent call last):
  File "detect_video.py", line 113, in <module>
    cv2.imshow("Frame", img_main)
cv2.error: /home/jhelmus/conda/conda-bld/opencv_1512174356192/work/modules/highgui/src/window.cpp:605: 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 checked and the libraries libgtk2.0-dev and pkg-config were installed already. I don’t know how to “configure the script in function cvShowImage” in a way that would make it work.


Error after installing OpenCV by compiling it

I went through the compilation processes described in these two guides. These guides worked on my Raspberry Pi 3b, but not on my Raspberry Pi 4:

  1. https://www.pyimagesearch.com/2018/09/26/install-opencv-4-on-your-raspberry-pi/
  2. https://www.learnopencv.com/install-opencv-4-on-raspberry-pi/

I was able to finish the compilation with make, sudo make install etc. Afterwards though i now still have to link the openCV installation to my Python bindings. With the cmake command i set the following paths:

  • Python 3 Interpreter: /home/pi/miniconda3/bin/python3 (ver 3.5.6)
  • Python 3 Packages: lib/python3.5/dist-packages
  • Python (for build): /usr/bin/python2.7

How can I now access OpenCV, how can I link it to Python so that i can import it as cv2?

Thank you in advance for your help :-)!

Paul

Answer

With the help of @Ingo I was finally able to install OpenCV on Raspbian Buster with a solution so much more simple than expected:

Simply run sudo apt install python3-opencv and it will work and also create windows with cv2.imshow().



Source: stackoverflow