简体   繁体   中英

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

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

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().

first have a fresh new raspbian

sudo apt-get update 
sudo apt-get upgrade
sudo apt-get install libatlas-base-dev
sudo apt-get install libjasper-dev
sudo apt-get install libqtgui4 
sudo apt-get install libqt4-test
sudo apt-get install libhdf5-dev
sudo pip3 install flask
sudo pip3 install numpy
sudo pip3 install opencv-contrib-python
sudo pip3 install imutils
sudo pip3 install opencv-python

You have to run make after your cmake . In the next step you run make install when your make finish without errors.

Now you can work with OpenCV in Python (assuming that your configuration is valid). Maybe you have to rebuild opencv, because you use Python2.7 for build. Set the PYTHON_DEFAULT_EXECUTABLE to Python3 to use OpenCV with Python3: -DPYTHON_DEFAULT_EXECUTABLE=$(which python3)

For opencv 4.3.0 raspberry pi 4 raspbian buster tbb neon

Installation steps

wget https://github.com/cyysky/OpenCV-Raspberry-Pi-4-Package-for-Python/raw/master/opencv_4.3.0-1_armhf.deb

sudo dpkg -i opencv_4.3.0-1_armhf.deb # This will install fail for dependency

sudo apt-get -f install # Auto install dependency package

sudo dpkg -i opencv_4.3.0-1_armhf.deb # Now start install

sudo apt-get install tesseract-ocr # Optional : tesseract-ocr

And if you have import issues :

export LD_PRELOAD=$LD_PRELOAD:/usr/lib/arm-linux-gnueabihf/libatomic.so.1.2.0

A slight shorter (list/dict comprehension) version of posted answer by user acine sachiro.

import os

dct = {'apt-get'         : ['update', 'upgrade', 'other commands', 'etc.'],
       'apt-get install' : ['libatlas-base-dev','libjasper-dev', 'libqtgui4', 'libhdf5-dev'],
       'pip3 instal'     : ['flask', 'numpy', 'opencv-contrib-python', 'imutils', 'opencv-python']}

# The below code shows how nested list comprehension works

for k,v in dct.items():
    for i in v:
        print (k,i)

# which can be re-written to:

[print (k,i) for k,v in dct.items() for i in v]

The actual working code to execute the commands is:

import os

dct = {'apt-get'         : ['update', 'upgrade', 'other commands', 'etc.'],
       'apt-get install' : ['libatlas-base-dev','libjasper-dev', 'libqtgui4', 'libhdf5-dev'],
       'pip3 instal'     : ['flask', 'numpy', 'opencv-contrib-python', 'imutils', 'opencv-python']}

[os.system(f"sudo {k} {i}") for k,v in dct.items() for i in v]

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