Install OpenCV 4.5 on Raspberry 64 OS - Q-engineering
Q-engineering
Q-engineering
Go to content
images/empty-GT_imagea-1-.png
OpenCV on Raspberry Pi

Install OpenCV 4.5.0 on Raspberry 64 OS

64-OS OpenCV 4.4.0
32-OS OpenCV 4.5.0

Introduction.

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.0 on a Raspberry Pi 4 with a 64-bit operating system.
You can find the release notes of version 4.5.0 on the GitHub page of OpenCV here.

Version check.
Before installing OpenCV 4.5.0 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.

Version_OpenCV_64

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.

Swap check.
The next check is the size of the memory swap. It must be large enough to support the building. You should have at least 2 Gbyte RAM and 2 Gbyte swap space. If you have installed the 64-bit OS according to our guide, this should be no problem. Otherwise, enlarge your memory swap size.

Swap size

EEPROM check.


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
EEPROM_4_3

Bad ideas.

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.
GPU memory.
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.
Configuration 1
Configuration 2

Dependencies.

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
# dependencies
$ 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

Qt5.

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.
No Qt5
With Qt5
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

Download OpenCV.

When all third-party software is installed, OpenCV itself can be downloaded. There are two packages needed; the basic version and the additional contributions. Check before downloading the latest version at https://opencv.org/releases/. If necessary, change the names of the zip files according to the latest version. 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.
$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.0.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.0.zip

$ unzip opencv.zip
$ unzip opencv_contrib.zip
The next step is some administration. Rename your directories with more convenient names like opencv and opencv_contrib. This makes live later on easier.
$ mv opencv-4.5.0 opencv
$ mv opencv_contrib-4.5.0 opencv_contrib

Virtual environment.

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.

Build Make.

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.
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
        -D ENABLE_NEON=ON \
        -D WITH_FFMPEG=ON \
        -D WITH_GSTREAMER=ON \
        -D WITH_TBB=ON \
        -D BUILD_TBB=ON \
        -D BUILD_TESTS=OFF \
        -D WITH_EIGEN=OFF \
        -D WITH_V4L=ON \
        -D WITH_LIBV4L=ON \
        -D WITH_VTK=OFF \
        -D WITH_QT=OFF \
        -D OPENCV_ENABLE_NONFREE=ON \
        -D INSTALL_C_EXAMPLES=OFF \
        -D INSTALL_PYTHON_EXAMPLES=OFF \
        -D BUILD_NEW_PYTHON_SUPPORT=ON \
        -D BUILD_opencv_python3=TRUE \
        -D OPENCV_GENERATE_PKGCONFIG=ON \
        -D BUILD_EXAMPLES=OFF ..
Hopefully, everything went well and CMake comes with a report that looks something like the screenshot below.

CMake succes 64

Make.

With all compilation directives in place, you can start the build with the following command. This will take a while (minimal 1:30).
$ make -j4
Hopefully, your build was as successful as the one below.

Build_Rdy_4_5

Now to complete, install all the generated packages to the database of your system with the next commands.
$ sudo make install
$ sudo ldconfig
$ sudo apt-get update

Checking.

Now it is time to check your installation in Python. You can use the commands as shown is the screen dump below. It all speaks for itself. Obvious, if you have installed OpenCV in a virtual environment, you need to activate this environment first with the command workon.

Success_4_5_0_64

Cleaning.

If you are in shortage of disk space, you may consider deleting the ~/opencv and ~/opencv_contrib folders. By the command, sudo make install, you have copied all the headers and libraries to their appropriate locations on your disk. The original files in the ~/opencv/build/ folder are no longer needed. Of course, if you have, for instance, enabled the building of examples, you can not delete the folders as this will also remove these examples. Let's say it's just a tip saving about 1.5 GByte of space.
# just a tip to save some space
$ sudo rm -rf ~/opencv
$ sudo rm -rf ~/opencv_contrib
Deep learning examples for Raspberry Pi
Install 64 OS
Install 32 OS
Raspberry and alt
Raspberry Pi 4
Jetson Nano
Back to content