Download opencv

cd ~
mkdir opencv
cd opencv
wget <https://github.com/opencv/opencv/archive/3.4.6.zip> -O opencv-3.4.6.zip
wget <https://github.com/opencv/opencv_contrib/archive/3.4.6.zip> -O opencv_contrib-3.4.6.zip
unzip opencv-3.4.6.zip
unzip opencv_contrib-3.4.6.zip

To build opencv

cd opencv-3.4.6/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \\
-D CMAKE_INSTALL_PREFIX=/usr/local \\
-D WITH_CUDA=ON \\
-D CUDA_ARCH_BIN=5.3 \\
-D CUDA_ARCH_PTX="" \\
-D WITH_CUBLAS=ON \\
-D ENABLE_FAST_MATH=ON \\
-D CUDA_FAST_MATH=ON \\
-D ENABLE_NEON=ON \\
-D WITH_GSTREAMER=ON \\
-D WITH_LIBV4L=ON \\
#####################-D ENABLE_CXX11=ON \\
-D WITH_TBB=OFF \\
-D WITH_IPP=OFF \\
-D WITH_1394=OFF \\
-D BUILD_WITH_DEBUG_INFO=OFF \\
-D BUILD_DOCS=OFF \\
-D INSTALL_C_EXAMPLES=ON \\
-D INSTALL_PYTHON_EXAMPLES=ON \\
-D BUILD_EXAMPLES=OFF \\
-D BUILD_TESTS=OFF \\
-D BUILD_PERF_TESTS=OFF \\
-D WITH_QT=ON \\
-D WITH_GTK=ON \\
-D WITH_OPENGL=ON \\
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.6/modules \\
-D WITH_V4L=ON  \\
-D WITH_FFMPEG=ON \\
-D WITH_XINE=ON \\
-D BUILD_NEW_PYTHON_SUPPORT=ON \\
-D PYTHON2_INCLUDE_DIR=/usr/include/python2.7 \\
-D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/lib/python2.7/dist-packages/numpy/core/include/ \\
-D PYTHON2_PACKAGES_PATH=/usr/lib/python2.7/dist-packages \\
-D PYTHON2_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython2.7.so \\
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.6m \\
-D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include/  \\
-D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages \\
-D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so \\
../

Before make fix some files

error: #error Please include the appropriate gl headers before including cuda_gl_interop.h

Open cuda_gl_inerop.h in /usr/local/cuda-10.2/include

cd /usr/local/cuda-10.2/include
sudo nano cuda_gl_interop.h

Find below line of code and comment as below

//#if defined(__arm__) || defined(__aarch64__) 
//#ifndef GL_VERSION 
//#error Please include the appropriate gl headers before including cuda_gl_interop.h 
//#endif 
//#else 
#include <GL/gl.h> 
//#endif

make opencv

$ time make -j $(nproc)
...
[100%] Built target opencv_python3

real	150m42,847s
user	474m40,500s
sys	25m48,308s

Install opencv

cv_bridge 관련 에러 해결

change the file:

/opt/ros/melodic/share/cv_bridge/cmake/cv_bridgeConfig.cmake

change this line:

set(_include_dirs "include;/usr/include;/usr/include/opencv")