Install OpenCV 4.3.0 on Raspberry 64 OS
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. Time to explore it. This guide will install OpenCV 4.3.0 on a Raspberry Pi 4 with a 64-bit operating system.
You can find the release notes of version 4.4.0 on the GitHub page of OpenCV here.
Before installing OpenCV 4.3.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.
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. 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.
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.3.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.
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
$ 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
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.3.0.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.3.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. It makes live easier later on.
$ mv opencv-4.3.0 opencv
$ mv opencv_contrib-4.3.0 opencv_contrib
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.
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. We save space by excluding any (Python) examples or tests.
There are only bare spaces before the -D flags, not tabs. Keep also a single space between the -D and the argument. Otherwise, CMake will misinterpreter the command. 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_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=OFF \
-D WITH_GSTREAMER=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 ..
Please note the absence of the ENABLE_VFPV3=ON flag used by the building of OpenCV for the 32-bits version.
If you had used this flag, cmake would generate erros. See the screendum below.
The error is quite misleading; Regex: "command line option. * Is valid for. * But not for C ++ '. This suggests an incompatibility of compilers instead of an unavailable option. 'Option VFPV3 not applicable' would be a better warning. OpenCV notorious gives these types of warnings, good to keep in mind. You can simply remove all your flags from the cmake command line and see if the build now succeeds. If so, add your flags one by one. This way, your problem will be located soon.
Hopefully, everything went well and CMake comes with a report that looks something like the screenshot below.
With all compilation directives in place, you can start the build with the following command. This will take a while (± 50 min).
$ make -j4
Hopefully, your build was as successful as the one below.
Now to complete, install all the generated packages to the database of your system with the next commands.
$ sudo make install
$ sudo ldconfig
# cleaning (frees 300 KB)
$ make clean
$ sudo apt-get update
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.