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.0 on Jetson Nano

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.
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.
Original
General configuration for OpenCV 4.1.1 =====================================
 Version control:               4.1.1-2-gd5a58aa75






 Platform:
   Timestamp:                   2019-12-13T17:25:11Z
   Host:                        Linux 4.9.140-tegra aarch64

**********

OpenCV modules:
   To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python2 python3 stitching ts video videoio







**********

Video I/O:

   FFMPEG:                      YES
     avcodec:                   YES (57.107.100)
     avformat:                  YES (57.83.100)
     avutil:                    YES (55.78.100)
     swscale:                   YES (4.8.100)
     avresample:                NO
   GStreamer:                   YES (1.14.5)
   v4l/v4l2:                    YES (linux/videodev2.h)

**********











Install to:                    /usr
Reinstalled
General configuration for OpenCV 4.5.0 =====================================
 Version control:               unknown

 Extra modules:
   Location (extra):            /home/jetson/opencv_contrib/modules
   Version control (extra):     unknown

 Platform:
   Timestamp:                   2020-10-14T10:57:53Z
   Host:                        Linux 4.9.140-tegra aarch64

**********

OpenCV modules:
   To be built:                 alphamat aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor ml objdetect optflow phase_unwrapping photo plot python2 python3 quality rapid reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto

**********

Video I/O:
   DC1394:                      YES (2.2.5)
   FFMPEG:                      YES
     avcodec:                   YES (57.107.100)
     avformat:                  YES (57.83.100)
     avutil:                    YES (55.78.100)
     swscale:                   YES (4.8.100)
     avresample:                YES (3.7.0)
   GStreamer:                   YES (1.14.5)
   v4l/v4l2:                    YES (linux/videodev2.h)

**********

 NVIDIA CUDA:                   YES (ver 10.2, CUFFT CUBLAS FAST_MATH)
   NVIDIA GPU arch:             53
   NVIDIA PTX archs:

 cuDNN:                         YES (ver 8.0.0)

 OpenCL:                        YES (no extra features)
   Include path:                /home/jetson/opencv/3rdparty/include/opencl/1.2
   Link libraries:              Dynamic load

Install to:                    /usr

Enlarge memory swap.
Building the full OpenCV 4.5.0 package requires more than 4 Gbytes of RAM on your Jetson Nano and the extra 2 Gbytes of swap space delivered by zram. 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.
# 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
# give the required memory size
$ sudo nano /etc/dphys-swapfile
# reboot afterwards
$ sudo reboot.
Swap space

Dependencies.

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.
# third-party libraries
$ sudo apt-get install build-essential cmake git unzip pkg-config
$ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install libgtk2.0-dev libcanberra-gtk*
$ 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
$ sudo apt-get install libv4l-dev v4l-utils
$ sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
$ sudo apt-get install libavresample-dev libvorbis-dev libxine2-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 libeigen3-dev gfortran
$ sudo apt-get install libhdf5-dev protobuf-compiler
$ sudo apt-get install libprotobuf-dev 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. Check before downloading the latest version at https://opencv.org/releases/. If necessary, change the names of the zip files according to the latest version. 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.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
The next step is some administration. Rename your directories with more convenient names like opencv and opencv_contrib. This makes live later on easier.
$ 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_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 WITH_OPENGL=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. The final instructions will delete the SD memory swap software installed at the beginning of this manual.
$ sudo rm -r /usr/include/opencv4/opencv2
$ sudo make install
$ sudo ldconfig
$ sudo apt-get update
# remove the dphys-swapfile now
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo apt-get remove --purge dphys-swapfile

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_5

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. Let's say it's just a tip saving about 1.5 GByte of space.
# just a tip to save some space
$ 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
jtop1

jtop2_4_5

Jetson family.


Finally, we would like you to give an overview of all the single board computers NVIDIA currently has.

Jetson Nano 2 GByte
4x Cortex-A57 CPU
128x CUDA
1.88 TOPS
1.43 GHz - 2 GB


€ 50

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

Single board computer like a Raspberry with special hardware tensor acceleration by the floating point CUDA's
Jetson nano B2
Jetson Nano B01
4x Cortex-A57 CPU
128x CUDA
1.88 TOPS
1.43 GHz - 4 GB
€ 110

Identical to the Jetson Nano A02 board, except it has two camera ports, which makes it ready for binocular applications like stereo recording, depth sensing, 3D object tracking and image stitching.
Jetson TX2
Jetson TX 2
2x ARMv8-A64 + 4x Cortex-A57 CPU
256x CUDA
5.32 TOPS
1.5 GHz - 8 GB

€ 429

Jetson TX2 was the mid-range AI single-board solution of NVIDIA. With a powerful multi-core CPU and Pascal GPU, it can perform many deep learning tasks. It's replaced by its successor, the Jetson Xavier.
Jetson Xavier
Jetson Xavier
6x ARMv8-A64 CPU
384x CUDA
84 TOPS
2.2 GHz - 8 GB
€ 429

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.
Jetson AGX Xavier
Jetson AGX Xavier
8x ARMv8-A64 CPU
512x CUDA
128 TOPS
2.2 GHz - 8 GB
€ 749

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.
Install 64 OS
Install 32 OS
Raspberry and alt
Raspberry Pi 4
Jetson Nano
Back to content