35

I installed cuda first using cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64.deb. Now I'm trying to install OpenCV 3.3.0 But i'm getting CMake Error:

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CUDA_nppi_LIBRARY (ADVANCED)

And then a very long list of targets like so:

linked by target "opencv_cudev" in directory /home/jjros/opencv-3.3.0/modules/cudev

I'm using this command to compile the library:

cmake 

-D CMAKE_C_COMPILER=/usr/bin/gcc-5 \ 
-D CMAKE_BUILD_TYPE=RELEASE \   
-D CMAKE_INSTALL_PREFIX=/usr/local \     
-D WITH_CUDA=ON \     
-D WITH_CUBLAS=ON \     
-D WITH_TBB=ON \    
-D WITH_V4L=ON \    
-D WITH_QT=ON \     
-D WITH_OPENGL=ON \    
-D ENABLE_FAST_MATH=1 \        
-D CUDA_FAST_MATH=1 \        
-D WITH_CUBLAS=1 \        
-D INSTALL_C_EXAMPLES=OFF \    
-D INSTALL_PYTHON_EXAMPLES=ON \        
-D BUILD_SHARED_LIBS=ON \        
-D WITH_GTK=ON \        
-D BUILD_EXAMPLES=ON \     
-D  CUDA_NVCC_FLAGS="-D_FORCE_INLINES" .. 

How can set my CMakeLists? What's going wrong?

Ja_cpp
  • 2,426
  • 7
  • 27
  • 49
  • 1
    OpenCV is not updated to cuda 9... which was release like a week ago. In cuda 9 they separated this library into smaller ones. Some people create a library [that has all of the smaller ones](https://stackoverflow.com/questions/45525377/installing-opencv-3-3-0-with-contrib-modules-using-cmake-cuda-9-0-rc-and-visual). [Here](https://devtalk.nvidia.com/default/topic/1024631/cmake-cuda-9-project-configurations-fails-cuda_nppi_library-is-not-available-/) someone try to modify the cmake files, but got another error relative to a missing math variable – api55 Oct 05 '17 at 11:10
  • 1
    Also fix slash on this line: `-D WITH_GTK=ON / ` – Ivan Aksamentov - Drop Oct 05 '17 at 16:26
  • @api55 so what do you suggest me to install, which versions of opencv and cuda to install and which one to install first? I'm using ubuntu 16.04 – Ja_cpp Oct 06 '17 at 10:37
  • 1
    I think with CUDA 8.0 should work, or you try to modify the cmake, by adding all the libraries which the nppi library divided to and removing the nppi library. I will try the latter today, if it works I will answer the question in detail. However I would be using Windows instead of Ubuntu for my test, but it should be the same fix – api55 Oct 06 '17 at 11:18

11 Answers11

73

I tried the following and it worked:

Change in FindCUDA.cmake the nppi library to the several splitted ones. This has to be done in 3 places. Remember this change is just to make it work with CUDA 9.0, I am not doing checks for version or anything, which should be done if you plan to give it to different people with different CUDA versions.

1) look for the line with:

find_cuda_helper_libs(nppi)

and replace it with the lines:

  find_cuda_helper_libs(nppial)
  find_cuda_helper_libs(nppicc)
  find_cuda_helper_libs(nppicom)
  find_cuda_helper_libs(nppidei)
  find_cuda_helper_libs(nppif)
  find_cuda_helper_libs(nppig)
  find_cuda_helper_libs(nppim)
  find_cuda_helper_libs(nppist)
  find_cuda_helper_libs(nppisu)
  find_cuda_helper_libs(nppitc)

2) find the line:

set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}")

and change it to

set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};${CUDA_nppig_LIBRARY};${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY};${CUDA_npps_LIBRARY}")

3) find the unset variables and add the new variables as well So, find:

unset(CUDA_nppi_LIBRARY CACHE)

and change it to:

unset(CUDA_nppial_LIBRARY CACHE)
unset(CUDA_nppicc_LIBRARY CACHE)
unset(CUDA_nppicom_LIBRARY CACHE)
unset(CUDA_nppidei_LIBRARY CACHE)
unset(CUDA_nppif_LIBRARY CACHE)
unset(CUDA_nppig_LIBRARY CACHE)
unset(CUDA_nppim_LIBRARY CACHE)
unset(CUDA_nppist_LIBRARY CACHE)
unset(CUDA_nppisu_LIBRARY CACHE)
unset(CUDA_nppitc_LIBRARY CACHE)

And also in OpenCVDetectCUDA.cmake you have to remove the 2.0 architechture which is no longer supported.

It has:

  ...
  set(__cuda_arch_ptx "")
  if(CUDA_GENERATION STREQUAL "Fermi")
    set(__cuda_arch_bin "2.0")
  elseif(CUDA_GENERATION STREQUAL "Kepler")
    set(__cuda_arch_bin "3.0 3.5 3.7")
  ...

It should be:

  ...
  set(__cuda_arch_ptx "")
  if(CUDA_GENERATION STREQUAL "Kepler")
    set(__cuda_arch_bin "3.0 3.5 3.7")
  elseif(CUDA_GENERATION STREQUAL "Maxwell")
    set(__cuda_arch_bin "5.0 5.2")
  ...

Basically I removed the first if and the first elif turns to an if.

As mentionned by @matko It also has :

set(__cuda_arch_bin "2.0 3.0 3.5 3.7 5.0 5.2 6.0 6.1") 

Which should be changed to:

set(__cuda_arch_bin "3.0 3.5 3.7 5.0 5.2 6.0 6.1") 

One last thing it is needed. CUDA 9.0 has a separated file for the halffloat (cuda_fp16.h) now. This needs to be included in OpenCV.

From CUDA 9.0 manual:

Unsupported Features General CUDA ‣ CUDA library. The built-in functions __float2half_rn() and __half2float() have been removed. Use equivalent functionality in the updated fp16 header file from the CUDA toolkit.

To do this, you need to add:

#include <cuda_fp16.h>

in the header file

opencv-3.3.0\modules\cudev\include\opencv2\cudev\common.hpp

This are the basics for a definite patch for OpenCV. What it is missing, well as I told you before, I do not care about CUDA versions (it needs an IF). Also, CUDA 9.0 has a bunch of deprecated functions used by OpenCV ... this probably will be replaced by the OpenCV team at some point. It is also possible that one or more of the splitted libraries of nppi is not used.

Final recommendations: For this kind of complex cmakes with so many options you should use ccmake (sudo apt-get install cmake-curses-gui) to be able to change easily the variables or at least view the values, or a real GUI one.

For other people with windows and visual studio 7, I also had to change the CUDA_HOST_COMPILER variable, else you get a bunch of errors with cmd.exe exit with code 1 or something similar... it seems it couldn't get there with the autodetected one.

This worked for me with OpenCV 3.3 and CUDA 9.0 and Visual Studio 2017 with Windows 10. I think it should work also in Ubuntu, since the error and the changes are related to CUDA. I haven't tested it much, I compiled and run the some of the performance tests and all of them passed... So I think everything worked ok.

anaBad
  • 309
  • 2
  • 13
api55
  • 11,070
  • 4
  • 41
  • 57
  • 4
    I'm facing an error related to gpu architecture. I'm trying to solve it. It says: nvcc fatal : Unsupported gpu architecture 'compute_20' CMake Error at cuda_compile_generated_gpu_mat.cu.o.cmake:208 (message): Error generating /home/jjros/opencv-3.3.0/build/modules/core/CMakeFiles/cuda_compile.dir/src/cuda/./cuda_compile_generated_gpu_mat.cu.o – Ja_cpp Oct 09 '17 at 22:03
  • 2
    @Ja_cpp that is also in the answer, but probably the variable is never unset/reset, you can delete the build folder and start again, or you can set the variable deleting 2.0 from the cuda_arch_bin, since Fermi architecture is no longer supported in CUDA 9.0 – api55 Oct 10 '17 at 06:54
  • 1
    Really really helpful. Thanks for a detailed guide. You should blog it somewhere. – Jerry Ajay Oct 27 '17 at 21:38
  • Thanks for your answer, it saves me. – dan Nov 14 '17 at 12:15
  • Perfect, not asking how you did all those changes. But it worked, Thanks a lot for this :) – GPrathap Mar 04 '19 at 21:27
4

@api55 I based on this process can be completed but still encounter @Ja_cpp problem (CMake Error at cuda_compile_generated_gpu_mat.cu.o.cmake:208 (message)), in my process, but also need to add

in

OpenCVDetectCUDA.cmake

It has:

set(__cuda_arch_bin "2.0 3.0 3.5 3.7 5.0 5.2 6.0 6.1") 

change to:

set(__cuda_arch_bin "3.0 3.5 3.7 5.0 5.2 6.0 6.1")

It worked for me.

max ko
  • 321
  • 2
  • 5
4

Replacing FindCUDA.cmake and OpenCVDetectCUDA.cmake from https://github.com/opencv/opencv/tree/master/cmake (master branch- opencv 3.4.1) works for me for opencv_2.4.13 with NVIDIA TitanXP graphics card of Pascal Architecture and with cuda-9.0 on Ubuntu 16.04.

  • I tried that and I got these errors: `/usr/bin/ld: cannot find -lcudart /usr/bin/ld: cannot find -lnppc /usr/bin/ld: cannot find -lnppial /usr/bin/ld: cannot find -lnppicc /usr/bin/ld: cannot find -lnppicom /usr/bin/ld: cannot find -lnppidei /usr/bin/ld: cannot find -lnppif /usr/bin/ld: cannot find -lnppig /usr/bin/ld: cannot find -lnppim /usr/bin/ld: cannot find -lnppist ...` – Kasparov92 Aug 12 '18 at 23:57
  • @Kasparov92, I think you need to add your CUDA lib directory to LD_LIBRARY_PATH. you can do this by, add the file "cuda.conf" to the directory "/etc/ld.so.conf.d/", with the contents "/usr/local/cuda/lib" then run "sudo ldconfig" – A. B. M. Nazibullah Feb 10 '19 at 16:56
4

This worked for me on Ubuntu 18.04 with OpenCV 4.2.0 and CUDA 10.0

Apparently, I was missing symbolic links to these libraries, so CMake couldn't locate them:

sudo ln -s /usr/lib/x86_64-linux-gnu/libnppc.so.9.1 /usr/lib/x86_64-linux-gnu/libnppc.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libnppial.so.9.1 /usr/lib/x86_64-linux-gnu/libnppial.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libnppicc.so.9.1 /usr/lib/x86_64-linux-gnu/libnppicc.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libnppicom.so.9.1 /usr/lib/x86_64-linux-gnu/libnppicom.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libnppidei.so.9.1 /usr/lib/x86_64-linux-gnu/libnppidei.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libnppif.so.9.1 /usr/lib/x86_64-linux-gnu/libnppif.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libnppig.so.9.1 /usr/lib/x86_64-linux-gnu/libnppig.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libnppim.so.9.1 /usr/lib/x86_64-linux-gnu/libnppim.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libnppist.so.9.1 /usr/lib/x86_64-linux-gnu/libnppist.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libnppisu.so.9.1 /usr/lib/x86_64-linux-gnu/libnppisu.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libnppitc.so.9.1 /usr/lib/x86_64-linux-gnu/libnppitc.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libnpps.so.9.1 /usr/lib/x86_64-linux-gnu/libnpps.so

Update: Although the above command allowed me to run CMake successfully, I still wasn't able to build. sudo apt-get install nvidia-cuda-toolkit did the trick.

Eric Smith
  • 126
  • 1
  • 5
2

I also had to do the following in OpenCVDetectCUDA.cmake:

replace

    if(${CUDA_VERSION} VERSION_LESS "8.0")
       set(__cuda_arch_bin "3.0 3.5 3.7 5.0 5.2")
     else()
       set(__cuda_arch_bin "3.0 3.5 3.7 5.0 5.2 6.0 6.1")
     endif()

with

    if(${CUDA_VERSION} VERSION_LESS "8.0")
      set(__cuda_arch_bin "3.0 3.5 3.7 5.0 5.2")    
      set(CUDA_ARCH_BIN "3.0 3.5 3.7 5.0 5.2")
    else()
      set(__cuda_arch_bin "3.0 3.5 3.7 5.0 5.2 6.0 6.1")
      set(CUDA_ARCH_BIN "3.0 3.5 3.7 5.0 5.2 6.0 6.1")
    endif()
1

I had the same error installing with the next setup.

Ubuntu 18.04 cuda, cudnn, opencv 4.2 and 4.3. Have made several attempts to fix this and finally come out with the next working installation.

Important that no drivers have been installed yet. Have noticed that my installation only works from a clean installation of ubuntu

change the GPU_ARCH version for your situation. Check https://developer.nvidia.com/cuda-gpus for your version

export CUDA_VERSION='10.2.89'
export CUDA_PKG_VERSION='10-2=10.2.89-1'
export PATH=/usr/local/nvidia/bin:/usr/local/cuda/bin:${PATH}
export LD_LIBRARY_PATH=/usr/local/nvidia/lib:/usr/local/nvidia/lib64
export NCCL_VERSION='2.5.6'
export CUDNN_VERSION='7.6.5.32'
export OPENCV_VERSION='4.3.0'
export GPU_ARCH='6.1'

apt-get update  && \
apt-get install -y --no-install-recommends \
gnupg2 curl ca-certificates && \
    curl -fsSL https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub | apt-key add - && \
    echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/cuda.list && \
    echo "deb https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/nvidia-ml.list


apt-get update && \
apt-get install -y --no-install-recommends \
        cuda-cudart-$CUDA_PKG_VERSION \
cuda-compat-10-2 && \
ln -s cuda-10.2 /usr/local/cuda


apt-get update && \
apt-get install -y --no-install-recommends \
    cuda-libraries-$CUDA_PKG_VERSION \
cuda-nvtx-$CUDA_PKG_VERSION \
libcublas10=10.2.2.89-1 \
libnccl2=$NCCL_VERSION-1+cuda10.2 && \
    apt-mark hold libnccl2 


apt-get update && apt-get install -y --no-install-recommends \
    libcudnn7=$CUDNN_VERSION-1+cuda10.2 && \
    apt-mark hold libcudnn7


apt-get update && apt-get install -y --no-install-recommends \
        cuda-nvml-dev-$CUDA_PKG_VERSION \
        cuda-command-line-tools-$CUDA_PKG_VERSION \
cuda-libraries-dev-$CUDA_PKG_VERSION \
        cuda-minimal-build-$CUDA_PKG_VERSION \
        libnccl-dev=$NCCL_VERSION-1+cuda10.2 \
libcublas-dev=10.2.2.89-1


apt-get update && apt-get install -y --no-install-recommends \
    libcudnn7=$CUDNN_VERSION-1+cuda10.2 \
libcudnn7-dev=$CUDNN_VERSION-1+cuda10.2 && \
    apt-mark hold libcudnn7



apt update && \
    apt install -y \
    tzdata \
    git \
    cmake \
    wget \
    unzip \
    build-essential \
    zlib1g-dev \
    libjpeg-dev \
    libwebp-dev \
    libpng-dev \
    libtiff5-dev \
    libopenexr-dev \
    libgdal-dev \
    libgtk2.0-dev \
    libdc1394-22-dev \
    libavcodec-dev \
    libavformat-dev \
    libswscale-dev \
    libtheora-dev \
    libvorbis-dev \
    libxvidcore-dev \
    libx264-dev \
    yasm \
    libopencore-amrnb-dev \
    libopencore-amrwb-dev \
    libv4l-dev \
    libxine2-dev \
    libgstreamer1.0-dev \
    libgstreamer-plugins-base1.0-dev \
    libtbb-dev \
    libeigen3-dev \
    python3-dev \
    python3-tk \
    python3-numpy



wget https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip && \
    unzip ${OPENCV_VERSION}.zip && rm ${OPENCV_VERSION}.zip && \
    mv opencv-${OPENCV_VERSION} OpenCV && \
    cd OpenCV && \
    wget https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip && \
    unzip ${OPENCV_VERSION}.zip && \
    mkdir build && \
    cd build && \
    cmake \
      -D WITH_TBB=ON \
      -D CMAKE_BUILD_TYPE=RELEASE \
      -D BUILD_EXAMPLES=ON \
      -D WITH_FFMPEG=ON \
      -D WITH_V4L=ON \
      -D WITH_OPENGL=ON \
      -D WITH_CUDA=ON \
      -D WITH_GSTREAMER=ON \
      -D OPENCV_DNN_CUDA=ON \
      -D CUDA_ARCH_BIN=${GPU_ARCH} \
      -D CUDA_ARCH_PTX=${GPU_ARCH} \
      -D WITH_CUBLAS=ON \
      -D WITH_CUFFT=ON \
      -D WITH_EIGEN=ON \
      -D EIGEN_INCLUDE_PATH=/usr/include/eigen3 \
      -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-${OPENCV_VERSION}/modules/ \
      ..

make all -j$(nproc)
make
jpvdw
  • 11
  • 1
0

@api55 solution solves the errors related to CUDA_nppi_LIBRARY. So just follow the steps and it works (for me on Ubuntu 16.04 with opencv 3.3). Make sure your GPU architecture is supported. I've Fermi which is no longer supported. The solution is to install Cuda-8 with gcc-5 compiler when installing opencv 3.3 with cuda support.

Ja_cpp
  • 2,426
  • 7
  • 27
  • 49
0

Tried the way api555 said, but when cmake, it also build sm20, I did a little more, two ways:

1.in file OpenCVDetectCUDA.cmake, line 133, in the "foreach(ARCH IN LISTS ARCH_LIST)" loop,add "elseif(ARCH MATCHES "20")" between if and else.

2.in file OpenCVDetectCUDA.cmake, line 46, "set(_generations "Fermi" "kepler" "Maxwell" "Pascal")" just delete the "Fermi"

I prefer the second way, may it do a little help.

Vega
  • 27,856
  • 27
  • 95
  • 103
0

I met the same problem here when installing opencv4.2 with cuda.

My cmake version is 3.16.2 at first. However, this version doesn't support HTTP download so that you might meet the download fail problem.

So I re-installed my cmake to version 3.9, which support HTTP protocal and the download problem was solved.

But the new problem was the same as @ja_cpp mentioned above. (CUDA_nppi_LIBRARY (ADVANCED) not found when cmake)

I followed @api55's method and modified FindCUDA.cmake and OpenCVDetectCUDA.cmake, but the problem was still there.

And because I was installing opencv4.2 not opecv3 so I think that's why the modification didn't work.

The most tricky thing is, I updated back my cmake version to 3.16.2, and cmake again with non-modified files again, and it worked!

I used this command.

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_ENABLE_NONFREE=ON -D WITH_CUDA=ON -D WITH_CUDNN=ON -D OPENCV_DNN_CUDA=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D CUDA_ARCH_BIN=6.1 -D WITH_CUBLAS=1 -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D HAVE_opencv_python3=ON -D PYTHON_EXECUTABLE=~/pengwenchen/anaconda3/envs/pytorch/bin/python -D BUILD_EXAMPLES=ON ..

My environment: GPU-1080, cmake version 3.16.2, cuda version 10.0

Hope this answer could help those who are installing opencv4.2 with cuda. :)

0

I have installed OpenCV 2.4.13.7 (with CUDA, QT, FFMPEG, etc.) on jetson nano (JetPack 4.5.1) based on previous answers using the script below:

#!/bin/bash

#The script was written for JetPack 4.5.1

echo "** Install vnc"
apt-get update -y 
apt-get install vino -y

chmod 777 /etc/X11/xorg.conf

echo '
   Section "Screen"
   Identifier    "Default Screen"
   Monitor       "Configured Monitor"
   Device        "Tegra0"
   SubSection "Display"
       Depth    24
       Virtual 1280 800 # Modify the resolution by editing these values
       EndSubSection
EndSection' >> /etc/X11/xorg.conf

mkdir -p ~/.config/autostart
cp /usr/share/applications/vino-server.desktop ~/.config/autostart

dbus-launch gsettings set org.gnome.Vino prompt-enabled false 
dbus-launch gsettings set org.gnome.Vino require-encryption false    

dbus-launch gsettings set org.gnome.Vino authentication-methods "['vnc']"       
dbus-launch gsettings set org.gnome.Vino vnc-password $(echo -n 'thepassword'|base64) 

echo "** Install requirements"
apt-get update -y  
apt-get install nano -y
apt-get install libc-ares-dev -y  
apt-get install uuid-dev -y  
apt-get install libssl-dev -y  
apt-get install libcurl4-openssl-dev -y   
apt-get install git -y  
apt-get install build-essential -y  
apt-get install libgtk2.0-dev -y
apt-get install pkg-config -y
apt-get install libavcodec-dev -y 
apt-get install libavformat-dev -y 
apt-get install libswscale-dev -y
apt-get install python-dev -y 
apt-get install python-numpy -y 
apt-get install libtbb2 -y 
apt-get install libtbb-dev -y 
apt-get install libjpeg-dev -y 
apt-get install libpng-dev -y
apt-get install libtiff-dev -y
apt-get install libjasper-dev -y
apt-get install libdc1394-22-dev -y
apt-get install libv4l-dev -y 
apt-get install v4l2ucp -y 
apt-get install v4l-utils -y
apt-get install libtesseract-dev -y 
apt-get install libleptonica-dev -y  
apt-get install tesseract-ocr-eng -y
apt-get install libcanberra-gtk3-module -y
apt-get install qt5-default -y
apt-get  install libcanberra-gtk-module -y
apt-get  install wget -y 
apt-get  install gcc -y   
apt-get  install g++ -y
#if you need to update your GCC please uncomment the commands below      
#apt-get install software-properties-common -y
#add-apt-repository ppa:ubuntu-toolchain-r/test -y
#apt-get update
#apt-get install gcc-9 g++-9 -y
#update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 20 --slave /usr/bin/g++ g++ /usr/bin/g++-9
#gcc --version

cd /tmp

echo "** Install cmake"
wget https://github.com/Kitware/CMake/releases/download/v3.16.4/cmake-3.16.4.tar.gz   
tar zxvf cmake-3.16.4.tar.gz    
cd cmake-3.16.4 
./configure 
make   
make install  
cmake -version  
cd ..
rm -r cmake* 

echo "** Install OpenCV 2.4.13.7"
#first, need to remove the previous version of OpenCV
apt-get purge *libopencv* -y

git clone -b 2.4 --single-branch https://github.com/opencv/opencv.git
cd opencv
git checkout 2.4.13.7

#need to implement some changes to build OpenCV with CUDA and QT
old_line='find_cuda_helper_libs(nppi)'
new_line='find_cuda_helper_libs(nppial)\n  find_cuda_helper_libs(nppicc)\n  find_cuda_helper_libs(nppicom)\n  find_cuda_helper_libs(nppidei)\n  find_cuda_helper_libs(nppif)\n  find_cuda_helper_libs(nppig)\n  find_cuda_helper_libs(nppim)\n  find_cuda_helper_libs(nppist)\n  find_cuda_helper_libs(nppisu)\n  find_cuda_helper_libs(nppitc)'
sed -i -e "s/$old_line/$new_line/g" cmake/FindCUDA.cmake 

old_line='set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}")'
new_line='set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};${CUDA_nppig_LIBRARY};${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY};${CUDA_npps_LIBRARY}")'
sed -i -e "s/$old_line/$new_line/g" cmake/FindCUDA.cmake

old_line='unset(CUDA_nppi_LIBRARY CACHE)'
new_line='unset(CUDA_nppial_LIBRARY CACHE)\n  unset(CUDA_nppicc_LIBRARY CACHE)\n  unset(CUDA_nppicom_LIBRARY CACHE)\n  unset(CUDA_nppidei_LIBRARY CACHE)\n  unset(CUDA_nppif_LIBRARY CACHE)\n  unset(CUDA_nppig_LIBRARY CACHE)\n  unset(CUDA_nppim_LIBRARY CACHE)\n  unset(CUDA_nppist_LIBRARY CACHE)\n  unset(CUDA_nppisu_LIBRARY CACHE)\n  unset(CUDA_nppitc_LIBRARY CACHE)'
sed -i -e "s/$old_line/$new_line/g" cmake/FindCUDA.cmake 

old_line='glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);'
new_line='\/\/glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);'
sed -i -e "s/$old_line/$new_line/g" modules/highgui/src/window_QT.cpp

sed -i -e '61,65s/^/\/\/ /' -e '67s/^/\/\/ /' /usr/local/cuda-10.2/include/cuda_gl_interop.h 

cd ..

git clone https://github.com/opencv/opencv_extra.git
cd opencv_extra
git checkout 2.4.13.7
cd ..

mkdir release
cd release

cmake \
  -D CMAKE_CXX_COMPILER=/usr/bin/g++-7          \
  -D CMAKE_C_COMPILER=/usr/bin/gcc-7            \
  -D CMAKE_BUILD_TYPE=RELEASE                   \
  -D INSTALL_C_EXAMPLES=ON                      \
  -D CMAKE_INSTALL_PREFIX=/usr/local            \
  -D ENABLE_FAST_MATH=ON                        \
  -D WITH_V4L=ON                                \
  -D WITH_OPENGL=ON                             \
  -D WITH_CUDA=ON                               \
  -D WITH_QT=ON                                 \
  -D WITH_CUBLAS=ON                             \
  -D WITH_LIBV4L=ON                             \
  -D WITH_GSTREAMER=ON                          \
  -D WITH_FFMPEG=ON                             \
  -D WITH_GTK=ON                                \
  -D BUILD_EXAMPLES=ON                          \
  -D BUILD_opencv_python2=ON                    \
  -D BUILD_opencv_python3=ON                    \
  -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-10.2 \
  -D CUDA_ARCH_BIN=5.3                          \
  -D CUDA_ARCH_PTX=""                           \
  -D CUDA_FAST_MATH=ON                          \
  -D OPENCV_TEST_DATA_PATH=../opencv_extra/testdata ../opencv

make -j4
make install
cd ..
rm -r opencv*

cd /etc/ld.so.conf.d
touch opencv.conf
chmod 777 opencv.conf
echo "/usr/local/lib/libopencv_core.so.2.4" >> opencv.conf
ldconfig -v

sudo reboot
-1

Replace the exisiting opencv with the latest version, it works fine. XD