Cross compiling OpenCV 4 for Raspberry Pi and BeagleBone Black
Posted on December 18, 2018 by Paul
Updated 7 August 2019
If you want to cross compile OpenCV for Raspberry Pi Zero please check this article.
In this article, I will show you how to cross compile the latest version of OpenCV for Raspberry Pi and BeagleBone Black. At the time of this writing, OpenCV is at version 4.1.0 while the version available with the default Debian based OS on Raspberry Pi and BeagleBone Black is 3.2.0. Building OpenCV 4 directly on a Raspberry Pi or BeagleBone Black is doable, but it could take a few hours. Cross compiling OpenCV for armhf is a 20 - 30 minutes process, depending on the speed of your computer it could be even shorter.
I recommend that you do the build in a Debian Buster virtual machine or a Docker container in order to not mess your system. If you decide to install Buster in a virtual machine, make sure to use the minimal netinst system. It is really important that you start with a bare bone system, because we need to install armhf executables and libraries. By using a minimal system we avoid potential conflicts with the native x86-64 versions.
For the remaining of this article, I will mention only Raspbian and Raspberry Pi, if you are using a BeagleBone Black everything should work the same.
I’ll assume that you have a clean Raspbian install. I’ve used the latest available desktop image. In principle you should be able to follow the article using Raspbian Lite, but I did all my tests using the full desktop version of Rasbian.
First, make sure your x86-64 Debian Stretch virtual machine or container is updated:
Next, let’s enable the armhf architecture on the x86-64 machine:
At this point, you should be able to install armhf libraries and applications on your system and run them.
We are going to build OpenCV with support for Python and C++. Let’s install NumPy and Python 2, 3 for the host system:
We’ll also need libpython for the armhf architecture:
Next, we are going to install libgtk-3 in order to be able to write simple GUI programs. If you plan to use OpenCV exclusively on a headless system you can safely ignore the next two libraries:
We also need to install a bunch of other libraries required by OpenCV (various image and video formats support):
Next, we are going to install the default cross compilers from Debian which can be used to create armhf binaries for Raspberry Pi:
At the time of this writing, the above toolchain is based on GCC 8.3, which is the same version of GCC available on Raspbian.
Finally, we’ll install Cmake, git, pkg-config and wget:
Next, we can download the current release of OpenCV. I will show you how to install the full OpenCV (default and contrib libraries):
We need to temporarily modify two system variables required to successfully build GTK+ support:
At this point, we can use Cmake to generate the OpenCV build scripts:
Assuming that you had no errors, you should have a Makefile in the build folder. We can start the actual build:
Once the build phase is done, we can install the library:
Next, we need to change the name of a library that the installer mistakenly labeled as a x86_64 library when in fact it is an armhf one:
Let’s compress the installation folder and save the archive to the home folder:
To make our life easier, I’ve also prepared a simple pkg-config settings file, named opencv.pc. Get it with:
Copy opencv-4.1.0-armhf.tar.bz2 and opencv.pc from your home folder to your Raspberry Pi.
For the next part of the article, I’ll assume that you are on a RPi.
Make sure your RPi has all the development libraries we’ve used. Like before, if you don’t plan to use GTK+, ignore the first line from the next commands. Most of these libraries should be already installed if you are using the full version of Raspbian:
Uncompress and move the library to the /opt folder of your RPi:
Optionally, you can erase the archive:
Next, let’s also move opencv.pc where pkg-config can find it:
In order for the OS to find the OpenCV libraries we need to add them to the library path:
Log out and log in or restart the Terminal.
Next, let’s create some symbolic links that will allow Python to load the newly created libraries:
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:
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:
or, if you have a display connected to your RPi:
Here is a screenshoot of the C++ GUI test running on my Pi:
For the Python tests, use:
As a side note, it is also possible to cross compile C++ programs that use OpenCV on your x86-64 Debian Stretch system and run the binary on your RPi.
If you want to learn more about programming on the Raspberry Pi, a very good book is Exploring Raspberry Pi by Derek Molloy: