Build and Install OpenCV 4.5.3–4.6.0 on Jetson Nano with CUDA, OpenCL, OpenGL and GStreamer Enable

Muhammad Yunus
3 min readSep 19, 2021

--

After previously we are successfully build and install OpenCL on Jetson Nano via PoCL with CUDA backend in here. Now, I want to show you how to build OpenCV with CUDA (+ cuDNN), OpenCL, OpenGL and GStreamer enable.

NVIDIA CUDA with cuDNN is important for OpenCV CUDA module and OpenCV DNN with CUDA backend to work. Then, OpenCL important for T-API backend and as a kernel for G-API. Then, OpenGL important for optimizing image rendering on OpenCV highgui module with QT or GTK backend. And then, GStreamer is important for accelerated video decoding using NVIDIA accelerated GStreamer element.

General Information

Board   : Jetson Nano (B01)
Arc : aarch64
OS : Ubuntu 18.04 LTS
Jetpack : 4.5 (L4T 32.5) -> 4.5.1 (L4T 32.7.2)
Kernel : GNU/Linux 4.9.201-tegra -> 4.9.253-tegra
CUDA : 10.2 (cuDNN 8.0)
GCC : 7.5.0 (G++ 7.5.0)

Prerequisites :

1. Install PoCL

  • Install OpenCL 1.2 (via PoCL 1.7 or 3.0) on Jetson Nano by following this tutorial,

2. Configure OpenGL

  • Install Qt-4,
sudo apt-get install qt4-default

don’t use Qt-5, it will give you black image when imshow with flag cv2.WINDOW_OPENGL

  • Patch OpenGL Header by creating OpenGLHeader.patch file with the following content,
  • Then apply patch to OpenGL Header in /usr/local/cuda-10.2/targets/aarch64-linux/include/cuda_gl_interop.h with the following command ,
sudo patch -N /usr/local/cuda-10.2/targets/aarch64-linux/include/cuda_gl_interop.h OpenGLHeader.patch
  • Then you should see the patch result success like below,
patching file /usr/local/cuda-10.2/targets/aarch64-linux/include/cuda_gl_interop.h
patch unexpectedly ends in middle of line
Hunk #1 succeeded at 58 with fuzz 2 (offset -1 lines).
  • After that, create symbolic link for libGL,
cd /usr/lib/aarch64-linux-gnu/ 
sudo ln -sf libGL.so.1.0.0 libGL.so

Download & Build OpenCV 4.5.3 -> 4.6.0

  • Create installation script with name build_opencv_4.5.3.sh and put the following script,
  • Update 18 December 2022 : For OpenCV 4.5.5 and 4.6.0 we need extra Cmake config -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages. For the detail please check this tutorial.
  • Then execute that script with the following command,
cd ~
sudo chmod +x build_opencv_4.5.3.sh
./build_opencv_4.5.3.sh
  • Build process will take several hours to complete,
build process
  • Above script will automatically installing dependency libraries, configuring OpenCV using cmake, building & installing.
  • This is build configuration from my side,
  • On above build configuration, we can see that OpenCV able to recognize CUDA with CuDNN, OpenCL, OpenGL with Qt-4 backend, and GStreamer 1.0.

Additional Note :

Add Swap

  • By default Jetson Nano use 2GB zram, this is advisable to you to add more swap into Jetson Nano to avoid out of memory during compilation.
  • After add 4GB extra swap from external drive, the compilation process continue without worried about out of memory.
htop profile after addition 4GB swap memory

Error Importing OpenCV in Jupyter Notebook

  • If you facing error like below when importing OpenCV through Jupyter Notebook,
aarch64: libgomp.so.1: cannot allocate memory in static TLS block
  • Then you need to add the following line into your Jetson Nano ~/.bashrc ,
export LD_PRELOAD=/usr/lib/aarch64-linux-gnu/libgomp.so.1

Source :

--

--

Muhammad Yunus
Muhammad Yunus

Written by Muhammad Yunus

IoT Engineer, Software Developer & Machine Learning Enthusiast