
Install OpenCV Lite 4.5 on Raspberry Pi.
64-OS OpenCV 4.5
Last updated: June 15, 2022
Introduction.
OpenCV is the tool for image processing and computer vision tasks. However, the package tries to fulfill almost every wish and grows over time. Nowadays it is quite a challenge to install the entire package with all its additional functions. Especially if you have a limited amount of memory on your board and you only need the basics.
The discussed installation is a striped version of OpenCV and is suitable for all boards with 32 or 64 bit CPU, from Raspberry Pi Zero to 4, and all are derivatives. As long as the operating system is a Debian distro, you can use this guide.

Raspberry Pi Zero, 1GHz, single-core ARMv6 CPU (32 bit), 512 MB RAM

Banana Pi Zero, 1.3 GHz, H2+ quad-core Cortex A7 (32-bit), 512 MB RAM
Pruning.
To reduce the footprint of the OpenCV installation, you can prune any unwanted functionality. For example, 3D calibration or stitching. Most users never use these features. The few highly qualified programmers who need them are perfectly capable of reinstalling the required algorithms themselves.
We have removed every dedicated algorithm, leaving only the pure basic functionality. These are picture and video i/o with their associate image manipulation, including the cv::mat operations.
GUI.
The subject of discussion is the GUI. As soon as you want to draw a picture on your screen, with, for instance, the OpenCV cv::imshow function, you will need to have a Graphical User Interface installed. OpenCV used the GTK toolkit. Although designed with the utmost care, the libraries still need a lot of space. The total installation of the GTK+2 environment takes 175 MB on your disk. You can choose not to install the GUI and get a really light OpenCV, but you won't be able to use the screen. You only work in memory. Besides, as soon as you create an application with, for example, Qt5, you will still have to install the GTK library. That is why we install the GTK, despite the required memory space.
Parallel framework.
OpenCV can use several parallel frameworks to speed up algorithms. By default, it uses pthread, but you can use OpenMP or TBB as a (slightly better) alternative.
There are some serious issues with OpenMP and the 64-bit operating system (aarch64). You cannot use OpenCV with OpenMP in Python without special arrangements. See issue #14884 on GitHub. If you want to use OpenCV with Python, you'd better use TBB as a parallel engine.
Of course, if your CPU has only one core (like the RPI Zero), no parallelization is needed. It will even slow down your code.
Memory check.
The next point to consider is the amount of RAM available. You need at least 1.9 GB of memory to build OpenCV.
2, 4, 8 GB RAM.
You have no problems with a Raspberry Pi 4 with 2, 4 or 8 GB of RAM. You don't even have to change your default swap limits; the default set to 100 MB when using dphys-swapfile.
1 GB RAM.
In this case, we recommend compiling OpenCV with only one core during installation (make -j1). The build now requires only 885 MB of RAM.

500 MB RAM.
If you have less than 1 GB on board, you will need to increase the swapping space. For example a Raspberry Pi Zero.

In general, there are two possible mechanisms used, zram or dphys-swapfile. You can check which one is installed by the command swapon -s.

When the command returns with no information, you don't have any swap mechanism currently working. We recommend installing zram, as it will not wear out your Sd-card unnecessarily. Follow the instructions given here.

With sudo nano /etc/dphys-swapfile, you can open the file and alter the amount of allocated swap memory. Use the arrow keys to move the cursor to the CONF_SWAPSIZE line where 512 can be entered. Next, close the session with the <Ctrl+X> key combination. With <Y> and <Enter>, changes are being saved. To activate the changes, restart the service by the commands given below


If you see four partitions, you have zram working.

Please check your zram documentation on how to enlarge the swap space on your board. In case you have installed zram according to our guide, please check the /usr/bin/zram.sh file.
Don't make the mistake of increasing the swap space by such an amount, hoping you can run a 4-core build on a 500 MB machine. Swapping also takes time. With 4 cores, you can end up with a massive bottleneck, smothering the swapping mechanism. Better, take a 50-minute break and make sure everything is running smoothly.
Board | OS | Clock | make | Time (HH:MM) |
Raspberry Pi Zero | 32 bit | 1 GHz | - j1 | 3 : 40 |
Raspberry Pi 2 | 32 bit | 900 MHz | -j4 | 1 : 00 |
Raspberry Pi 3 | 32 bit | 1.4 GHz | -j4 | 0 : 24 |
Raspberry Pi 4 | 32 bit | 1.5 GHz | - j4 | 0 : 13 |
Raspberry Pi 4 | 64 bit | 1.5 GHz | - j4 | 0 : 15 |
Raspberry Pi 4 | 64 bit | 1.9 GHz | - j4 | 0 : 12 |
Lite
General configuration for OpenCV 4.5.2 =====================================
Version control: unknown
Extra modules:
Location (extra): /home/pi/opencv_contrib/modules
Version control (extra): unknown
Platform:
Timestamp: 2021-04-07T16:11:13Z
Host: Linux 5.10.17-v8+ aarch64
**********
OpenCV modules:
To be built: core features2d flann highgui imgcodecs imgproc photo python2 python3 video videoio
**********
Media I/O:
ZLib: /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.11)
JPEG: /usr/lib/aarch64-linux-gnu/libjpeg.so (ver 62)
PNG: /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.36)
HDR: YES
SUNRASTER: YES
PXM: YES
PFM: YES
**********
Video I/O:
DC1394: YES (2.2.5)
FFMPEG: YES
avcodec: YES (58.35.100)
avformat: YES (58.20.100)
avutil: YES (56.22.100)
swscale: YES (5.3.100)
avresample: NO
GStreamer: NO
v4l/v4l2: YES (linux/videodev2.h)
Full
General configuration for OpenCV 4.5.2 =====================================
Version control: unknown
Extra modules:
Location (extra): /home/pi/opencv_contrib/modules
Version control (extra): unknown
Platform:
Timestamp: 2021-04-07T16:11:13Z
Host: Linux 5.10.17-v8+ aarch64
**********
OpenCV modules:
To be built: aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python2 python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
**********
Media I/O:
ZLib: /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.11)
JPEG: /usr/lib/aarch64-linux-gnu/libjpeg.so (ver 62)
WEBP: build (ver encoder: 0x020f)
PNG: /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.36)
TIFF: build (ver 42 - 4.2.0)
JPEG 2000: build (ver 2.4.0)
OpenEXR: build (ver 2.3.0)
HDR: YES
SUNRASTER: YES
PXM: YES
PFM: YES
**********
Video I/O:
DC1394: YES (2.2.5)
FFMPEG: YES
avcodec: YES (58.35.100)
avformat: YES (58.20.100)
avutil: YES (56.22.100)
swscale: YES (5.3.100)
avresample: NO
GStreamer: NO
v4l/v4l2: YES (linux/videodev2.h)
Foot print.
How much smaller is the stripped installation compared to the full OpenCV version. There are a few points to consider. First, OpenCV works with third-party libraries, like libjpeg-dev or libgtk2.0-dev. Without these libraries, OpenCV can't work. If you compare different installations, you should also consider the amount of disk space the required libraries will take. Second, obvious, you can compare the size of the different OpenCV libraries themselves. The table below gives you all the information. By the way, after a successful installation, the OpenCV folder itself is removed. It will otherwise take 356 MB of your disk.
Lite | Full | |
Third-party libraries | 210 MB | 564 MB |
OpenCV headers | 4.3 MB | 6.9 MB |
OpenCV libraries | 10.2 MB | 39.6 MB |
Python libraries | 2.3 MB | 8.2 MB |
Python 3 libraries | 2.3 MB | 8.2 MB |
Total | 229.1 MB | 626.9 MB |
As can be seen, the footprint is reduced by ± 65%. If the GTK+2 GUI was not installed, the total disk space would be 55 MB. However, as mentioned earlier, you'll need some GUI, so keep it in place. Another possible diminution is to delete the Python libraries. On the other hand, it only takes up 2% of the space, don't worry.
Operation system check.
Before you start downloading, just a last check to see which operating system is used, 32 or 64 bit. You also need to know how many cores your CPU has. Please follow the commands in the screen dump below.

Dependencies.
The OpenCV software uses other third-party software libraries. Below the list for the OpenCV Lite installation. Note the last instruction, the downloading of the TBB engine. If your CPU has just one core, there is no need to install this parallel framework. It will never be used.
# check for updates
$ sudo apt-get update
$ sudo apt-get upgrade
# general tools (35.8 MB)
$ sudo apt-get install build-essential cmake git pkg-config
# if you want to get OpenCV working in python or python3 (208 MB)
$ sudo apt-get install python3-dev python3-numpy
# The latest Debian 11, Bullseye don't support python2 full
# hence, no need to install if you're having a Raspberry Bullseye OS
$ sudo apt-get install python-dev python-numpy
# image formats (0.9 MB)
$ sudo apt-get install libjpeg-dev libpng-dev
# video formats (32.1 MB)
$ sudo apt-get install libavcodec-dev libavformat-dev
$ sudo apt-get install libswscale-dev libdc1394-22-dev
# video back engine (0.6 MB)
$ sudo apt-get install libv4l-dev v4l-utils
# the GTK+2 GUI (175 MB)
$ sudo apt-get install libgtk2.0-dev libcanberra-gtk* libgtk-3-dev
# parallel framework (2.7 MB)
# don't install if your having a 1 core CPU (like RPi zero)
$ sudo apt-get install libtbb2 libtbb-dev
Download OpenCV.
When all third-party software is installed, OpenCV can be downloaded and prepared. Please be aware of line wrapping in the text boxes. The two commands are starting with git and ending with git.
# download OpenCV (you get always the latest version)
# 280 MB, which will be removed later
$ cd ~
$ git clone --depth=1 https://github.com/opencv/opencv.git
$ cd opencv
$ mkdir build
$ cd build
Now it is time to tell CMake what, where and how to make OpenCV on your Raspberry. There are many flags involved. 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 to find its CMakeLists.txt (the large recipe file); one directory up. Please select the correct operating system, 32 or 64 bit and number of cores.