3

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 always works 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: nu such file or directory

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

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/

After the process from the make command reached almost the end, it could not link the openCV installation to the Python executable (neither the Python executable in my conda virtual environment /home/pi/miniconda3/envs/py35/lib/python3.5, nor the global Python3 executable /usr/bin/python3. The errors were somehow like "Permission denied: Could not access Python3.5". I used this cmake setting:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
    -D BUILD_opencv_python3=yes \
    -D PYTHON_DEFAULT_EXECUTABLE=/home/pi/miniconda3/envs/py35/lib/python3.5 \
    -D OPENCV_PYTHON3_INSTALL_PATH=/home/pi/miniconda3/envs/py35/lib/python3.5/site-packages \
    -D ENABLE_NEON=ON \
    -D ENABLE_VFPV3=ON \
    -D BUILD_TESTS=OFF \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D INSTALL_PYTHON_EXAMPLES=OFF \
    -D BUILD_EXAMPLES=OFF ..

I once tried this cmake command with the PYTHON_DEFAULT_EXECUTABLE = /usr/bin/python3.

Thank you in advance for your help :-)!

Paul

4
  • Welcome. What's wrong with apt install python-opencv? It is not version 4?
    – goldilocks
    Jul 3, 2019 at 12:49
  • Hi @goldilocks ,thanks for welcoming and your suggestion. I tried it and suddenly i could run cv2.imshow() in python2 but not in python3, do you know why? I also read that the python-opencv repository is older, outdated, and doesn’t include the opencv_contrib package and therefore people do not recommend installing OpenCV via apt-get because one will be missing features and be running an older, outdated version.
    – Paul
    Jul 3, 2019 at 14:23
  • I was now able to link the OpenCV installation to my Python environments but when I import cv2, it leads to the error: ImportError: libopencv_hdf.so.3.3: cannot open shared object file: No such file or directory
    – Paul
    Jul 3, 2019 at 15:14
  • I would favour the distro package unless there is a specific reason beyond "I want to use the latest one", i.e., "I need to use version X.x because that version includes something I cannot do without". When I looked though the package search (apt search opencv | grep python) I'm pretty sure I saw python 3 explicitly , but you may want to have a closer look through that. I think the OCV version is 3.2.x
    – goldilocks
    Jul 3, 2019 at 20:07

2 Answers 2

5

You should use OpenCV from the default Raspbian Repository. It is tested and fits best into the Raspbian distribution without errors (I hope :). On Buster you will find with:

rpi ~$ apt list python*opencv*
Listing... Done
python-opencv-apps/testing 1.12.0-2 all
python-opencv/testing 3.2.0+dfsg-6 armhf
python3-opencv-apps/testing 1.12.0-2 all
python3-opencv/testing 3.2.0+dfsg-6 armhf
rpi ~$

Just sudo apt install python3-opencv and it should do. With apt show python3-opencv you will find that Version: 3.2.0+dfsg-6 is installed. If you like you can also install the additional apps.

1
  • 2
    Thank you so much!!! It now works finally after almost one week of trying.. Because of my little reputation on StackExchange i cannot upvote your answer but i marked it as the right solution!
    – Paul
    Jul 4, 2019 at 14:00
0

Just found this after posting an OpenCV issue with Raspbian Buster. The op followed the same guide as I did (although I am using a Pi3) and installed python-opencv-contrib not the basic opencv package?

I have also installed python-opencv-contrib, but using the PIP guide and am using it in a virtual environment. OpenCV is 4.5.1, installing using apt will only give me version 3.2.0?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.