【Ubuntu20.04编译安装Opencv4.5.4,支持Cuda加速】

https://pyimagesearch.com/2020/02/03/how-to-use-opencvs-dnn-module-with-nvidia-gpus-cuda-and-cudnn/
https://blog.csdn.net/fixed_zhang/article/details/110428259
https://blog.csdn.net/BockSong/article/details/96437452

Ubuntu20.04编译安装Opencv4.5.4,支持Cuda加速

  • 一、准备工作
  • 二、编译安装
  • 三、卸载

一、准备工作

  1. 安装NVIDIA驱动
  2. 安装CUDA和CUDNN
  3. 安装Opencv相关依赖:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential cmake cmake-gui unzip pkg-config
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libgtk2.0-dev
sudo apt-get install libv4l-dev libxvidcore-dev libx264-dev libtbb2 libtbb-dev libdc1394-22-dev libjasper-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libatlas-base-dev liblapacke-dev gfortran
sudo apt-get install python3-dev
sudo apt-get install ffmpeg 
  1. 下载 opencv4.5.4 和对应版本的 opencv_contrib-4.5.4,新建一个opencv文件夹,将两个文件解压到此处,在opencv文件中新建一个build文件夹

二、编译安装

  1. CMake
    在终端打开cmake-gui:
sudo apt-get install cmake-gui

分别配置sourcebuild路径,source是opencv的源码路径,build是前面新建的build文件夹路径,然后点击Configure,在弹出的窗口中保持默认选项,最后Finish
【Ubuntu20.04编译安装Opencv4.5.4,支持Cuda加速】_第1张图片
勾选下面的编译选项:
WITH_CUDA
BUILD_CUDA_STUBS
OPENCV_DNN_CUDA
ENABLE_FAST_MATH
CMAKE_BUILD_TYPE中添加 Release
CMAKE_INSTALL_PREFIX中添加路径/usr/local
OPENCV_EXTRA_MODULES_PATH中添加opencv_contrib中的modules路径:/home/wj/opencv/opencv_contrib-4.5.4/modules

再点击Configure,结束后勾选CUDA_FAST_MATH,在CUDA_ARCH_BIN中选择保留需要的算力值,可以在通过此页面查询显卡对应的算力值,这里保留了7.0;7.5,最后再点击进行一次Configure
【Ubuntu20.04编译安装Opencv4.5.4,支持Cuda加速】_第2张图片
如上图中成功的显示CUDA和CUDNN的版本即可,然后点击Generate,结束后关闭cmake-gui

  1. make
    执行下面的命令,等待很长一段时间:
sudo make -j8		#根据CPU选择j后面的数值
  1. 安装
sudo make install
  1. 环境配置
sudo gedit /etc/ld.so.conf

添加下面两行:
在这里插入图片描述

sudo ldconfig
sudo gedit /etc/bash.bashrc

在文件末尾加上下面两行:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

source /etc/bash.bashrc

结束!

三、卸载

进入前面建立的build文件夹,打开终端:

sudo make uninstall
cd ..
sudo rm -r build
sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
cd /usr/
find . -name "*opencv*" | xargs sudo rm -rf

你可能感兴趣的:(opencv,c++,linux,ubuntu)