Building OpenCV
To Install OpenCV follow the installation tutorial
Use the following script to build OpenCV from source. Ensure that you adapt the relevant CUDA versions and that both CUDA and cuDNN are installed as defined by their compatibility matrices https://docs.nvidia.com/deploy/cuda-compatibility/index.html and https://docs.nvidia.com/deeplearning/cudnn/support-matrix/index.html
sudo apt install pkg-config
sudo apt install git
sudo apt install ffmpeg
sudo apt install libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libavresample-dev
sudo apt install libgstreamer-plugins-base1.0-dev
source ~/envs/env-cv-full-py38/bin/activate
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PATH=/usr/local/cuda-11.8/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-11.8
export CUDA_HOME=/usr/local/cuda-11.8
export CUDA_NVCC_EXECUTABLE=/usr/local/cuda-11.8/bin/nvcc
cd ~/src
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib && git checkout 4.6.0 && cd ..
cd opencv && mkdir -p build && git checkout 4.6.0 && cd build
cd opencv/build
export CUDA_PATH=/usr/local/cuda-11.8/bin
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_CUDA=ON \
-D CUDA_BIN_PATH=/usr/local/cuda-11.8/bin \
-D CUDA_VERSION=11.8 \
-D WITH_GSTREAMER=ON \
-D WITH_FFMPEG=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_CUDNN=ON \
-D WITH_WEBP=OFF \
-D OPENCV_DNN_CUDA=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=ON \
-D HAVE_opencv_python3=ON \
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.8 \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=ON .. && \
make -j $(($(nproc) + 1)) && \
sudo make install && \
sudo ldconfig
set -e
cd /usr/local/lib/python3.8/site-packages/cv2/python-3.8
sudo mv cv2.cpython-38-x86_64-linux-gnu.so cv2.so
cd ~/envs/env-cv-full-py38/lib/python3.8/site-packages/
ln -s /usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.so cv2.so
sudo apt install pkg-config
sudo apt install git
sudo apt install ffmpeg
sudo apt install libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libavresample-dev
sudo apt install libgstreamer-plugins-base1.0-dev
source ~/envs/env-cv-full-py38/bin/activate
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PATH=/usr/local/cuda-11.8/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-11.8
export CUDA_HOME=/usr/local/cuda-11.8
export CUDA_NVCC_EXECUTABLE=/usr/local/cuda-11.8/bin/nvcc
cd ~/src
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib && git checkout 4.6.0 && cd ..
cd opencv && mkdir -p build && git checkout 4.6.0 && cd build
cd opencv/build
export CUDA_PATH=/usr/local/cuda-11.8/bin
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_CUDA=ON \
-D CUDA_BIN_PATH=/usr/local/cuda-11.8/bin \
-D CUDA_VERSION=11.8 \
-D WITH_GSTREAMER=ON \
-D WITH_FFMPEG=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_CUDNN=ON \
-D WITH_WEBP=OFF \
-D OPENCV_DNN_CUDA=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=ON \
-D HAVE_opencv_python3=ON \
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.8 \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=ON .. && \
make -j $(($(nproc) + 1)) && \
sudo make install && \
sudo ldconfig
set -e
cd /usr/local/lib/python3.8/site-packages/cv2/python-3.8
sudo mv cv2.cpython-38-x86_64-linux-gnu.so cv2.so
cd ~/envs/env-cv-full-py38/lib/python3.8/site-packages/
ln -s /usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.so cv2.so
There exists a but when using CUDA_STANDARD
, simply ensure you're using CMake > 3.20 then the bug will be gone.