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

Install OpenCV Lite 4.5 on Raspberry 64 OS

64-OS OpenCV 4.5

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
Raspberry Pi Zero, 1GHz, single-core ARMv6 CPU (32 bit), 512 MB RAM
Bana Pi zero
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.

1 GB swap
500 MB RAM.
If you have less than 1 GB onboard, forces you to enlarge the swapping space. For instance, a Raspberry Pi zero.

none

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

No swap

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.

SwapDphysOpenCVLite

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
$ sudo nano /etc/dphys-swapfile
# change your swap size and restart service
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo /etc/init.d/dphys-swapfile start

DphysOpenCVLite

Swap 877

If you see four partitions, you have zram working.

SwapZramOpenCVLite

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.
BoardOSClock
make
Time (HH:MM)
Raspberry Pi Zero32 bit1 GHz- j13 : 40
Raspberry Pi 232 bit900 MHz-j41 : 00
Raspberry Pi 332 bit1.4 GHz-j40 : 24
Raspberry Pi 4
32 bit1.5 GHz- j40 : 13
Raspberry Pi 4
64 bit1.5 GHz- j40 : 15
Raspberry Pi 4
64 bit1.9 GHz- j40 : 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.

LiteFull
Third-party libraries
210 MB564 MB
OpenCV headers
4.3 MB6.9 MB
OpenCV libraries
10.2 MB39.6 MB
Python libraries
2.3 MB8.2 MB
Python 3 libraries2.3 MB8.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.

OS

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
# 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
# install only if your have a RPi OS lite with no desktop
$ sudo apt-get install python3-dev python3-numpy python3-pip
$ sudo apt-get install python-dev python-numpy
# 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.
can be a Raspberry Pi 3, 4

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D ENABLE_NEON=ON \
-D BUILD_OPENMP=ON \
-D BUILD_TIFF=OFF \
-D BUILD_OPENJPEG=OFF \
-D BUILD_JASPER=OFF \
-D BUILD_OPENEXR=OFF \
-D BUILD_WEBP=OFF \
-D BUILD_TBB=ON \
-D BUILD_IPP_IW=OFF \
-D BUILD_ITT=OFF \
-D WITH_OPENMP=ON \
-D WITH_OPENCL=OFF \
-D WITH_AVFOUNDATION=OFF \
-D WITH_CAP_IOS=OFF \
-D WITH_CAROTENE=OFF \
-D WITH_CPUFEATURES=OFF \
-D WITH_EIGEN=OFF \
-D WITH_GSTREAMER=OFF \
-D WITH_GTK=ON \
-D WITH_IPP=OFF \
-D WITH_HALIDE=OFF \
-D WITH_VULKAN=OFF \
-D WITH_INF_ENGINE=OFF \
-D WITH_NGRAPH=OFF \
-D WITH_JASPER=OFF \
-D WITH_OPENJPEG=OFF \
-D WITH_WEBP=OFF \
-D WITH_OPENEXR=OFF \
-D WITH_TIFF=OFF \
-D WITH_OPENVX=OFF \
-D WITH_GDCM=OFF \
-D WITH_TBB=ON \
-D WITH_HPX=OFF \
-D WITH_EIGEN=OFF \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_VTK=OFF \
-D WITH_QT=OFF \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_java=OFF \
-D BUILD_opencv_gapi=OFF \
-D BUILD_opencv_objc=OFF \
-D BUILD_opencv_js=OFF \
-D BUILD_opencv_ts=OFF \
-D BUILD_opencv_dnn=OFF \
-D BUILD_opencv_calib3d=OFF \
-D BUILD_opencv_objdetect=OFF \
-D BUILD_opencv_stitching=OFF \
-D BUILD_opencv_ml=OFF \
-D BUILD_opencv_world=OFF \
-D BUILD_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=OFF \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF ..
can be a Raspberry Pi 2, 3, 4

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D BUILD_OPENMP=ON \
-D BUILD_TIFF=OFF \
-D BUILD_OPENJPEG=OFF \
-D BUILD_JASPER=OFF \
-D BUILD_OPENEXR=OFF \
-D BUILD_WEBP=OFF \
-D BUILD_TBB=ON \
-D BUILD_IPP_IW=OFF \
-D BUILD_ITT=OFF \
-D WITH_OPENMP=ON \
-D WITH_OPENCL=OFF \
-D WITH_AVFOUNDATION=OFF \
-D WITH_CAP_IOS=OFF \
-D WITH_CAROTENE=OFF \
-D WITH_CPUFEATURES=OFF \
-D WITH_EIGEN=OFF \
-D WITH_GSTREAMER=OFF \
-D WITH_GTK=ON \
-D WITH_IPP=OFF \
-D WITH_HALIDE=OFF \
-D WITH_VULKAN=OFF \
-D WITH_INF_ENGINE=OFF \
-D WITH_NGRAPH=OFF \
-D WITH_JASPER=OFF \
-D WITH_OPENJPEG=OFF \
-D WITH_WEBP=OFF \
-D WITH_OPENEXR=OFF \
-D WITH_TIFF=OFF \
-D WITH_OPENVX=OFF \
-D WITH_GDCM=OFF \
-D WITH_TBB=ON \
-D WITH_HPX=OFF \
-D WITH_EIGEN=OFF \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_VTK=OFF \
-D WITH_QT=OFF \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_java=OFF \
-D BUILD_opencv_gapi=OFF \
-D BUILD_opencv_objc=OFF \
-D BUILD_opencv_js=OFF \
-D BUILD_opencv_ts=OFF \
-D BUILD_opencv_dnn=OFF \
-D BUILD_opencv_calib3d=OFF \
-D BUILD_opencv_objdetect=OFF \
-D BUILD_opencv_stitching=OFF \
-D BUILD_opencv_ml=OFF \
-D BUILD_opencv_world=OFF \
-D BUILD_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=OFF \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF ..
can be a Raspberry Zero

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D ENABLE_NEON=OFF \
-D ENABLE_VFPV3=OFF \
-D BUILD_OPENMP=OFF \
-D BUILD_TIFF=OFF \
-D BUILD_OPENJPEG=OFF \
-D BUILD_JASPER=OFF \
-D BUILD_OPENEXR=OFF \
-D BUILD_WEBP=OFF \
-D BUILD_TBB=OFF \
-D BUILD_IPP_IW=OFF \
-D BUILD_ITT=OFF \
-D WITH_OPENMP=OFF \
-D WITH_OPENCL=OFF \
-D WITH_AVFOUNDATION=OFF \
-D WITH_CAP_IOS=OFF \
-D WITH_CAROTENE=OFF \
-D WITH_CPUFEATURES=OFF \
-D WITH_EIGEN=OFF \
-D WITH_GSTREAMER=OFF \
-D WITH_GTK=ON \
-D WITH_IPP=OFF \
-D WITH_HALIDE=OFF \
-D WITH_VULKAN=OFF \
-D WITH_INF_ENGINE=OFF \
-D WITH_NGRAPH=OFF \
-D WITH_JASPER=OFF \
-D WITH_OPENJPEG=OFF \
-D WITH_WEBP=OFF \
-D WITH_OPENEXR=OFF \
-D WITH_TIFF=OFF \
-D WITH_OPENVX=OFF \
-D WITH_GDCM=OFF \
-D WITH_TBB=OFF \
-D WITH_HPX=OFF \
-D WITH_EIGEN=OFF \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_VTK=OFF \
-D WITH_QT=OFF \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_java=OFF \
-D BUILD_opencv_gapi=OFF \
-D BUILD_opencv_objc=OFF \
-D BUILD_opencv_js=OFF \
-D BUILD_opencv_ts=OFF \
-D BUILD_opencv_dnn=OFF \
-D BUILD_opencv_calib3d=OFF \
-D BUILD_opencv_objdetect=OFF \
-D BUILD_opencv_stitching=OFF \
-D BUILD_opencv_ml=OFF \
-D BUILD_opencv_world=OFF \
-D BUILD_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=OFF \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_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.
$ make -j$(nproc)
If everything went well, your build ends up as shown below.

Make ready

To finish, copy the generated packages to the appropriate directories and update your system database with the following commands.
$ sudo make install
$ sudo ldconfig
$ sudo apt-get update
The last action is removing the OpenCV folder.
# delete the OpenCV folder with all its temporary files
# if will give you back 357 MB of disk space
$ cd ~
$ sudo rm -rf opencv

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.

CheckOpenCVLite

Obviously, your version number will change over time as the OpenCV community releases new versions.

Below you see the folder with all headers. As you can see, each installed module has its own subfolder. It can be a simple check in the future when an algorithm requires a particular module, and you are not sure if it is already installed.

HeadersOpenCVLite

All libraries are installed in this folder.

none

Final remarks.
You've probably noticed, but this OpenCV Lite installation doesn't support OpenCV's deep learning module cv::dnn.
Not many people will use it. And if you need to run some deep learning network, consider ncnn. It is a very lightweight but yet complete framework.

Another point is the OpenCV Lite version for the Jetson Nano. We haven't discussed the topic because the Jetson Nano JetPack already has a stripped-down version of OpenCV on board. There is no point in repeating the installation.
Install 64 OS
Install 32 OS
Raspberry and alt
Raspberry Pi 4
Jetson Nano
images/GithubSmall.png
images/YouTubeSmall.png
images/SDcardSmall.png
Back to content