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:
- https://www.pyimagesearch.com/2018/09/26/install-opencv-4-on-your-raspberry-pi/
- 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
2 Answers 2
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.
-
2Thank 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– Paul2019年07月04日 14:00:33 +00:00Commented Jul 4, 2019 at 14:00
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?
apt install python-opencv
? It is not version 4?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