Solarian Programmer

My programming ramblings

Install OpenCV 4 on Raspberry Pi for C++ and Python development

Posted on September 17, 2019 by Paul

In this article, I will show you how to install OpenCV 4 with Python and C++ support on Raspberry Pi. I assume that you have the latest Raspbian installed on your Raspberry Pi, which at the time of this writing is based on Debian 10 Buster.

Unfortunately, there is no official binary of OpenCV 4 for Raspberry Pi, so I had to built OpenCV with Python 2 and 3 support for Raspberry Pi Zero and up from sources. If you want to do the build yourself check my previous articles:

In this article I’ll will show you how to install the already built binaries.

There is also a video version of this tutorial:

Let’s start by downloading the already built OpenCV 4 from https://github.com/sol-prog/raspberry-pi-opencv/releases/. As you can see, there are four binaries: two for RPi Zero with and without GUI support and two for RPi 2 and up with and without GUI support. Pick the proper version for your RPi.

In the remaining of this article, I will assume that you want to install the full GUI version on a RPi 2 and up. If you are using RPi Zero just change the name of the files accordingly.

Open a Terminal and download the desired version of OpenCV:

1 wget https://github.com/sol-prog/raspberry-pi-opencv/releases/download/opencv4rpi2.1/opencv-4.1.0-armhf.tar.bz2

Next, extract the archive:

1 tar xvf opencv-4.1.0-armhf.tar.bz2

Once the archive was extracted, move the resulting opencv-4.1.0 folder to /opt:

1 sudo mv opencv-4.1.0 /opt

Optionally, you can remove the archive:

1 rm opencv-4.1.0-armhf.tar.bz2

Next, we are going to install a couple of libraries required by OpenCV:

1 sudo apt install libtiff-dev zlib1g-dev
2 sudo apt install libjpeg-dev libpng-dev
3 sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
4 sudo apt-get install libxvidcore-dev libx264-dev
5 sudo apt install python-numpy python3-numpy

The next two libraries are only required if you are using the GUI version of OpenCV, you can safely ignore these if you are using the headless version:

1 sudo apt install libgtk-3-dev libcanberra-gtk3-dev

Next, we’ll add OpenCV to the system library path, you’ll need to run these commands from your home folder:

1 cd ~
2 echo 'export LD_LIBRARY_PATH=/opt/opencv-4.1.0/lib:$LD_LIBRARY_PATH' >> .bashrc
3 . .bashrc

Restart your Terminal or log in and log out if you are connected to your RPi through SSH.

Next, let’s create some symbolic links that will allow Python to load the newly created libraries:

1 sudo ln -s /opt/opencv-4.1.0/lib/python2.7/dist-packages/cv2 /usr/lib/python2.7/dist-packages/cv2
2 sudo ln -s /opt/opencv-4.1.0/lib/python3.7/dist-packages/cv2 /usr/lib/python3/dist-packages/cv2

Install git if necessary:

1 sudo apt install git

We’ll clone a simple config file useful if you want to be able to use OpenCV from C++:

1 git clone https://gist.github.com/sol-prog/ed383474872958081985de733eaf352d opencv_cpp_compile_settings
2 cd opencv_cpp_compile_settings/
3 sudo cp opencv.pc /usr/lib/arm-linux-gnueabihf/pkgconfig
4 cd ~
5 rm -rf opencv_cpp_compile_settings/

At this point, you should be able to use the OpenCV library from C++ or Python.

On the repository for this article you can find a few C++ and Python test programs. You can download the code on your Pi with:

1 git clone https://github.com/sol-prog/raspberry-pi-opencv.git
2 cd raspberry-pi-opencv/tests

There are two headless tests that you can use even if you don’t have a display connected to your RPi: cli_cpp_test.cpp and cli_python_test.py. I’ve also included two graphical tests that require a display: gui_cpp_test.cpp and gui_python_test.py.

You can build and run the C++ tests like this:

1 g++ cli_cpp_test.cpp -o cli_cpp_test `pkg-config --cflags --libs opencv`
2 ./cli_cpp_test

or, if you have a display connected to your RPi:

1 g++ gui_cpp_test.cpp -o gui_cpp_test `pkg-config --cflags --libs opencv`
2 ./gui_cpp_test

Here is a screenshoot of the C++ GUI test running on my Pi:

Raspberri Pi C++ OpenCV 4 test

For the Python tests, use:

1 python3 cli_python_test.py

or

1 python3 gui_python_test.py

If you want to learn more about programming on the Raspberry Pi, a very good book is Exploring Raspberry Pi by Derek Molloy:


Show Comments