当前位置:   article > 正文

Ubuntu22.04系统下opencv-4.8.0及opencv-contrib-4.8.0的安装_ubuntu 下载opencv4.8.0

ubuntu 下载opencv4.8.0

学习目标:

学习Ubuntu22.04系统下opencv-4.8.0及opencv-contrib-4.8.0的安装

内容来源:

ubuntu20.04 opencv4.5.1+cuda11.0安装全过程
ubuntu 编译安装支持CUDA的OpenCV

安装流程:

1. cuda及cudnn的安装:
网上教程很多,这里安装的是cuda12.1和cudnn8.9.6,适配当前最新的pytorch和tensorRT版本
2. 下载opencv安装依赖项:
如果没有修改过apt install的下载源,优先添加豆瓣源,避免依赖项下载中出错。

sudo add-apt-repository ‘deb http://security.ubuntu.com/ubuntu xenial-security main’
sudo apt update
  • 1
  • 2

可能出现没有公钥的报错,添加公钥

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
  • 1

出现公钥过时的报错,进行更新(可能存在一步添加的方法,没有研究)

cd /etc/apt
sudo cp trusted.gpg trusted.gpg.d
  • 1
  • 2

然后根据文章内容来源中的内容安装opencv需要的依赖项,其中libdc1394-22-dev替换为libdc1394-dev,其他依赖项也可能随着更新发生替换,根据安装过程中的具体提示进行修改。

sudo apt install build-essential cmake git pkg-config libgtk-3-dev \
    libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
    libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
    gfortran openexr libatlas-base-dev python3-dev python3-numpy \
    libtbb2 libtbb-dev libdc1394-dev libopenexr-dev \
    libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3. 下载opencv-4.8.0和opencv-contrib-4.8.0

cd ~/home/administrator/opencv4.8
wget -O opencv.zip https://github.com/opencv/opencv/archive/refs/tags/4.8.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/refs/tags/4.8.0.zip
unzip opencv.zip
unzip opencv_contrib.zip
  • 1
  • 2
  • 3
  • 4
  • 5

4. 编译opencv
在opencv-4.8.0目录下新建build(位置影响后续cmake命令中cmakelist.txt的地址查询)

cd opencv-4.8.0
mkdir build
cd build
  • 1
  • 2
  • 3

切换root权限

sudo -i
  • 1

进行预编译

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D CMAKE_C_COMPILER=/usr/bin/gcc-11 \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_opencv_python3=ON \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D WITH_TBB=ON \
-D OPENCV_DNN_CUDA=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D CUDA_ARCH_BIN=8.6 \
-D WITH_CUBLAS=1 \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=/home/administrator/opencv/opencv_contrib-4.8.0/modules \
-D PYTHON3_EXECUTABLE=/home/administrator/anaconda3/envs/doommmmm/bin/python3.9 \
-D PYTHON3_INCLUDE_DIR=/home/administrator/anaconda3/envs/doommmmm/include/python3.9 \
-D PYTHON3_LIBRARY=/home/administrator/anaconda3/envs/doommmmm/lib/libpython3.9.so.1.0 \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/home/administrator/anaconda3/envs/doommmmm/lib/python3.9/site-packages/numpy/core/include \
-D PYTHON3_PACKAGES_PATH=/home/administrator/anaconda3/envs/doommmmm/lib/python3.9/site-packages \
-D PYTHON_DEFAULT_EXECUTABLE=/home/administrator/anaconda3/envs/doommmmm/bin/python3.9 \
-D CUDNN_LIBRARY=/usr/local/cuda-12.1/targets/x86_64-linux/lib/libcudnn.so.8.9.7 \
-D CUDNN_INCLUDE_DIR=/usr/local/cuda-12.1/targets/x86_64-linux/include  \
-D CUDA_CUDA_LIBRARY=/usr/local/cuda-12.1/targets/x86_64-linux/lib/stubs/libcuda.so \
-D OPENCV_PYTHON3_INSTALL_PATH=/home/administrator/anaconda3/envs/doommmmm/lib/python3.9/site-packages \
-D WITH_WEBP=OFF \
-D WITH_OPENCL=OFF \
-D ETHASHLCL=OFF \
-D ENABLE_CXX11=ON \
-D BUILD_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-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 ..
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

CUDA_ARCH_BIN可以根据显卡型号在cuda官网查询算力
https://developer.nvidia.com/cuda-gpus

部分设置根据之前安装的虚拟环境中python的安装目录,cuda和cudun的安装目录做修改

-D OPENCV_EXTRA_MODULES_PATH=/home/administrator/opencv/opencv_contrib-4.8.0/modules \
-D PYTHON3_EXECUTABLE=/home/administrator/anaconda3/envs/doommmmm/bin/python3.9 \
-D PYTHON3_INCLUDE_DIR=/home/administrator/anaconda3/envs/doommmmm/include/python3.9 \
-D PYTHON3_LIBRARY=/home/administrator/anaconda3/envs/doommmmm/lib/libpython3.9.so.1.0 \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/home/administrator/anaconda3/envs/doommmmm/lib/python3.9/site-packages/numpy/core/include \
-D PYTHON3_PACKAGES_PATH=/home/administrator/anaconda3/envs/doommmmm/lib/python3.9/site-packages \
-D PYTHON_DEFAULT_EXECUTABLE=/home/administrator/anaconda3/envs/doommmmm/bin/python3.9 \
-D CUDNN_LIBRARY=/usr/local/cuda-12.1/targets/x86_64-linux/lib/libcudnn.so.8.9.7 \
-D CUDNN_INCLUDE_DIR=/usr/local/cuda-12.1/targets/x86_64-linux/include  \
-D CUDA_CUDA_LIBRARY=/usr/local/cuda-12.1/targets/x86_64-linux/lib/stubs/libcuda.so \
-D OPENCV_PYTHON3_INSTALL_PATH=/home/administrator/anaconda3/envs/doommmmm/lib/python3.9/site-packages \
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

预编译过程中有部分下载内容由于某些原因不能下载,将对应的cmakelist和xxx.cmake中的下载地址改为镜像源,
ippicv的下载
修改/home/administrator/opencv/opencv-4.8.0/3rdparty/ippicv/ippicv.cmake

 URL
      "${OPENCV_FACE_ALIGNMENT_URL}"
      "$ENV{OPENCV_FACE_ALIGNMENT_URL}"
      "https://raw.staticdn.net/opencv/opencv_3rdparty/${__commit_hash}/"
  • 1
  • 2
  • 3
  • 4

face_landmark.dat的下载
修改 /home/administrator/opencv/opencv_contrib-4.8.0/modules/face/CMakeLists.txt

 URL
      "${OPENCV_FACE_ALIGNMENT_URL}"
      "$ENV{OPENCV_FACE_ALIGNMENT_URL}"
      "https://raw.staticdn.net/opencv/opencv_3rdparty/${__commit_hash}/"
  • 1
  • 2
  • 3
  • 4

vgg_generated_xx.i的下载
修改 /home/administrator/opencv/opencv_contrib-4.8.0/modules/xfeatures2d/cmake/download_vgg.cmake

URL
       "${OPENCV_VGGDESC_URL}"
       "$ENV{OPENCV_VGGDESC_URL}"
       "https://raw.staticdn.net/opencv/opencv_3rdparty/${OPENCV_3RDPARTY_COMMIT}/"
  • 1
  • 2
  • 3
  • 4

根据预编译中的报错补充依赖或者修改预编译宏定义
最后进行编译和下载

make -j16 #根据系统核数cpu调整
sudo make install
  • 1
  • 2

5. 环境配置

修改ld.so.conf

sudo vim /etc/ld.so.conf
  • 1

在文件(可能空白)中最后加入一行

include /usr/local/lib
  • 1

保存并关闭文件,在终端输入

sudo ldconfig
  • 1

修改~./bashrc

sudo vim ~/.bashrc
  • 1

在最后添加

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
  • 1
  • 2

保存并关闭文件,在终端输入

source /etc/bash.bashrc
  • 1

over

结果检验:

检验opencv安装成功

pkg-config --modversion opencv4
  • 1

结果如下:

4.8.0
  • 1

进入python虚拟环境(与预编译使用的环境相同)
输入:

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2021.8 [2021.8.0]
           at:                   /home/administrator/opencv/opencv-4.8.0/build/3rdparty/ippicv/ippicv_lnx/icv
    Intel IPP IW:                sources (2021.8.0)
              at:                /home/administrator/opencv/opencv-4.8.0/build/3rdparty/ippicv/ippicv_lnx/iw
    VA:                          NO
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.19.1)
    Flatbuffers:                 builtin/3rdparty (23.5.9)

  NVIDIA CUDA:                   YES (ver 12.1, CUFFT CUBLAS FAST_MATH)
    NVIDIA GPU arch:             86
    NVIDIA PTX archs:

  cuDNN:                         YES (ver 8.9.7)

  Python 3:

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

安装成功。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/119883
推荐阅读
相关标签
  

闽ICP备14008679号