How to Compile OpenCV with CUDA Support on Ubuntu 20.04 for YOLO GPU Acceleration?

I’m relatively new to OpenCV and I’m looking for some guidance on how to compile the latest version of OpenCV with CUDA support on Ubuntu 20.04. I intend to run YOLO on my PC using the GPU instead of the CPU.

The problem: After a lot of research I’ve been unable to find clear, straightforward instructions for compiling OpenCV with CUDA on Ubuntu 20.04, and then configure my environment to use this version of OpenCV.

While I’m aware that the Python library can be installed directly, it unfortunately isn’t built with the CUDA modules that I need.

My environment:

OS: Ubuntu 20.04

nvcc --version nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2024 NVIDIA Corporation Built on Thu_Mar_28_02:18:24_PDT_2024 Cuda compilation tools, release 12.4, V12.4.131 Build cuda_12.4.r12.4/compiler.34097967_0

nvidia-smi Tue Apr 30 18:25:09 2024
±----------------------------------------------------------------------------------------+ | NVIDIA-SMI 550.54.15 Driver Version: 550.54.15 CUDA Version: 12.4 | |-----------------------------------------±-----------------------±---------------------+ | GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |=========================================+========================+======================| | 0 NVIDIA GeForce RTX 3050 … Off | 00000000:01:00.0 Off | N/A | | N/A 43C P3 N/A / 35W | 12MiB / 4096MiB | 0% Default | | | | N/A | ±----------------------------------------±-----------------------±---------------------+

±----------------------------------------------------------------------------------------+ | Processes: | | GPU GI CI PID Type Process name GPU Memory | | ID ID Usage | |=========================================================================================| | 0 N/A N/A 2118 G /usr/lib/xorg/Xorg 4MiB | | 0 N/A N/A 3195 G /usr/lib/xorg/Xorg 4MiB | ±----------------------------------------------------------------------------------------+

I found in the official OpenCV documentation “Installation in Linux” which covers installation on Ubuntu 18.04 and doesn’t mention anything about the flags needed for compiling with CUDA support or how to use the compiled version later on.

The documentation on the official repo leads to the openCV official documentatio.

I also found this tutorial but it’s 4 years old and it’s for ubuntu 18.04

The question is how to compile OpenCV with CUDA on Ubuntu 20.04 or where to find detailed official documentation?