Compile OpenCV with Cuda from the source.
Alight, so you have the NVIDIA CUDA Toolkit and cuDNN library installed on your GPU-enabled system.
What next?

Prerequisites for Unix
Tested on Ubuntu 16.04 and 18.04 (NVIDIA 2080Ti and 1080TI)
Install the dependencies via apt
.
sudo apt update -y
sudo apt upgrade -y --no-install-recommends
sudo apt install -y build-essential pkg-config cmake git wget curl unzip
sudo apt install -y libgtk-3-dev
sudo apt install -y ffmpeg
sudo apt install -y libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libavresample-dev
sudo apt install -y libyaml-cpp-dev libgoogle-glog-dev libgflags-dev
Install the following dependencies (don’t worry if they do not work.)
sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt-get install libgtk2.0-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install libhdf5-serial-dev
sudo apt-get install python2.7-dev
For this tutorial, I’ll be using OpenCV 3.4. But you could also use OpenCV 3.X or OpenCV 3.0. Use the following commands to download the source.
Execute the following cmd in the terminal.

wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.4.0.zip
2. wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.4.0.zip
3. unzip opencv.zip
4. unzip opencv_contrib.zip
We are now ready to use cmake
to configure our build. Take special care when running this command, as I’m introducing some configuration variables you may not be familiar with:
5. cd opencv-3.4.0
6. mkdir build
7. cd build
9. cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.0/modules -D BUILD_EXAMPLES=ON -DBUILD_opencv_cudacodec=OFF ..
cmake
will take around 5 min to build all OpenCV and Cuda NVCC library. after this execute the following command.
10. type nproc
in the terminal, it will display the number of cores in your system. for me it is 12
. so in the next step, I will use this 12.
11. make -j12
make -j12 will take time around 40 to 45 minutes to compile all OpenCV and Cuda library.
12. sudo make install
13. sudo ldconfig -v
Done! OpenCV is compiled with Cuda, you are ready to go now.