[Share] Install OpenCV 4.5 on Raspberry Pi 64-bit OS
[Copy link]
introduce
Raspberry Pi is moving towards a 64-bit operating system. In a year or so, the 32-bit operating system will be completely replaced by a faster 64-bit version.
The Raspberry Foundation recently released a more than functional beta version. Installation instructions can be found here. This guide is a guide to installing OpenCV 4.5.x on a 64-bit operating system on a Raspberry Pi 4.
You can find the release notes for the 4.5.x versions on OpenCV's GitHub page.
OpenCV Lite
Looking for a smaller OpenCV? Our OpenCV Lite installation might be just what you need. Just a very basic installation that saves you a lot of memory.
OpenCV 4.5.1
On December 22, 2020, OpenCV released version 5.1.4. The most important improvements in this version are the G-API framework and the RISC-V port. Neither of them will affect the Raspberry Pi. That's why we don't have a special installation guide for this version. If you prefer to use a version older than 4.5.0, feel free to change the version number from 4.5.0 to 4.5.1 during the download process.
OpenCV 4.5.2
On April 2, 2021, OpenCV released version 4.5.2. Find the version breaking changes in the log file here. Important changes were made in the imgproc, videoio and DNN modules. Feel free to change the version number from 4.5.0 to 4.5.2 during the download process.
OpenCV 4.5.3
On July 5, 2021, OpenCV released version 4.5.3. Besides various bug fixes and improvements, the most important changes are the DaSiamRPN tracker, a GPU FFmpeg backend for videoio and MatMul support in the CUDA backend. More information is here.
OpenCV 4.5.4
On October 11, 2021, OpenCV released version 4.5.4. The main change is the continued improvement of the DNN module. Of course, there are bug fixes and improvements. More information is here.
OpenCV 4.5.5
On December 25, 2021, OpenCV released version 4.5.5. Again, the biggest change is the improvements to the DNN module. Of course, there are bug fixes and improvements. More information is here.
You should know that there is a problem. The Python3 installation script was modified incorrectly. It puts all the libraries in the wrong directory and Python can't find them. By adding -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages to the build, the libraries are stored in the correct folder. We will remove this workaround once the problem is resolved. Here is the bug report on GitHub.
OpenCV 4.6.0
On July 7, 2022, OpenCV released version 4.6.0. In addition to the changes and improvements to the DNN library, the most important change is support for GCC 15 and Clang 5 compilers. Also worth noting is support for FFmpeg 5.0.
We have to use the additional line -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages.
Starting with Python 3.12, installation in the dist-packages folder is deprecated in favor of the site-package folder.
Version Check
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 using the screen print below.
You also need to check your C++ compiler version with the command gcc -v . It must also be the aarch64-linux-gnu version as shown in the screenshot. If you are using a 64-bit OS but your gcc version is different from the one given above, reinstall the entire OS with the latest version. Here is a guide: Installing a 64-bit OS on Raspberry Pi 4. You must have a 64-bit C++ compiler because we will be building all the libraries from scratch. Otherwise, there is no point in building the 32-bit version on a 64-bit machine.
Exchange Check
The next check is the size of the memory swap. It must be big enough to support the build.
Version 4.5.2
OpenCV version 4.5.2 requires more memory. If you have a Raspberry Pi 4 with 4 or 8 GB of memory, nothing changes. Only if you have a Raspberry Pi 4 with 2 GB of RAM, you need to increase the swap space in the zram configuration file. Follow the instructions you already know when installing a 64-bit operating system.
$ 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 three times, you will get 1.8 GB of additional swap space. This is enough 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 5.5 GB of total memory. If you installed a 64-bit operating system according to our guide, this should be no problem.
If you have a Raspberry Pi 4 without zram.sh installed , you may have installed dphys-swapfile with the initial installation. In this case, you must increase the default 100 MB swap space. With a Raspberry Pi with 4 GB of RAM onboard, you must increase to a maximum of 2048 GByte. However, with a Raspberry Pi with only 2 or 1 GB of RAM, you will run into trouble.
To exceed the 2048 MB limit usually found in /etc/dphys-swapfile, you must first increase this maximum in /sbin/dphys-swapfile. The slideshow below demonstrates everything.
# activate the modifications
$ sudo reboot
EEPROM Check
The last thing to do is to check the EEPROM software version. The Raspberry Pi 3 has all of its operating software installed on an SD card. The Raspberry Pi 4, on the other hand, also boots partially from two EEPROMs. These EEPROMs are programmed at the factory after PCB assembly. The Raspberry Foundation recently released new and improved software for these EEPROMs. This has nothing to do with OpenCV, but everything to do with thermals. In one of our vision applications, the CPU heat dropped from 65°C (149°F) to 48°C (118°F) simply by updating the contents of the EEPROM. And, as you know, lower CPU temperatures will extend the life of your Pi. See this article for more details.
Use the following commands to check and, if needed, update the EEPROM. Here are the instructions.
# to get the current status
$ sudo rpi-eeprom-update
# if needed, to update the firmware
$ sudo rpi-eeprom-update -a
$ sudo reboot
Notice
A few words of warning. Do not use pip3 to install OpenCV on your Raspberry Pi. Also do not use sudo apt-get install. All versions are not up to date and certainly not 64-bit. The only correct way to install OpenCV 4.5 is to build it from source.
Graphics memory
As described here, the physical RAM chips are used by both the CPU and the GPU. If necessary, use the menu below to check and change the capacity to at least 128 MB.
Installation Script
Installing OpenCV on the Raspberry Pi is not complicated. With its 60 command lines, it is more of an administrative task. That's why we created an installation script that can execute all the commands in this guide at once. Use it if you want, it shouldn't cause any problems. The whole installation takes about an hour and a half to complete. It starts with installing dependencies and ends with ldconfig.
If you want to beautify your OpenCV with a Qt5 GUI, follow the instructions on the GitHub page or in the following section.
OpenCV 4.5.5
# check your memory first
$ free -m
# you need at least a total of 6.5 GB!
# if not, enlarge your swap space as explained in the guide
$ wget
$ sudo chmod 755 ./OpenCV-4-5-5.sh
$ ./OpenCV-4-5-5.sh
OpenCV 4.5.4
# check your memory first
$ free -m
# you need at least a total of 6.5 GB!
# if not, enlarge your swap space as explained in the guide
$ wget
$ sudo chmod 755 ./OpenCV-4-5-4.sh
$ ./OpenCV-4-5-4.sh
OpenCV 4.5.3
# check your memory first
$ free -m
# you need at least a total of 6.5 GB!
# if not, enlarge your swap space as explained in the guide
$ wget
$ sudo chmod 755 ./OpenCV-4-5-3.sh
$ ./OpenCV-4-5-3.sh
OpenCV 4.5.2
# check your memory first
$ free -m
# you need at least a total of 6.5 GB!
# if not, enlarge your swap space as explained in the guide
$ wget
$ sudo chmod 755 ./OpenCV-4-5-2.sh
$ ./OpenCV-4-5-2.sh
OpenCV 4.5.1
# check your memory first
$ free -m
# you need at least a total of 5.5 GB
# if not, enlarge your swap space as explained in the guide
$ wget
$ sudo chmod 755 ./OpenCV-4-5-1.sh
$ ./OpenCV-4-5-1.sh
OpenCV 4.5.0
# check your memory first
$ free -m
# you need at least a total of 5.5 GB
# if not, enlarge your swap space as explained in the guide
$ wget
$ sudo chmod 755 ./OpenCV-4-5-0.sh
$ ./OpenCV-4-5-0.sh
Cleaning and removing dphys-swap should be done manually following these instructions.
rely
Not using scripts? Here is the full guide.
The OpenCV software uses other third-party software libraries. These must be installed first. Some come with the Raspberry 64-bit OS, and some may already be installed. It is better to be safe than sorry, so here is the full list. Only install the latest packages according to this procedure.
# check for updates
$ 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 libgstreamer1.0-dev gstreamer1.0-gtk3
$ sudo apt-get install libgstreamer-plugins-base1.0-dev gstreamer1.0-gl
$ 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 developing 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 like sliders and checkboxes. It is definitely not mandatory for OpenCV to work, but is only for cosmetic reasons. It can be said that using Qt5 will slow down OpenCV by a few percentage points. If you choose the fastest application, don't use it.
If you wish to enable Qt5 support in OpenCV, you have to download the library as shown in the following command. The next step is to set the -D WITH_QT flag during the build process.
# only install if you want Qt5
# to beautify your OpenCV GUI
$ sudo apt-get install qt5-default
Download OpenCV
Once all third-party software is installed, you can download OpenCV itself. Two packages are needed; the base version and the rest. Once downloaded, you can unzip the files. Note the line breaks in the text box. Both commands start with wget and end with zip.
OpenCV 4.5.5
# check your memory first
$ free -m
# you need at least a total of 6.5 GB!
# if not, enlarge your swap space as explained earlier
# download the latest version
$ cd ~
$ wget -O opencv.zip
$ wget -O opencv_contrib.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.5.5 opencv
$ mv opencv_contrib-4.5.5 opencv_contrib
# clean up the zip files
$ rm opencv.zip
$ rm opencv_contrib.zip
OpenCV 4.5.4
# check your memory first
$ free -m
# you need at least a total of 6.5 GB!
# if not, enlarge your swap space as explained earlier
# download the latest version
$ cd ~
$ wget -O opencv.zip
$ wget -O opencv_contrib.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.5.4 opencv
$ mv opencv_contrib-4.5.4 opencv_contrib
# clean up the zip files
$ rm opencv.zip
$ rm opencv_contrib.zip
OpenCV 4.5.3
# check your memory first
$ free -m
# you need at least a total of 6.5 GB!
# if not, enlarge your swap space as explained earlier
# download the latest version
$ cd ~
$ wget -O opencv.zip
$ wget -O opencv_contrib.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.5.3 opencv
$ mv opencv_contrib-4.5.3 opencv_contrib
# clean up the zip files
$ rm opencv.zip
$ rm opencv_contrib.zip
OpenCV 4.5.2
# check your memory first
$ free -m
# you need at least a total of 6.5 GB!
# if not, enlarge your swap space as explained earlier
# download the latest version
$ cd ~
$ wget -O opencv.zip
$ wget -O opencv_contrib.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.5.2 opencv
$ mv opencv_contrib-4.5.2 opencv_contrib
# clean up the zip files
$ rm opencv.zip
$ rm opencv_contrib.zip
OpenCV 4.5.1
# download the latest version
$ cd ~
$ wget -O opencv.zip
$ wget -O opencv_contrib.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.5.1 opencv
$ mv opencv_contrib-4.5.1 opencv_contrib
# clean up the zip files
$ rm opencv.zip
$ rm opencv_contrib.zip
OpenCV 4.5.0
# download the latest version
$ cd ~
$ wget -O opencv.zip
$ wget -O opencv_contrib.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.5.0 opencv
$ mv opencv_contrib-4.5.0 opencv_contrib
# clean up the zip files
$ rm opencv.zip
$ rm opencv_contrib.zip
Virtual Environment
Now it's time to decide whether to use a virtual environment for the OpenCV installation. We will not use a virtual environment. Instead, we can simply swap the SD card if needed. However, installing a virtual environment is also possible. All instructions are given in the 32-bit OpenCV guide.
TensorFlow
Importing TensorFlow and OpenCV into Python may raise the error: cannot allocate memory in static TLS block.
This behavior only occurs on aarch64 systems and is caused by OpenMP memory requirements not being met. For more information, see GitHub #14884.
There are several solutions. The simplest way is to import OpenCV at the beginning, as shown above.
Another is to disable OpenMP by setting the -DBUILD_OPENMP and -DWITH_OPENMP flags OFF. OpenCV will now use the default pthread or TBB engine for parallelization when possible.
We do not recommend it. Not all OpenCV algorithms will automatically switch to pthread. Our recommendation is to import OpenCV into Python first.
Build and compile
Before we start actually building the library, there is one small step left to go. You must create a directory where all the built files can be found.
$ cd ~/opencv
$ mkdir build
$ cd build
Now it's time for the big step. Here you tell CMake what, where and how to build OpenCV on your Raspberry. There are many flags involved. You'll recognize most. You'll probably notice the -D WITH_QT=OFF line. Qt5 support is disabled here. If you choose to use Qt5 software for the GUI, set -D WITH_QT=ON. We save space by excluding any (Python) examples or tests.
There are only exposed spaces before the -D flag, no tabs. The last two dots are not a typo, by the way. It tells CMake where to find its CMakeLists.txt (the big configuration file); one directory up.
OpenCV 4.5.5
$ 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_OPENMP=ON \
-D WITH_OPENCL=OFF \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D WITH_GSTREAMER=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 PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF ..
OpenCV 4.5.4
$ 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_OPENMP=ON \
-D WITH_OPENCL=OFF \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D WITH_GSTREAMER=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_opencv_python3=TRUE \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF ..
OpenCV 4.5.3
$ 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_OPENMP=ON \
-D WITH_OPENCL=OFF \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D WITH_GSTREAMER=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_opencv_python3=TRUE \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF ..
OpenCV 4.5.2
$ 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_OPENMP=ON \
-D WITH_OPENCL=OFF \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D WITH_GSTREAMER=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_opencv_python3=TRUE \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF ..
OpenCV 4.5.1
$ 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_OPENMP=ON \
-D WITH_OPENCL=OFF \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=OFF \
-D WITH_GSTREAMER=ON \
-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 ..
OpenCV 4.5.0
$ 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_OPENMP=ON \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=OFF \
-D WITH_GSTREAMER=ON \
-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 , all goes well and CMake ships with a report similar to the screenshot below.
Compile
Once all the compilation instructions are complete, you can start the build using the following command. This will take a while (minimum 1:30).
$ make -j4
Hopefully your build will be as successful as the one below.
Now to finish, install all the generated packages into your system’s database using the following command.
$ sudo make install
$ sudo ldconfig
# cleaning (frees 300 KB)
$ make clean
$ sudo apt-get update
examine
Now it’s time to check your installation in Python. You can use the command as shown in the screen below. It’s all self-explanatory. Obviously, if you installed OpenCV in a virtual environment, you need to activate this environment first using the command workon.
OpenCV will be installed in the /usr/local directory and all files will be copied to the following locations:
/usr/local/bin- 可执行文件
/usr/local/lib- libraries (.so)
/usr/local/cmake/opencv4- cmake package
/usr/local/include/opencv4- headers
/usr/local/share/opencv4- 其他文件(例如 XML 格式的文件)
Clear
The first thing to do is to reset the swap space back to its original 100 MB if you installed dphys-swapfile. Flash memory can only be written to for a limited number of cycles. Eventually, it will wear out your SD card. Therefore, it is wise to keep memory swapping to a minimum. Also, it will slow down your applications. That's why we prefer to use zram as described in the Raspberry Pi 4 64-bit OS guide.
$ sudo nano /etc/dphys-swapfile
set CONF_SWAPSIZE=100 with the Nano text editor
$ sudo reboot
If you are running low on disk space, you can consider deleting the ~/opencv and ~/opencv_contrib folders. With the command sudo make install you have copied all headers and libraries to the appropriate locations on disk. The original files in the ~/opencv/build/ folder are no longer needed. Of course, if you have enabled building of examples, for example, you can't delete the folders, because this would also delete those examples. This is just a tip and saves about 1.5 GB of space.
# just a tip to save some space
$ sudo rm -rf ~/opencv
$ sudo rm -rf ~/opencv_contrib
Reprinted from: https://qengineering.eu/install-opencv-4.5-on-raspberry-64-os.html
|