目 录CONTENT

文章目录

ubuntu 22 源码编译OpenCV(CUDA版)

GrantLi
2023-08-31 / 0 评论 / 0 点赞 / 252 阅读 / 4955 字 / 正在检测是否收录...

依赖

硬件驱动

  • nvidia
  • cuda
  • cudnn

软件依赖

sudo apt install cmake \
                         pkg-config \
                         yasm \
                         checkinstall \
                         libjpeg-dev \
                         libpng-dev \
                         libtiff-dev \
                         libavcodec-dev \
                         libavformat-dev \
                         libswscale-dev \
                         libgstreamer1.0-dev \
                         libgstreamer-plugins-base1.0-dev \
                         libxvidcore-dev x264 libx264-dev \
                         libfaac-dev libmp3lame-dev \
                         libtheora-dev libfaac-dev \
                         libmp3lame-dev \
                         libvorbis-dev \
                         libopencore-amrnb-dev \
                         libopencore-amrwb-dev \
                         libxine2-dev \
                         libv4l-dev \
                         v4l-utils \
                         libgtk-3-dev \
                         libtbb-dev \
                         libatlas-base-dev gfortran
sudo ln -s -f /usr/include/linux/libv4l1-videodev.h videodev.h

下载及编译

下载源码

# 创建opencv工作空间
mkdir -p ~/opencv_ws

# 下载opencv源码
cd ~/opencv_ws
git clone https://github.com/opencv/opencv.git

# 下载opencv_contrib源码
cd ~/opencv_ws
git clone https://github.com/opencv/opencv_contrib.git

编译opencv_contrib

cd ~/opencv_ws/opencv_contrib
mkdir build
cd build
cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules <opencv_source_directory>
make -j$(nproc)

其中<opencv_contrib>为下载保存的opencv_contrib文件目录路径,<opencv_source_directory>为opencv文件目录路径

编译opencv

cd ~/opencv_ws/opencv
mkdir build
cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1  \
-D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules \
-D INSTALL_C_EXAMPLES=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D WITH_TBB=ON \
-D OPENCV_DNN_CUDA=ON \
-D CUDA_ARCH_BIN=8.6 \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D CUDNN_LIBRARY=/usr/lib/x86_64-linux-gnu/libcudnn.so.8 \
-D CUDNN_INCLUDE_DIR=/usr/local/cuda/include  \
-D CUDA_CUDA_LIBRARY=/usr/local/cuda/lib64/stubs/libcuda.so \
-D WITH_WEBP=OFF \
-D WITH_OPENCL=OFF \
-D ETHASHLCL=OFF \
-D ENABLE_CXX11=ON \
-D BUILD_EXAMPLES=OFF \
-D WITH_OPENGL=ON \
-D WITH_GSTREAMER=ON \
-D WITH_V4L=ON \
-D WITH_QT=OFF \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_python2=OFF \
-D HAVE_opencv_python3=ON   ..

# 编译
make -j$(nproc)
sudo make install

其中OPENCV_EXTRA_MODULES_PATH中的<opencv_contrib>为下载保存的opencv_contrib文件目录路径CUDNN_LIBRARYcudnn动态连接库文件目录路径CUDA_CUDA_LIBRARYcuda动态连接库文件目录路径

环境配置

sudo /bin/bash -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

验证

  • 检查opencv是否安装成功
pkg-config --modversion opencv4
pkg-config --libs opencv4
  • 在终端上启动 python 并执行以下操作:
import cv2
print(cv2.getBuildInformation())

卸载

进入build目录下:

sudo make uninstall
0

评论区