Install OpenCV 4.5 on Jetson Nano
Last updated: October 21, 2021
Why would you install OpenCV on the Jetson Nano, when your operating system has already a version pre-installed? The main reason is that the shipped version has no CUDA support. And after all, wasn't the CUDA accelerator the main reason you bought the Jetson Nano in the first place?
Regularly, we get the question if we have an SD image of a Jetson Nano with pre-installed OpenCV, TensorFlow and PyTorch.
We are happy to comply with this request. Please, find a complete Jetson Nano image on our GitHub page. Download the zip file from our Gdrive site, unzip and flash the image on a 32 GB SD-card, and enjoy!
On December 22, 2020, OpenCV released version 4.5.1. The most important improvement in this version is the work on the G-API framework and the RISC-V port. Neither will affect the Jetson Nano.
On April 2, 2021, OpenCV released version 4.5.2. Find the significant changes in version in the log file here. Important changes are made in the imgproc, videoio and DNN module.
On July 5, 2021, OpenCV released version 4.5.3. Besides various bug fixes and improvements, the most significant changes are the DaSiamRPN tracker, a GPU FFmpeg backend for videoio and MatMul support in the CUDA backend. More info here.
On October 11, 2021, OpenCV released version 4.5.4. The main change is the continuous improvements of the DNN module. Of course there are bug fixes and improvements. More info here.
It may be advisable to install the lightdm desktop instead of the gdm3 when prompted during the initial installation, saving about 500 MByte of precious memory space. It can make all the difference, especially with memory-hungry deep learning models. Although some of you probably prefer the Ubuntu look and feel to the somewhat archaic lxdm experience.
Before installing OpenCV 4.5.0 on your Jetson Nano, consider overclocking. When the CUDA accelerator is not used, which is in most daily applications, the Jetson Nano has a quad ARM Cortex-A57 core running at 1.4 GHz. Compared to the quad Cortex-A72 at 1.5 GHz of the Raspberry Pi 4, there isn't that great a difference. Especially with an overclocked RPi. In that case, it will beat the Jetson in most tasks. Except, of course, when the CUDA kicks in. On this page the overclocking of the Jetson Nano is covered.
So, time to reinstall OpenCV on the Jetson Nano with the CUDA and cuDNN support this time.
Enlarge memory swap.
Building the full OpenCV 4.5 package requires more than 4 Gbytes of RAM and the 2 Gbytes of swap space delivered by zram usually found on your Jetson Nano. We have to install dphys-swapfile to get the additional space from your SD card temporarily. After the compilation, the mechanism will be removed, eliminating swapping to the SD card. Please follow the next commands. Note also the installation of nano, a tiny text editor.
OpenCV 4.5.2, 4.5.3 and 4.5.4.
As of version 4.5.2, OpenCV even needs more memory than the previous version. In this case, you need to increase the dphys swap beyond the regular 2048 limit. It is done by first changing the maximum boundary in /sbin/dphys-swapfile to 4096. Next, set the /etc/dphys-swapfile. The slideshow will guide you. If there is not enough swap memory, the compilation will crash at 100% without notification, especially when using all 4 cores simultaneously.
We do not recommend increasing the zram swap limits. You can't just keep compressing system memory in the hopes of getting some extra space. There are limits. It is better that you temporarily use the SD memory. Once OpenCV is installed, you can remove dphys-swapfile again.
# a fresh start, so check for updates
$ sudo apt-get update
$ sudo apt-get upgrade
# install nano
$ sudo apt-get install nano
# install dphys-swapfile
$ sudo apt-get install dphys-swapfile
# enlarge the boundary (4.5.2, 4.5.3 and 4.5.3 only)
$ sudo nano /sbin/dphys-swapfile
# give the required memory size
$ sudo nano /etc/dphys-swapfile
# reboot afterwards
$ sudo reboot.
Installing OpenCV on your Jetson Nano is not that complicated. With its 68 command lines, it is more of an administrative task. That's why we created an installation script that executes all commands in this guide at once. Use it if you want, it shouldn't cause any problems. The whole installation will take two hours to complete. It starts with the installation of the dependencies and ends with the ldconfig.
If you want to beautify your OpenCV with the Qt5 GUI, follow the instructions on the GitHub page, or in the section below.
Not using the script? Here's the full guide.
The OpenCV software uses other third party software libraries. Install these first. Some come with the Jetson Tegra operating system, others you may already have installed. Better to be safe than sorry, so here's the full list. Only the latest packages are installed according to the procedure.
The installation will take up an additional 800 MByte on your disk, if not already present.
# reveal the CUDA location
$ sudo sh -c "echo '/usr/local/cuda/lib64' >> /etc/ld.so.conf.d/nvidia-tegra.conf"
$ sudo ldconfig
# third-party libraries
$ sudo apt-get install build-essential cmake git unzip pkg-config zlib1g-dev
$ sudo apt-get install libjpeg-dev libjpeg8-dev libjpeg-turbo8-dev
$ sudo apt-get install libpng-dev libtiff-dev libglew-dev$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev $ sudo apt-get install libgtk2.0-dev libgtk-3-dev libcanberra-gtk*$ sudo apt-get install python-dev python-numpy python-pip$ sudo apt-get install python3-dev python3-numpy python3-pip$ sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev$ sudo apt-get install libtbb2 libtbb-dev libdc1394-22-dev libxine2-dev$ sudo apt-get install gstreamer1.0-tools libgstreamer-plugins-base1.0-dev
$ sudo apt-get install libgstreamer-plugins-good1.0-dev
$ sudo apt-get install libv4l-dev v4l-utils v4l2ucp qv4l2
$ sudo apt-get install libtesseract-dev libxine2-dev libpostproc-dev$ sudo apt-get install libavresample-dev libvorbis-dev $ sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev $ sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev$ sudo apt-get install libopenblas-dev libatlas-base-dev libblas-dev$ sudo apt-get install liblapack-dev liblapacke-dev libeigen3-dev gfortran$ sudo apt-get install libhdf5-dev libprotobuf-dev protobuf-compiler
$ sudo apt-get install libgoogle-glog-dev libgflags-dev
Qt is an open-source toolkit for the development of cross-platform graphical user interfaces. It also works on the Linux Tegra operating system. 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.
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
When all third-party software is installed, OpenCV itself can be downloaded. There are two packages needed; the basic version and the additional contributions. 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.
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.
Also, note the /usr directory where all header files and OpenCV libraries will reside. We have a slight preference for the /usr/local folder. For now, we'll be following the Tegra installation directory to keep pre-existing software running smoothly with the reinstalled OpenCV. Keep it in mind when you port some of our Raspberry Pi software on GitHub to your Jetson Nano.