Install OpenCV 4.5 on Raspberry 64 OS
Last updated: July 29, 2021
The Raspberry Pi is moving towards a 64-bit operating system. Within a year or so, the 32-bit OS will be fully replaced by the faster 64-bit version.
The Raspberry Foundation has recently released a more than functional beta version. Installation instructions can be found here. This guide will install OpenCV 4.5.x on a Raspberry Pi 4 with a 64-bit operating system.
You can find the release notes of version 4.5.x on the GitHub page of OpenCV here.
Looking for a smaller OpenCV footprint? Our OpenCV Lite installation might be just what you needed. Only the very basics installed, saving you a lot of memory.
On December 22, 2020, OpenCV released version 4.5.1. The most important improvement in this version is the work on the G-API framework and the RISC-V port. Neither will affect the Raspberry Pi. That's why we do not have a special installation guide for this version. Feel free to change the version numbers from 4.5.0 to 4.5.1 during downloading, if you prefer to use this version over 4.5.0.
On April 2, 2021, OpenCV released version 4.5.2. Find the significant changes in version in the log file here. Important changes are made in the imgproc, videoio and DNN module. Feel free to change the version numbers from 4.5.0 to 4.5.2 during downloading.
On July 5, 2021, OpenCV released version 4.5.3. Besides various bug fixes and improvements, the most significant changes are the DaSiamRPN tracker, a GPU FFmpeg backend for videoio and MatMul support in the CUDA backend. More info here.
Before installing OpenCV 4.5 on your Raspberry 64-bit OS, you should first check your version. Run the command uname -a and verify your version with the screen dump below.
You also need to check your C++ compiler version with the command gcc -v. It must also be an aarch64-linux-gnu version, as shown in the screenshot. If you have a 64-bit operating system, but your gcc version is different from the one given above, reinstall the whole operating system with the latest version. The guide is found here: Install 64 bit OS on Raspberry Pi 4. You must have a 64-bit C ++ compiler as we are going to build all libraries from scratch. Otherwise, there is no point in building a 32-bit version on a 64 machine.
The next check is the size of the memory swap. It must be large enough to support the building.
OpenCV version 4.5.2 requires more memory than its predecessors. If you have a Raspberry Pi 4 with 4 or 8 GB of memory, you don't have the change anything. Only when you have a Raspberry Pi 4 with 2 GB of RAM, you need to increase the swap space in the zram configuration file. Please follow the instruction as you already know from the installation of your 64 bit OS.
$ sudo nano /usr/bin/zram.sh
# alter the limit with * 2
mem=$(( ($totalmem / $cores)* 1024 * 3))
# save with <Ctrl+X>, <Y> and <Enter>
$ sudo reboot
By increasing the ratio to a factor of three, you get 1.8 GB of extra swap space. This is sufficient for building OpenCV 4.5.2.
Version 4.5.0 - 4.5.1.
OpenCV version 4.5.0 or 4.5.1 requires at least a total of 5.5 GB of memory. If you have installed the 64-bit OS according to our guide, this should be no problem.
If you have a Raspberry Pi 4 without zram.sh installed, you have probably the originally installed dphys-swapfile in place. In that case, you have to increase the default 100 MByte swap space. With a Raspberry Pi with 4 GByte RAM onboard, you have to increase to the maximum 2048 GByte. However, with a Raspberry Pi with only 2 or 1 GByte of RAM, you run into troubles.
To exceed the 2048 MByte limit normally found in the /etc/dphys-swapfile, you must first increase this maximum in the /sbin/dphys-swapfile. Everything is demonstrated in the slideshow below.
# activate the modifications
$ sudo reboot
The last check is the EEPROM software version. The Raspberry Pi 3 had all the operating software on the SD card. The Raspberry Pi 4, on the other hand, is also partially booted from two EEPROMs. These EEPROMs are programmed after PCB assembly in the factory. The Raspberry foundation has recently released new and improved software for these EEPROMs. This nothing to do with OpenCV, but all the more with heat dissipation. In one of our vision applications, the heat of the CPU drops from 65 °C (149 °F) to 48 °C (118 °F) simply by updating the EEPROMs contents. And, as you know, a low CPU temperature will prolong your Pi lifespan. For more information see this article.
Check, and if needed update, the EEPROMs with the following commands. The screen dumps speak for there self.
# to get the current status
$ sudo rpi-eeprom-update
# if needed, to update the firmware
$ sudo rpi-eeprom-update -a
$ sudo reboot
Some words of warning. Do not use pip3 to install OpenCV on your Raspberry Pi. Nor use sudo apt-get install. All versions are not up to date and certainly not 64 bit. The only proper way to install OpenCV 4.4.0 is by building it from source.
As explained here, the physical RAM chip is used both by the CPU and the GPU. Check and change the amount to at least 128 Mbyte if necessary, using the following menu.
Installing OpenCV on your Raspberry Pi is not that complicated. With its 60 command lines, it is more of an administrative task. That's why we created an installation script that executes all commands in this guide at once. Use it if you want, it shouldn't cause any problems. The whole installation will take one and a half hour to complete. It starts with the installation of the dependencies and ends with the ldconfig.
If you want to beautify your OpenCV with the Qt5 GUI, follow the instructions on the GitHub page, or in the section below.
Cleaning and removing dphys-swap should now be done manually according to these instructions.
Not using the script? Here's the full guide.
The OpenCV software uses other third party software libraries. These must be installed first. Some come with the Raspberry 64-bit operating system, others may already be installed. Better to be safe than sorry so here's the full list. Only the latest packages are installed according to the procedure.
# check for updates (64-bit OS is still under development!)
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install build-essential cmake git unzip pkg-config
$ sudo apt-get install libjpeg-dev libpng-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install libgtk2.0-dev libcanberra-gtk* libgtk-3-dev
$ sudo apt-get install libxvidcore-dev libx264-dev
$ sudo apt-get install python3-dev python3-numpy python3-pip
$ sudo apt-get install libtbb2 libtbb-dev libdc1394-22-dev
$ sudo apt-get install libv4l-dev v4l-utils
$ sudo apt-get install libopenblas-dev libatlas-base-dev libblas-dev
$ sudo apt-get install liblapack-dev gfortran libhdf5-dev
$ sudo apt-get install libprotobuf-dev libgoogle-glog-dev libgflags-dev
$ sudo apt-get install protobuf-compiler
Qt is an open-source toolkit for the development of cross-platform graphical user interfaces. It also works on the Raspberry Pi. The software can be used to beautify OpenCV windows and other user interfaces, such as sliders and checkboxes. It is absolutely not mandatory for the working of OpenCV, only to embellish the look. Must be said, that using Qt5 will slow down your OpenCV by a few percent. If you go for the fastest possible apps, don't use it.
If you want the Qt5 support enabled in OpenCV, you have to download the library as shown in the command below. The next step is to set the -D WITH_QT flag during the build.
# only install if you want Qt5
# to beautify your OpenCV GUI
$ sudo apt-get install qt5-default
When all third-party software is installed, OpenCV itself can be downloaded. There are two packages needed; the basic version and the additional contributions. After downloading, you can unzip the files. Please be aware of line wrapping in the text boxes. The two commands are starting with wget and ending with zip.
Now it is time to decide whether or not to use a virtual environment for your OpenCV installation. We don't use the virtual environment. Instead, we simply swap the SD-card if needed. However, feel free to install a virtual environment. All instructions are given in the 32-bit OpenCV guide.
Importing both TensorFlow and OpenCV into Python can throw the error: cannot allocate memory in static TLS block.
This behaviour only occurs on an aarch64 system and is caused by the OpenMP memory requirements not being met. For more information, see GitHub ticket #14884.
There are a few solutions. The easiest is to import OpenCV at the beginning, as shown above.
The other is disabling OpenMP by setting the -DBUILD_OPENMP and -DWITH_OPENMP flags OFF. Where possible, OpenCV will now use the default pthread or the TBB engine for parallelization.
We don't recommend it. Not all OpenCV algorithms automatically switch to pthread. Our advice is to import OpenCV into Python first before anything else.
Before we begin with the actual build of the library, there is one small step to go. You have to make a directory where all the build files can be located.
$ cd ~/opencv
$ mkdir build
$ cd build
Now it is time for an important step. Here you tell CMake what, where and how to make OpenCV on your Raspberry. There are many flags involved. The most you will recognize. You will probably notice the -D WITH_QT=OFF line. Here Qt5 support is disabled. Set -D WITH_QT=ON if you choose to use the Qt5 software for the GUI. We save space by excluding any (Python) examples or tests.
There are only bare spaces before the -D flags, not tabs. By the way, the two last dots are no typo. It tells CMake where it can find its CMakeLists.txt (the large recipe file); one directory up.