Cross compiling OpenCV 4 for Raspberry Pi Zero
Posted on August 7, 2019 by Paul
In this article I will show you how to cross compile the latest version of OpenCV for Raspberry Pi Zero. If you want to build OpenCV for Raspberry Pi 2, 3 or 4 please check this article for a much faster procedure.
Unfortunately the crossbuild-essential-armhf from Debian will not generate working binaries for Raspberry Pi Zero, so we can’t use the cross compilation procedure I’ve presented in my previous article about cross compiling OpenCV for Raspberry 2 and up.
In order to cross compile OpenCV, we are going to use a slightly more involved procedure, in which we’ll basically emulate an ARM processor on a more powerful x86-64 Debian. The approach presented in this article will work with all versions of Raspberry Pi, but it is much slower than the approach presented in my previous article because of the emulation part. Please note that, even with an emulated ARM architecture, cross compiling OpenCV is much faster than building directly on a Raspberry Pi Zero where it could take days, assuming that the build doesn’t fail because of the limited amount of RAM on the Pi Zero.
As a side note, I’ve cross compiled OpenCV for Raspberry Pi Zero on a Debian 10 x86-64 machine with 8GB of RAM. In theory, the same approach should work with a Debian virtual machine or with a Docker container, just be sure that you have plenty of RAM.
First, make sure your Debian Buster system is updated:
Next, we are going to install Raspbian, in a chroot, on the host Debian x86-64 system:
At this point you should be running on Raspbian on an emulated ARM system.
If you need to go back to your host Debian system, write exit. If you want to restart the emulated Raspbian system use:
If, at a later time, you decide that you want to remove the emulated Raspbian system, simply erase the raspbian folder from your host system, e.g.:
For the next part of the article, I’ll assume that you are on the emulated Raspbian system.
Start by installing the build tools necessary for compiling OpenCV:
Since Raspberry Pi comes with both Python 2 and Python 3 installed, we are going to build OpenCV with support for both versions of Python.
Next, we are going to install two graphical libraries required if you want to run OpenCV with GUI support:
Let’s also install a couple of image and video libraries that are used by OpenCV:
Next, we can download the current release of OpenCV. I will show you how to install the full OpenCV (default and contrib libraries):
Create a build folder:
At this point, we can use Cmake to generate the OpenCV build scripts:
If you want to build OpenCV without GUI support, a headless build, use the next command:
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:
A final step:
Next, compress the library in order to deploy it on a real Raspberry Pi Zero device:
You should end up with the library archived inside the emulated Raspbian system, e.g.:
If you exit from the emulated Raspbian, the archive should be in:
Copy the archive opencv-4.1.0-pizero.tar.bz2 to your Pi Zero device.
For the next part of the article, I’ll assume that you are on a Raspberry Pi Zero.
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:
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:
Next, install git if necessary:
We’ll clone a simple config file useful if you want to be able to use OpenCV from C++:
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:
If you want to learn more about programming on the Raspberry Pi, a very good book is Exploring Raspberry Pi by Derek Molloy: