Install OpenCV on Jetson Orin Nano
Last updated: November 17, 2023
Why would you install OpenCV on the Jetson Orin Nano, when your operating system has already a version pre-installed? The main reason is that the shipped outdated version 4.5.4 has no CUDA support. And after all, wasn't the CUDA accelerator the main reason you bought the Orin Nano in the first place?
Basically, there are three ways to install OpenCV on a machine. Let's take a closer look.
First, we have the Python package manager pip.
To install OpenCV, you give the command $ pip3 install opencv-python or $ pip3 install opencv-contrib-python.
The latter installs all the contributions, as well as the core opencv-python library. Do not use them! The distributions come without CUDA bindings, so you gain nothing. Even worse, the installation is likely to interfere with the already existing OpenCV. And what's more, it doesn't support C++ programming.
sudo apt-get install.
The second common method is $ sudo apt-get install libopencv-dev. Unfortunately, he returns with the comment that you already have the latest version (4.5.4 !). And still you are missing CUDA support. So there is no need to use it.
Compiling from source.
In fact, the only method to install the latest OpenCV on the Orin Nano with CUDA and cuDNN support is by building it from the source.
Installing OpenCV on your Orin Nano is not that complicated. With its 63 command lines, it is more of an administrative task. That's why we created an installation script that executes all commands in this guide one after the other.
It is the same script used for the regular Nano. The script will detect the right Nano. Use it if you like it shouldn't cause any problems.
The whole installation will take two to three hours to complete. It starts with installing the dependencies and ends with the $sudo 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 guide.
The OpenCV software uses other third-party software libraries. Install these first. Some come with the Jetson Tegra operating system, and others you may already have installed. Better to be safe than sorry, so here's the complete 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 cmake
$ 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 python3-pip
$ sudo apt-get install libxvidcore-dev libx264-dev
$ sudo apt-get install libtbb-dev libdc1394-22-dev libxine2-dev
$ sudo apt-get install libv4l-dev v4l-utils qv4l2
$ sudo apt-get install libtesseract-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 your Orin Nano. 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 Orin Nano. 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 want 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 typos. It tells CMake where it can find its CMakeLists.txt (the large recipe file), one directory up.
The list is almost identical to the one used for the Jetson Nano.
Only the CUDA_ARCH number 8.7 reflects the Ampere CUDA architecture instead of Maxwells 5.3 used in the regular Nano.
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 keep the Tegra installation directory to let pre-existing software run smoothly with the reinstalled OpenCV. Keep it in mind when you port some of our Raspberry Pi software on GitHub to your Orin Nano.