Ubuntu(x86)安装OpenCV(源码)的方法

参考:https://www.cnblogs.com/arkenstone/p/6490017.html

1 安装依赖

$ sudo apt-get install build-essential
$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev # 处理图像所需的包
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
$ sudo apt-get install libxvidcore-dev libx264-dev # 处理视频所需的包
$ sudo apt-get install libatlas-base-dev gfortran # 优化opencv功能
$ sudo apt-get install ffmpeg

2.下载opencv3.2.0

这里需要下载opencv和opencv_contrib(后者会在cmake配置的时候用到),这是因为opencv3以后SIFT和SURF之类的属性被移到了contrib中

$ wget https://github.com/opencv/opencv/archive/3.2.0.zip # 从github上直接下载或者clone也可
$ wget https://github.com/opencv/opencv_contrib/archive/3.2.0.zip

3 编译opencv

a.无NVIDIA CUDA版本

将上述opencv包解压,然后cmake配置属性

$ cd opencv-3.2.0
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.2.0/modules -D PYTHON_EXCUTABLE=/usr/bin/python3 -DPYTHON_DEFAULT_EXECUTABLE=$(which python3) -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=OFF -D WITH_GTK=ON -D WITH_OPENGL=ON -D BUILD_EXAMPLES=ON .. 
$ make -j24
$ sudo make install

b NVIDIA CUDA版本

注意有几个地方要修改

  • 1 CUDA_ARCH_BIN="5.3",注意这块很重要,要找到自己对应显卡的值,根据官网来查找,如果没有设置对,会报计算力错误nvcc fatal : Unsupported gpu architecture 'compute_**'
  • 2 -D PYTHON_EXCUTABLE=/usr/bin/python3,要修改为自己的python路径,如果不清楚,可以用which python或者whereis python来查询
  • 3 如果要在python2和python3之间切换,要加一个 -DPYTHON_DEFAULT_EXCUTABLE==/usr/bin/python3
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.2.0/modules -D PYTHON_EXCUTABLE=/usr/bin/python3 -DPYTHON_DEFAULT_EXECUTABLE=$(which python3) -D WITH_CUDA=ON -D WITH_CUBLAS=ON -D DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" -D CUDA_ARCH_BIN="5.3" -D CUDA_ARCH_PTX="" -D CUDA_FAST_MATH=ON -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_GTK=ON -D WITH_OPENGL=ON -D BUILD_EXAMPLES=ON ..

编译过程中如果报错ICV: Failed to download ICV package: ippicv_linux_20151201.tgz.,首先重新运行一遍,如果还不能下载到,就手动下载,下载路径,然后复制替换到opencv-3.2.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e文件下

如果在cmake过程中报downloading protobuf-cpp-3.1.tar.gz.的错误,则手动下载即可,下载路径(此时想疯狂吐槽一下CSDN的资源下载,之前花费50个积分下载别人上传的资源,第二次下载的时候还让我花费积分,说好的之后免费呢???还是google好使,找到免费资源),下载完之后放在/opencv_contrib/modules/dnn/.download/bd5e3eed635a8d32e2b99658633815ef/v3.1.0路径下,然后重新cmake即可。

编译过程中又报新的错误,提示如下

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CUDA_nppi_LIBRARY (ADVANCED)
    linked by target "opencv_cudev" in directory /home/lthpc/workspace_zong/opencv3.2/opencv-3.2.0/modules/cudev
    linked by target "opencv_cudev" in directory /home/lthpc/workspace_zong/opencv3.2/opencv-3.2.0/modules/cudev
    linked by target "opencv_test_cudev" in directory /home/lthpc/workspace_zong/opencv3.2/opencv-3.2.0/modules/cudev/test
    linked by target "opencv_core" in directory /home/lthpc/workspace_zong/opencv3.2/opencv-3.2.0/modules/core
    linked by target "opencv_core" in directory /home/lthpc/workspace_zong/opencv3.2/opencv-3.2.0/modules/core
    linked by target "opencv_test_core" in directory /home/lthpc/workspace_zong/opencv3.2/opencv-3.2.0/modules/core
    linked by target "opencv_perf_core" in directory /home/lthpc/workspace_zong/opencv3.2/opencv-3.2.0/modules/core
……

查找资料后,是cuda的原因,解决方法如下:

先找到FindCUDA.cmake文件,路径是在/opencv-3.2.0/cmake/FindCUDA.cmke

a.打开找到行

find_cuda_helper_libs(nppi)

修改为

  find_cuda_helper_libs(nppial)
  find_cuda_helper_libs(nppicc)
  find_cuda_helper_libs(nppicom)
  find_cuda_helper_libs(nppidei)
  find_cuda_helper_libs(nppif)
  find_cuda_helper_libs(nppig)
  find_cuda_helper_libs(nppim)
  find_cuda_helper_libs(nppist)
  find_cuda_helper_libs(nppisu)
  find_cuda_helper_libs(nppitc)

b.找到行

set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}")

修改为

set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};${CUDA_nppig_LIBRARY};${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY};${CUDA_npps_LIBRARY}")

c.找到行

unset(CUDA_nppi_LIBRARY CACHE)

修改为

unset(CUDA_nppial_LIBRARY CACHE)
unset(CUDA_nppicc_LIBRARY CACHE)
unset(CUDA_nppicom_LIBRARY CACHE)
unset(CUDA_nppidei_LIBRARY CACHE)
unset(CUDA_nppif_LIBRARY CACHE)
unset(CUDA_nppig_LIBRARY CACHE)
unset(CUDA_nppim_LIBRARY CACHE)
unset(CUDA_nppist_LIBRARY CACHE)
unset(CUDA_nppisu_LIBRARY CACHE)
unset(CUDA_nppitc_LIBRARY CACHE)

修改完之后保存,然后修改下一个文件

cuda9中有一个单独的halffloat(cuda_fp16.h)头文件,也应该被包括在opencv的目录里

将头文件cuda_fp16.h添加至 opencv-3.2.0\modules\cudev\include\opencv2\cudev\common.hpp,即在common.hpp中添加

#include <cuda_fp16.h>

然后重新生成即可,运行成功后会提示如下

-- Configuring done
-- Generating done

配置完之后make

$ make -j24
$ sudo make install

安装完之后,运行python,导入cv2,查看是否成功即可

相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页