The NVIDIA Jetson Xavier NX computer ships with a version of OpenCV that does not use the GPU. This means that the computer does not use some of its features like cuDNN for Neural Network algorithms. To enable CUDA and full GPU support, OpenCV can be build from source.
Pre-installation
Remove the old version of OpenCV.
$ sudo apt-get purge *libopencv*
Update all packages.
$ sudo apt-get update
$ sudo apt-get upgrade
Make sure CUDA can be found.
$ sudo sh -c "echo '/usr/local/cuda/lib64' >> /etc/ld.so.conf.d/nvidia-tegra.conf"
$ sudo ldconfig
Install all thrid party libraries that OpenCV depends on.
$ 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 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
Download OpenCV
Download OpenCV 4.5.2 from Github.
$ 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
Unzip the folders, rename them and clean up the zipped folders.
$ unzip opencv.zip
$ unzip opencv_contrib.zip
$ mv opencv-4.5.2 opencv
$ mv opencv_contrib-4.5.2 opencv_contrib
$ rm opencv.zip
$ rm opencv_contrib.zip
Build OpenCV
Create the building directory.
$ cd ~/opencv
$ mkdir build
$ cd build
Configure CMake to build OpenCV the way you want it. These flags can be changed to adapt the installation to your needs.
$ 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 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 CMAKE_C_COMPILER=/usr/bin/gcc-7 \
-D BUILD_EXAMPLES=OFF ..
If the configuration went well, you should see a report and a message that the configuration is done. You are now ready to build OpenCV.
$ make -j$(nproc)
This step will take one to two hours and might sometimes look frozen. Do not stop the process before the build is 100% completed and you see the prompt back. Now you can install the libraries that you just built and remove the build folders.
$ sudo make install
$ sudo ldconfig
$ make clean
$ sudo rm -rf ~/opencv
$ sudo rm -rf ~/opencv_contrib
You should now have OpenCV 4.5.2 installed on you companion computer. To test that your installation, you can use Python.
$ python3
>>>import cv2
>>>cv2.__version__
>>>exit()