OpenCV - Q-engineering
Q-engineering
Q-engineering
Go to content
images/empty-GT_imagea-1-.png
OpenCV on Jetson Orin Nano

Install OpenCV on Jetson Orin Nano

Last updated: November 17, 2023

Introduction.

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?

No Cuda OpenCV

Basically, there are three ways to install OpenCV on a machine. Let's take a closer look.

pip.

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.

No Need OpenCV

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.
Installation script.
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.
$ wget https://github.com/Qengineering/Install-OpenCV-Jetson-Nano/raw/main/OpenCV-4-8-0.sh
$ sudo chmod 755 ./OpenCV-4-8-0.sh
$ ./OpenCV-4-8-0.sh
# once the installation is done...
$ rm OpenCV-4-8-0.sh
# just a tip to save an additional 275 MB
$ sudo rm -rf ~/opencv
$ sudo rm -rf ~/opencv_contrib
$ wget https://github.com/Qengineering/Install-OpenCV-Jetson-Nano/raw/main/OpenCV-4-7-0.sh
$ sudo chmod 755 ./OpenCV-4-7-0.sh
$ ./OpenCV-4-7-0.sh
# once the installation is done...
$ rm OpenCV-4-7-0.sh
# just a tip to save an additional 275 MB
$ sudo rm -rf ~/opencv
$ sudo rm -rf ~/opencv_contrib
$ wget https://github.com/Qengineering/Install-OpenCV-Jetson-Nano/raw/main/OpenCV-4-6-0.sh
$ sudo chmod 755 ./OpenCV-4-6-0.sh
$ ./OpenCV-4-6-0.sh
# once the installation is done...
$ rm OpenCV-4-6-0.sh
# just a tip to save an additional 275 MB
$ sudo rm -rf ~/opencv
$ sudo rm -rf ~/opencv_contrib

Dependencies.

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

Qt5.

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.
With Qt5
No Qt5
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

Download OpenCV.

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.
# download the latest version
$ cd ~
$ git clone --depth=1 https://github.com/opencv/opencv.git
$ git clone --depth=1 https://github.com/opencv/opencv_contrib.git
$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.7.0.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.7.0.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.7.0 opencv
$ mv opencv_contrib-4.7.0 opencv_contrib
# clean up the zip files
$ rm opencv.zip
$ rm opencv_contrib.zip
$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.6.0.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.6.0.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.6.0 opencv
$ mv opencv_contrib-4.6.0 opencv_contrib
# clean up the zip files
$ rm opencv.zip
$ rm opencv_contrib.zip

Build Make.

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.
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D EIGEN_INCLUDE_PATH=/usr/include/eigen3 \
-D WITH_OPENCL=OFF \
-D CUDA_ARCH_BIN=8.7 \
-D CUDA_ARCH_PTX="sm_87" \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D WITH_CUBLAS=ON \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D OPENCV_DNN_CUDA=ON \
-D ENABLE_NEON=ON \
-D WITH_QT=OFF \
-D WITH_OPENMP=ON \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=ON \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_PROTOBUF=ON \
-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 ..
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D EIGEN_INCLUDE_PATH=/usr/include/eigen3 \
-D WITH_OPENCL=OFF \
-D CUDA_ARCH_BIN=8.7 \
-D CUDA_ARCH_PTX="sm_87" \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D WITH_CUBLAS=ON \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D OPENCV_DNN_CUDA=ON \
-D ENABLE_NEON=ON \
-D WITH_QT=OFF \
-D WITH_OPENMP=ON \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=ON \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-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 ..
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D EIGEN_INCLUDE_PATH=/usr/include/eigen3 \
-D WITH_OPENCL=OFF \
-D CUDA_ARCH_BIN=8.7 \
-D CUDA_ARCH_PTX="sm_87" \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D WITH_CUBLAS=ON \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D OPENCV_DNN_CUDA=ON \
-D ENABLE_NEON=ON \
-D WITH_QT=OFF \
-D WITH_OPENMP=ON \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=ON \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-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 ..
Make.
With all compilation directives in place, you can start the build with the following command. This will take about three hours!
$ make -j4
After minimal an hour and a half, your build should be ready and you get the screen below.

Make Rdy

After a successful compilation, you can remove the old packages and install all newly generated packages in the database of your system with the following commands.
$ sudo rm -r /usr/include/opencv4/opencv2
$ sudo make install
$ sudo ldconfig
# cleaning (frees 300 MB)
$ make clean
$ sudo apt-get update

Checking.

Now it is time to check your installation. It can be done in a fast way by using Python. Use the commands shown in the screen dump below. It all speaks for itself.
OpenCV Orin Success
OpenCV will be installed to the /usr directory, all files will be copied to following locations:
  • /usr/bin - executable files
  • /usr/lib/aarch64-linux-gnu - libraries (.so)
  • /usr/lib/aarch64-linux-gnu/cmake/opencv4 - cmake package
  • /usr/include/opencv4 - headers
  • /usr/share/opencv4 - other files (e.g. trained cascades in XML format)

Cleaning.

If you are in shortage of disk space, you may consider deleting the ~/opencv and ~/opencv_contrib folders. By the command, sudo make install, you have copied all the headers and libraries to their appropriate locations on your disk. The original files in the ~/opencv/build/ folder are no longer needed. Of course, if you have, for instance, enabled the building of examples, you obviously cannot delete the folders as this will also remove these samples. Or, if you're planning to use the Haar Cascades scripts used in the OpenCV Face Detection module, as they are located in the ~/opencv/data/haarcascades folder. Let's say it's just a tip saving about 1.5 GByte of space.
# just a tip to save an additional 275 MB
$ sudo rm -rf ~/opencv
$ sudo rm -rf ~/opencv_contrib

JTOP.

You probably already have jtop installed. If not, you should do it right away. An excellent debug tool by Raffaello Bonghi especially made for the Jetson family. It gives you all the information you are looking for during development, just like htop. You can install it with the following command.
$ sudo -H pip install -U jetson-stats
$ sudo reboot
# start the app with the simple command
$ jtop
Orin with CUDA OpenCV
images/GithubSmall.png
images/YouTubeSmall.png
images/SDcardSmall.png
Back to content