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

Install OpenCV 4.5 on Jetson Nano

Last updated: April 29, 2023

Introduction.

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?
Tip

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!
OpenCV 4.5.1.
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.
OpenCV 4.5.2.
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.
OpenCV 4.5.3.
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.
OpenCV 4.5.4.
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.
OpenCV 4.5.5.
On Decemeber 25, 2021, OpenCV released version 4.5.5. Again the larges changes are improvements of the DNN module. Of course there are bug fixes and improvements. More info here.
As of Python 3.10, the /usr/lib/python3/dist-packages directory of the libraries is deprecated. Python 3.12 removes the entire location.
Somewhat over-enthusiastically, the installation script has already been modified for this new location, preventing the Nano's Python 3.6 from finding OpenCV. The libraries are saved in the correct directory by adding -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages to the build. Here is the bug report on GitHub.
OpenCV 4.6.0.
On June 7, 2022, OpenCV released version 4.6.0. As usual, most of the changes and improvements can be found in the DNN module, apart from the bug fixes. More info here.
The location of the Python libraries is still an issue. Adding -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages to the build remains the solution for now. We have submitted a merge request to OpenCV to resolve this issue.
Tips.
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.
gdm2 desktop
lightdm desktop
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.
FullOpenCV
SimpleOpenCV

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, 4.5.4 and 4.5.5.
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 and higher)
$ sudo nano /sbin/dphys-swapfile
# give the required memory size
$ sudo nano /etc/dphys-swapfile
# reboot afterwards
$ sudo reboot
Please follow the steps in the side show below.
Free -m

Installation script.

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.
# check your memory first
$ free -m
# you need at least a total of 8.5 GB!
# if not, enlarge your swap space as explained in the guide
$ 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
# remove the dphys-swapfile now
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo apt-get remove --purge dphys-swapfile
# just a tip to save an additional 275 MB
$ sudo rm -rf ~/opencv
$ sudo rm -rf ~/opencv_contrib
# check your memory first
$ free -m
# you need at least a total of 8.5 GB!
# if not, enlarge your swap space as explained in the guide
$ wget https://github.com/Qengineering/Install-OpenCV-Jetson-Nano/raw/main/OpenCV-4-5-5.sh
$ sudo chmod 755 ./OpenCV-4-5-5.sh
$ ./OpenCV-4-5-5.sh
# once the installation is done...
$ rm OpenCV-4-5-5.sh
# remove the dphys-swapfile now
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo apt-get remove --purge dphys-swapfile
# just a tip to save an additional 275 MB
$ sudo rm -rf ~/opencv
$ sudo rm -rf ~/opencv_contrib
# check your memory first
$ free -m
# you need at least a total of 8.5 GB!
# if not, enlarge your swap space as explained in the guide
$ wget https://github.com/Qengineering/Install-OpenCV-Jetson-Nano/raw/main/OpenCV-4-5-4.sh
$ sudo chmod 755 ./OpenCV-4-5-4.sh
$ ./OpenCV-4-5-4.sh
# once the installation is done...
$ rm OpenCV-4-5-4.sh
# remove the dphys-swapfile now
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo apt-get remove --purge dphys-swapfile
# just a tip to save an additional 275 MB
$ sudo rm -rf ~/opencv
$ sudo rm -rf ~/opencv_contrib
# check your memory first
$ free -m
# you need at least a total of 8.5 GB!
# if not, enlarge your swap space as explained in the guide
$ wget https://github.com/Qengineering/Install-OpenCV-Jetson-Nano/raw/main/OpenCV-4-5-3.sh
$ sudo chmod 755 ./OpenCV-4-5-3.sh
$ ./OpenCV-4-5-3.sh
# once the installation is done...
$ rm OpenCV-4-5-3.sh
# remove the dphys-swapfile now
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo apt-get remove --purge dphys-swapfile
# just a tip to save an additional 275 MB
$ sudo rm -rf ~/opencv
$ sudo rm -rf ~/opencv_contrib
# check your memory first
$ free -m
# you need at least a total of 8.5 GB!
# if not, enlarge your swap space as explained in the guide
$ wget https://github.com/Qengineering/Install-OpenCV-Jetson-Nano/raw/main/OpenCV-4-5-2.sh
$ sudo chmod 755 ./OpenCV-4-5-2.sh
$ ./OpenCV-4-5-2.sh
# once the installation is done...
$ rm OpenCV-4-5-2.sh
# remove the dphys-swapfile now
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo apt-get remove --purge dphys-swapfile
# just a tip to save an additional 275 MB
$ sudo rm -rf ~/opencv
$ sudo rm -rf ~/opencv_contrib
# check your memory first
$ free -m
# you need at least a total of 6.5 GB!
# if not, enlarge your swap space as explained in the guide
$ wget https://github.com/Qengineering/Install-OpenCV-Jetson-Nano/raw/main/OpenCV-4-5-1.sh
$ sudo chmod 755 ./OpenCV-4-5-1.sh
$ ./OpenCV-4-5-1.sh
# once the installation is done...
$ rm OpenCV-4-5-1.sh
# remove the dphys-swapfile now
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo apt-get remove --purge dphys-swapfile
# just a tip to save an additional 275 MB
$ sudo rm -rf ~/opencv
$ sudo rm -rf ~/opencv_contrib
# check your memory first
$ free -m
# you need at least a total of 6.5 GB!
# if not, enlarge your swap space as explained in the guide
$ wget https://github.com/Qengineering/Install-OpenCV-Jetson-Nano/raw/main/OpenCV-4-5-0.sh
$ sudo chmod 755 ./OpenCV-4-5-0.sh
$ ./OpenCV-4-5-0.sh
# once the installation is done...
$ rm OpenCV-4-5-0.sh
# remove the dphys-swapfile now
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo apt-get remove --purge dphys-swapfile
# 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 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

Qt5.

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.
No Qt5
With 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 ~
$ 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
# download the latest version
$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.5.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.5.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.5.5 opencv
$ mv opencv_contrib-4.5.5 opencv_contrib
# clean up the zip files
$ rm opencv.zip
$ rm opencv_contrib.zip
# download the latest version
$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.4.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.4.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.5.4 opencv
$ mv opencv_contrib-4.5.4 opencv_contrib
# clean up the zip files
$ rm opencv.zip
$ rm opencv_contrib.zip
# download the latest version
$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.3.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.3.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.5.3 opencv
$ mv opencv_contrib-4.5.3 opencv_contrib
# clean up the zip files
$ rm opencv.zip
$ rm opencv_contrib.zip
# download the latest version
$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.2.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.2.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.5.2 opencv
$ mv opencv_contrib-4.5.2 opencv_contrib
# clean up the zip files
$ rm opencv.zip
$ rm opencv_contrib.zip
# download the latest version
$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.1.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.1.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.5.1 opencv
$ mv opencv_contrib-4.5.1 opencv_contrib
# clean up the zip files
$ rm opencv.zip
$ rm opencv_contrib.zip
# download the latest version
$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.0.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.0.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.5.0 opencv
$ mv opencv_contrib-4.5.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 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.
$ 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 WITH_CUDA=ON \
-D CUDA_ARCH_BIN=5.3 \
-D CUDA_ARCH_PTX="" \
-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 WITH_CUDA=ON \
-D CUDA_ARCH_BIN=5.3 \
-D CUDA_ARCH_PTX="" \
-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 WITH_CUDA=ON \
-D CUDA_ARCH_BIN=5.3 \
-D CUDA_ARCH_PTX="" \
-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 BUILD_opencv_python3=TRUE \
-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 WITH_CUDA=ON \
-D CUDA_ARCH_BIN=5.3 \
-D CUDA_ARCH_PTX="" \
-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 BUILD_opencv_python3=TRUE \
-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 WITH_CUDA=ON \
-D CUDA_ARCH_BIN=5.3 \
-D CUDA_ARCH_PTX="" \
-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 BUILD_opencv_python3=TRUE \
-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 WITH_CUDA=ON \
-D CUDA_ARCH_BIN=5.3 \
-D CUDA_ARCH_PTX="" \
-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 BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=TRUE \
-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 WITH_CUDA=ON \
-D CUDA_ARCH_BIN=5.3 \
-D CUDA_ARCH_PTX="" \
-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 BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=TRUE \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF ..
Hopefully, everything went well and CMake comes with a report that looks something like the screenshot below.

CMake OpenCV ready

Make.

With all compilation directives in place, you can start the build with the following command. This will take about two 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.

Succes_4_6

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.
In any case, delete the SD memory swap software installed at the beginning of this manual.
# remove the dphys-swapfile now
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo apt-get remove --purge dphys-swapfile
# free the allocated memory
$ sudo rm /var/swap
# just a tip to save an additional 275 MB
$ sudo rm -rf ~/opencv
$ sudo rm -rf ~/opencv_contrib

cv : : dnn : : net.

OpenCV comes with an excellent deep learning framework module. Without any additional installations, you can run TensorFlow, Caffe, ONNX, PyTorch or Darknet models. See this OpenCV page for more information.
Now that we've installed OpenCV with CUDA support, it's interesting to see the performance gains this brings to the deep learning models. Several tests have been carried out for this purpose. The results are shown in the table below.
ModelCPU
2 GHz
CPU + CUDA
2 GHz
CPU
1.47 GHz
CPU + CUDA
1.47 GHz
Raspberry Pi 4
1.9 GHz
UltraFace
1.1 MByte
48 FPS
35 FPS
37 FPS
31 FPS
28.8 FPS
MobileNetV1_SSD   
28 MByte
8.1 FPS
13.3 FPS6.3 FPS
10.0 FPS
5.6 FPS
GoogleLeNet
51 MByte
6.25 FPS21.7 FPS4.7 FPS20.4 FPS5 FPS
MobileNetV2_SSD   
66 MByte
4.5 FPS
8.0 FPS
3.52 FPS
6.7 FPS
3.2 FPS
ResNet50
98 MByte
2.7 FPS
13 FPS
2.1 FPS12.35 FPS2 FPS
VGG16
528 MByte
0.7 FPS4.35 FPS0.52 FPS4.17 FPS0.52 FPS
YOLOV4 (416x416)
245 MByte
0.35 FPS1.94 FPS0.26 FPS1.84 FPS0.46 FPS
The first to columns are the results with an overclocked Jetson Nano. The second two are the outcomes with Jetson Nano with the regular clock speed. The last column is added for comparison reasons, an overclocked Raspberry Pi 4.
Also note that the larger the model, the more effect CUDA has. A very tiny model like UltraFace becomes even slower. It has to do with the relatively time-consuming memory swapping between CPU and GPU. The more tensors the GPU can process at one time, the better the performance boost.

To enable CUDA support for your network, two additional lines of code are required. Without these lines, OpenCV falls to its default CPU mode.
Note also some time elapse before your application comes to live. That has to do with the rather time-consuming heuristic search in the background for the best tensor calculation strategy.

						
						#include <opencv2/dnn/dnn.hpp>
						
						.....
						    // load some network
						    net = cv::dnn::readNetFromCaffe("ResNet-50.prototxt", "ResNet-50.caffemodel");
						    
						    // check
						    if(net.empty()){
						        std::cout << "init the model net error" << std::endl;
						        return -1;
						    }
						
						    // enable CUDA support
						    net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
						    net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
						.....
						

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
JTOP



Jtop_OpenCV

Jetson family.


Finally, we would like you to give an overview of all the single board computers NVIDIA currently has.
Please note that the listed price is from January 2023, in the middle of the global chip shortage.
none
Jetson Nano 2 GByte
4x Cortex-A57 CPU
128x CUDA
1.88 TOPS
1.43 GHz - 2 GB


(sold out)

Striped version to compete with the Raspberry. One USB 3 port, a single camera and 2 GByte of RAM. A good start for your first deep learning steps, as long as your models fit the 2 GB.
none
Jetson Nano B01
4x Cortex-A57 CPU
128x CUDA
1.88 TOPS
1.43 GHz - 4 GB
€ 216

Single board computer with CUDA acceleration and two camera ports, which makes it ready for binocular applications like stereo recording, depth sensing, 3D object tracking and image stitching.
OrinNano
Jetson Orin Nano 4GB
6x Cortex-A78 CPU
512x CUDA
20 TOPS
1.43 GHz - 4 GB

€ 200

The successor to the Jetson Nano. Except this board has a lot more AI power for almost the same price. If you want to start deep learning at the edge, here's your board.
none
Jetson Xavier
6x ARMv8-A64 CPU
384x CUDA
48x Tensor core
21 TOPS
2.2 GHz - 8 GB
€ 785 (partly sold out)

The Xavier is a high-performance single-board specialized for AI tasks. Impressive GPU acceleration enables complex deep learning models to run in real-time. Six camera inputs and many I/O makes it a real workhorse.
none
Jetson AGX Xavier
8x ARMv8-A64 CPU
512x CUDA
64x Tensor core
32 TOPS
2.2 GHz - 64 GB
€ 652 (sold out)

The AGX Xavier is a small supercomputer on a single board. It is suitable for the most demanding AI tasks. It is suited for deploying and training the most neural networks very fast.
none
Jetson AGX Orin
12x ARMv8-A64 CPU
2048x CUDA
64x Tensor cores
275 TOPS
2.2 GHz - 32 GB
€ 1999
The AGX Orion is the latest member of the ever-growing Jetson family. With twice the AI power of the Xavier, it is suitable for all AI and robotic tasks.
Raspberry 64 OS
Raspberry 32 OS
Raspberry and alt
Raspberry Pi 4
Jetson Nano
images/GithubSmall.png
images/YouTubeSmall.png
images/SDcardSmall.png
Back to content