At present,The latest verion of offical OpenCV is 4.5.2。
In this blog, I introduce how to compile OpenCV from source code and enable GPU/CUDA support
- GPU: Tesla V100
- CUDA: 11.0
- CUDA Capability: 7.0 (corresponding to GPU Tesla V100)
- OS: Ubuntu 20.04
# Install prerequisites
sudo apt-get update && apt-get upgrade -y &&\
apt-get install -y \
build-essential \
unzip \
yasm \
pkg-config \
libswscale-dev \
libtbb2 \
libtbb-dev \
libjpeg-dev \
libpng-dev \
libtiff-dev \
libavformat-dev \
libpq-dev \
libxine2-dev \
libglew-dev \
libtiff5-dev \
zlib1g-dev \
libjpeg-dev \
libavcodec-dev \
libavformat-dev \
libavutil-dev \
libpostproc-dev \
libswscale-dev \
libeigen3-dev \
libtbb-dev \
libgtk2.0-dev \
Download source from github
# Download and unpack sources
wget https://github.com/opencv/opencv/archive/refs/tags/4.5.2.zip -O opencv.zip
wget https://github.com/opencv/opencv_contrib/archive/refs/tags/4.5.2.zip -O opencv_contrib.zip
unzip opencv.zip
mv opencv-4.5.2 opencv
unzip opencv_contrib.zip
mv opencv_contrib-4.5.2 opencv_contrib
# Create build directory and switch into it
mkdir opencv/build && cd opencv/build
Please check the CUDA Capability of your GPU from this table(the final part of this blog), and then set the current
CUDA_ARCH_BIN
# Configure
cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_C_COMPILER=/usr/bin/gcc \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ \
-D CUDA_CUDA_LIBRARY=/usr/local/cuda/lib64/stubs/libcuda.so \
-D CUDA_ARCH_BIN=7.0 \
-D CUDA_ARCH_PTX="" \
-D WITH_CUDA=ON \
-D WITH_TBB=ON \
-D WITH_FFMPEG=ON \
-D BUILD_PYTHON_SUPPORT=ON \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_OPENCV_PYTHON3=ON \
-D PYTHON_INCLUDE_DIR=(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON_PACKAGES_PATH=(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
-D WITH_V4L=ON \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_EXAMPLES=ON \
-D WITH_QT=ON \
-D WITH_GSTREAMER=ON \
-D WITH_OPENGL=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
-D CMAKE_LIBRARY_PATH=/usr/local/cuda/lib64/stubs \
-D WITH_CUBLAS=ON \
-D WITH_NVCUVID=ON \
-D BUILD_opencv_cudacodec=ON \
-D OPENCV_DNN_CUDA=ON \
-D WITH_CUDNN=ON \
-D OPENCV_ENABLE_NONFREE=ON\
-D WITH_GSTREAMER=ON \
-D BUILD_EXAMPLES=ON ..
# build
make -j"$(nproc)"
# install
sudo make install
ldconfig
文章评论