Install OpenCV with CUDA support on Jetson Boards

From RidgeRun Developer Wiki


Introduction to OpenCV with CUDA support on Jetson

This wiki page will guide the reader on how to install OpenCV with CUDA support on a Jetson Tegra platform. The steps are done natively on the device and not on the host.

System configuration

  • JetPack 4.3, L4T 32.3.1
  • Installed the whole JetPack 4.3 with GStreamer, default OpenCV and others, but without DeepStream support.
  • Performed a sudo apt update and upgrade after the JetPack install.

Remove preinstalled OpenCV form the Tegra

sudo apt purge libopencv-dev libopencv-python libopencv-samples libopencv*
sudo apt update

The names of the above libraries can be different depending on the JetPack used.

Download OpenCV

Clone the main repository and change the branch accordingly:

git clone https://github.com/opencv/opencv.git
cd opencv/
git checkout 4.1.0

In case you require the extra modules of OpenCV, you can do:

git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib/
git checkout 4.1.0

Configure and build OpenCV's cmake

Go to the opencv downloaded directory and create a build dir:

cd opencv
mkdir build && cd build

And execute the following cmake command:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D WITH_CUDA=ON \
    -D INSTALL_C_EXAMPLES=OFF \
    -D WITH_GSTREAMER=ON \
    -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D BUILD_TESTS=OFF \
    -D BUILD_PERF_TESTS=OFF \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D PYTHON_EXECUTABLE=$(which python3) \
    -D BUILD_opencv_python2=OFF \
    -D PYTHON3_EXECUTABLE=$(which python3) \
    -D PYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
    -D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
    -D OPENCV_GENERATE_PKGCONFIG=YES ..

The flag CUDA_ARCH_BIN was retrieved from the next table:

Table 1. Jetson Compute Capabilities related to its architecture (CUDA_ARCH_BIN)
GPU Compute Capability
Jetson AGX Xavier 7.2
Jetson TX2 6.2
Jetson TX1 5.3
Jetson Nano 5.3

In this case, a Jetson TX2 was used, therefore the number 6.2.

Once the cmake configuration succeed, then build and install using:

make -j8
sudo make install
sudo ldconfig