【Ubuntu版】源码安装opencv(一)

一、参考资料

从源码安装OpenCV,使用python

在ubuntu下安装opencv4.1.0详细步骤

ubuntu16.04下编译安装OpenCV3.4.5步骤(超详细)

ubuntu:安装及使用OpenCV4.1.0(C++/python)

OpenCV 4.1.0 安装官方文档

OpenCV3.3+CUDA9.0+Cmake3.9 环境搭建

linux平台编译cuda版本opencv

编译opencv cuda 版本

编译安装 opencv3.3.0 的cuda版本(cuda10.0)

ubuntu16.04安装cuda版本的opencv3.3.0,安装NVIDIA、CUDA和opencv

Opencv 源码下载

Ubuntu系统上OpenCV 4.1.2 源码的编译与安装

ubuntu系统编译安装OpenCV 4.4

linux ubuntu16.04 opencv3.4.2 cuda9 安装编译填坑记

Linux Ubuntu16.04 Opencv3(+CUDA9.0)安装记录

ubuntu16.04安装opencv3.3.1

Install_OpenCV4_CUDA11_CUDNN8.md

[OpenCV] Install OpenCV 4.0 with DNN

二、注意事项

  1. opencv支持CUDA加速

  2. 编译安装opencv不要用默认安装路径(CMAKE_INSTALL_PREFIX=/usr/local),后续多版本opencv共存时会存在很大问题;

  3. 从OpenCV-4.0之后,CUDA模块被移动到了opencv_contrib中,默认的源码包是不带CUDA的

  4. 如果我们只下载OpenCV源码编译是不行的,必须加上contrib模块,下载好之后,在Cmake编译时添加contrib模块的路径:-D OPENCV_EXTRA_MODULES_PATH=/modules并开启-DWITH_CUDA=ON

  5. apt-get install cmake 下载版本较低,在 cmake 官网下载最新版本;

  6. 避免编译opencv过程中下载相关文件,参考博客 ubuntu cmake opencv4.2;

  7. 博主以安装 OpenCV 4.1.0 安装官方文档 为例;

  8. opencv官方教程;

  9. 博主的系统环境:

    Environment
    Operating System + Version: Ubuntu + 16.04
    GPU Type: GeForce GTX1650,4GB
    Nvidia Driver Version: 470.63.01
    CUDA Version: 
    CUDNN Version: 
    gcc:
    g++:
    
  10. 官方建议的系统环境

    GCC 4.4.x or later
    CMake 2.8.7 or higher
    Git
    GTK+2.x or higher, including headers (libgtk2.0-dev)
    pkg-config
    Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
    ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
    [optional] libtbb2 libtbb-dev
    [optional] libdc1394 2.x
    [optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
    [optional] CUDA Toolkit 6.5 or higher
    
  11. opencv_contrib的版本与opencv版本必须一致

三、相关介绍

opencv-4.5.3

1. 需要安装的程序

Installed Programs

opencv_annotation
opencv_interactive-calibration
opencv_model_diagnostics
opencv_version
opencv_visualisation
setup_vars_opencv4.sh

2. 需要安装的库

Installed Libraries

libopencv_calib3d.so
libopencv_core.so
libopencv_dnn.so
libopencv_features2d.so
libopencv_flann.so
libopencv_gapi.so
libopencv_highgui.so
libopencv_imgcodecs.so
libopencv_imgproc.so
libopencv_ml.so
libopencv_objdetect.so
libopencv_photo.so
libopencv_stitching.so
libopencv_video.so
libopencv_videoio.so

3. 安装的路径

Installed Directories
可能有些路径存在,可能不存在

/usr/include/opencv4 
/usr/lib/cmake/opencv4
/usr/lib/python3.9/site-packages/cv2
/usr/share/licenses/opencv4
/usr/share/opencv4
/usr/share/java/opencv4
/usr/local/include/opencv4
/usr/local/share/opencv4
/usr/local/bin/opencv*
/usr/local/lib/libopencv*
/usr/local/lib/pkgconfig/opencv4.pc
/usr/local/lib/cmake/opencv4

四、准备工作

ubuntu下安装opencv3.4以及c++编译
树莓派3B/3B+和4B安装OpenCV教程 (详细教程)

方法一(修改下载链接)

cmake解决opencv编译下载失败的方法

方法二

1. 准备 ippicv

ippicv 是一个并行计算库,安装过程中下载 ippicv_2017u3_lnx_intel64_general_20170822.tgz 的时间很长,甚至下载失败。

ippicv其他版本
源码编译opencv卡在IPPICV: Download: ippicv_2017u3_lnx_intel64_general_20170822.tgz解决办法
ubuntu16.04安装opencv3.3.1

手动下载,以 ippicv_2017u3_lnx_intel64_general_20180518.tgz 为例:

修改文件
/home/yichao/360Downloads/opencv-3.3.0/3rdparty/ippicv/ippicv.cmake

"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"
修改为
手动下载的文件的本地路径,如:"file:///home/yichao/MyDocuments/cmake_files/"

下载后,在文件 ippicv_2017u3_lnx_intel64_general_20170822.tgz 所在目录创建一个脚本文件,内容如下:

#!/bin/bash 
# 根据实际情况修改路径
ipp_file=./ippicv_2017u3_lnx_intel64_general_20170822.tgz && 
ipp_hash=$(md5sum $ipp_file | cut -d" " -f1) && 
ipp_dir=/home/yichao/360Downloads/opencv-3.3.0/3rdparty/ippicv/downloads/linux-$ipp_hash && mkdir -p $ipp_dir && 
cp $ipp_file $ipp_dir

opencv-3.3.1/3rdparty/ippicv/ 下创建downloads文件夹。然后执行该脚本。

2. face_landmark_model.dat

face_landmark_model.dat下载

修改文件
/home/yichao/360Downloads/opencv-3.3.0/opencv_contrib-3.3.0/modules/face/CMakeLists.txt

"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${__commit_hash}/"
修改为
手动下载的文件的本地路径,如:"file:///home/yichao/MyDocuments/cmake_files/"

3. boostdesc_bgm.i等文件

opencv_contrib-3.4.2/modules/xfeatures2d/src
/home/yichao/360Downloads/opencv-3.3.0/opencv_contrib-3.3.0/modules/xfeatures2d/src目录中,如下文件可能下载失败:

\boostdesc 
 
boostdesc_bgm.i
boostdesc_bgm_bi.i
boostdesc_bgm_hd.i
boostdesc_binboost_064.i
boostdesc_binboost_128.i
boostdesc_binboost_256.i
boostdesc_lbgm.i
 
\vgg 
 
vgg_generated_120.i
vgg_generated_48.i
vgg_generated_64.i
vgg_generated_80.i

boostdesc下载: https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/*,*可以替换为 boostdesc_bgm.i 等。

vgg下载: https://github.com/opencv/opencv_3rdparty/tree/contrib_xfeatures2d_vgg_20160317 下载zip包。

修改/home/yichao/360Downloads/opencv-3.3.0/opencv_contrib-3.3.0/modules/xfeatures2d/cmake/download_boostdesc.cmake文件

"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${OPENCV_3RDPARTY_COMMIT}/"
修改为
手动下载的文件的本地路径,如:"file:///home/yichao/MyDocuments/cmake_files/"

修改/home/yichao/360Downloads/opencv-3.3.0/opencv_contrib-3.3.0/modules/xfeatures2d/cmake/download_vgg.cmake文件

"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${OPENCV_3RDPARTY_COMMIT}/"
修改为
手动下载的文件的本地路径,如:"file:///home/yichao/MyDocuments/cmake_files/"

五、关键步骤

  1. 下载 opencv 4.1.0 源码 ,下载 opencv_contrib 4.1.0 源码

    # 方法一
    官网下载 [opencv 4.1.0](https://opencv.org/releases/),选择源码下载
    
    # 方法二
    github仓库下载
    https://github.com/opencv
    
  2. 解压缩包

    unzip opencv-4.1.0.zip
    
    ./opencv-3.3.0  # opencv路径
    ├── 3rdparty
    ├── apps
    ├── cmake
    ├── CMakeLists.txt
    ├── CONTRIBUTING.md
    ├── data
    ├── doc
    ├── include
    ├── LICENSE
    ├── modules
    ├── opencv_build_dir
    ├── opencv_contrib-3.3.0  # opencv_contrib路径
    ├── platforms
    ├── README.md
    └── samples
    
  3. 安装依赖包
    参考:OpenCV 4.1.0 安装官方文档

    # 安装编译器
    sudo apt-get install build-essential
    
    # 安装CMake等开发人员工具
    # 安装build-essential、cmake、git和pkg-config
    sudo apt-get install build-essential cmake git pkg-config
    
    # 安装GTK2.0
    sudo apt-get install libgtk2.0-dev
    
    # 安装常用图像工具包
    # 安装jpeg格式图像工具包
    sudo apt-get install libjpeg8-dev 
    
    # 安装tif格式图像工具包
    sudo apt-get install libtiff5-dev 
    
    #安装JPEG-2000图像工具包
    sudo apt-get install libjasper-dev 
    
    # 安装png图像工具包
    sudo apt-get install libpng12-dev 
    
    # 安装openCV数值优化函数包
    sudo apt-get install libatlas-base-dev gfortran
    
    # v4l中4后面的是 英文字母“l”
    sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
    
    # 可选
    sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libdc1394-22-dev
    
  4. 创建build文件夹

    cd /home/yichao/360Downloads/opencv 4.1.0
    mkdir build
    cd build
    
  5. cmake生成 CMakeLists.txt 文件
    方法一:见后文

    方法二:见后文

  6. make编译以及make install安装

    # 如果重新编译,需要清理编译
    make clean
    
    # 编译
    make -j8
    
    # 安装
    sudo make install
    

【Ubuntu版】源码安装opencv(一)_第1张图片
【Ubuntu版】源码安装opencv(一)_第2张图片
7. 将opencv库添加到系统路径

sudo gedit /etc/ld.so.conf.d/opencv.conf
或者
sudo gedit /etc/ld.so.conf

# 添加一行
/usr/local/lib

# 更新配置
sudo ldconfig
或者
sudo /sbin/ldconfig
  1. 添加环境变量

     sudo gedit /etc/bash.bashrc
    
    # 末尾添加内容
    PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig 
    export PKG_CONFIG_PATH
    
    # 更新
    source /etc/bash.bashrc 
    
    sudo updatedb 
    
  2. 测试是否安装成功

    # 方法一
    # 查看opencv版本
    pkg-config opencv --modversion
    
    # 方法二
    cd /home/yichao/360Downloads/opencv-4.1.0/samples/cpp/example_cmake
    cmake .
    make
    ./opencv_example
    
    # 如果需要重新编译
    make clean
    
    # 如果显示 Hello OpenCV,表示安装成功
    

【Ubuntu版】源码安装opencv(一)_第3张图片
【Ubuntu版】源码安装opencv(一)_第4张图片

cmake指令编译

根据下述详细配置来进行编译,可以避免包括上述问题在内的很多问题,一般来说不关键的缺省就可以了

# 不支持GPU加速
cmake -D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_GENERATE_PKGCONFIG=YES \
-D WITH_1394=OFF ..
# 支持GPU加速
cmake \
-D BUILD_CUDA_STUBS=ON \
-D BUILD_DOCS=ON \
-D BUILD_EXAMPLES=ON \
-D BUILD_opencv_cudacodec=OFF \
-D CUDA_FAST_MATH=ON \
-D CUDA_ARCH_BIN=7.5 \
-D CUDA_GENERATION=Pascal \
-D CUDA_NVCC_FLAGS="-std=c++11 --expt-relaxed-constexpr" \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D ENABLE_AVX=ON \
-D ENABLE_CXX11=1 \
-D ENABLE_FAST_MATH=1 \
-D HAVE_opencv_python3=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_DNN_CUDA=ON \
-D OPENCV_EXTRA_MODULES_PATH=/home/yichao/360Downloads/opencv-4.1.0/opencv_contrib-4.1.0/modules \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D PYTHON_EXECUTABLE=~/anaconda3/envs/face/bin/python \
-D WITH_CUBLAS=ON \
-D WITH_CUFFT=ON \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D WITH_EIGEN=ON \
-D WITH_V4L=ON \
-D WITH_NVCUVID=1 \
-D WITH_FFMPEG=ON \
-D WITH_IPP=ON \
-D WITH_TBB=ON \
-D WITH_OPENGL=ON \
-D WITH_OPENMP=ON \
-D WITH_GTK=ON \
-D WITH_OPENCL=ON ..

# 字段解释
CMAKE_BUILD_TYPE=RELEASE,表示编译发布版本。
CMAKE_INSTALL_PREFIX,指定opencv_contrib-4.1.0的路径。
CUDA_ARCH_BIN=7.5,指定Compute Capability算力,对应的算力值需要根据自己的显卡型号到官网上查询。
CUDA_FAST_MATH=ON,计算速度更快但是相对不精确。
CUDA_NVCC_FLAGS="-std=c++11 --expt-relaxed-constexpr",这句话是告诉cuda,也使用c++11来编译,否则会报错:“opencv cuda error: identifier “nullptr” is undefined”。
WITH_CUDA=ON,使用CUDA,如果不是非常需要GPU或者没有CUDA,关掉这个就可以解决CUDA_nppi_LIBRARY的问题。
WITH_CUBLAS=ON,与WITH_CUDA=ON 保持一致就好。
WITH_GTK=ON,这个配置是为了防止GTK配置失败:即安装了libgtk2.0-dev依赖,还是报错未安装。
WITH_QT=ON,如果qt未安装可以删去此行;若因为未正确安装qt导致的Qt5Gui报错,可将build内文件全部删除后重新cmake,具体可以参考[这里](http://stackoverflow.com/questions/17420739/opencv-2-4-5-and-qt5-error-s)OPENCV_GENERATE_PKGCONFIG=YES,表示自动生成OpenCV的pkgconfig文件,否则需要自己手动生成。

cmake-gui编译

1. start cmake-gui

2. select the opencv source code folder and the folder where binaries will be built (the 2 upper forms of the interface)

3. press the configure button. you will see all the opencv build parameters in the central interface

4. browse the parameters and look for the form called OPENCV_EXTRA_MODULES_PATH (use the search form to focus rapidly on it)

5. complete this OPENCV_EXTRA_MODULES_PATH by the proper pathname to the /modules value using its browse button.

6. press the configure button followed by the generate button (the first time, you will be asked which makefile style to use)

7. build the opencv core with the method you chose (make and make install if you chose Unix makfile at step 6)

8. to run, linker flags to contrib modules will need to be added to use them in your code/IDE. For example to use the aruco module, "-lopencv_aruco" flag will be added.

Configure和Generate生成配置

参考博客:
【Ubuntu版】源码安装opencv(三)

六、多版本切换共存

参考博客:
【Ubuntu版】opencv多版本切换共存

七、卸载 opencv

参考博客:
Ubuntu下卸载opencv
卸载Opencv(apt方式)

你可能感兴趣的:(编程工具,opencv,ubuntu)