Ubuntu16.04 安装 OpenCV2.4.13.6

安装步骤

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

2.进到安装路径下拷贝 OpenCV 源码

git clone https://github.com/opencv/opencv.git

3.选择安装的 OpenCV 版本号

cd opencv
git checkout -d 2.4.13.6


4.使用 Cmake 编译 OpenCV 源码

mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

5.make 安装 OpenCV

make -j4
sudo make install

安装到此结束。

测试

命令进入 Python 编辑器

import cv2
print(cv2.__version__)

会输出 OpenCV 的版本号。

cuda 9.0 与 opencv 版本匹配问题:

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)
原因

在cuda9里面,NVIDIA把
libnppi.so
换成了
libnppc.so
libnppial.so
libnppicc.so
libnppicom.so
libnppidei.so
libnppif.so
libnppig.so
libnppim.so
libnppist.so
libnppisu.so
libnppitc.so
libnpps.so

解决方案

修改opencv/cmake/FindCUDA.cmake 文件,将其中的

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)

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

替换为

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)
find_cuda_helper_libs(npps)
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}")
错误
opencv nvcc fatal   : Unsupported gpu architecture 'compute_20'
原因

cuda9不支持‘ compute-20 ’

解决方案:

更改 OpenCVDetectCUDA.cmake 文件,把有关 ‘ compute-20 ’ 的全删掉

if(CUDA_GENERATION STREQUAL "Fermi")
    set(__cuda_arch_bin "3.0 3.5")
  elseif(CUDA_GENERATION STREQUAL "Kepler")
    if(${CUDA_VERSION} VERSION_LESS "5.0")
      set(__cuda_arch_bin "2.0 2.1")
    else()
      set(__cuda_arch_bin "3.0 3.5")

替换为

if(CUDA_GENERATION STREQUAL "Fermi")
    set(__cuda_arch_bin "3.0 3.5")
  elseif(CUDA_GENERATION STREQUAL "Kepler")
    if(${CUDA_VERSION} VERSION_LESS "5.0")
      set(__cuda_arch_bin "3.0")
    else()
      set(__cuda_arch_bin "3.0 3.5")

将:

if(${CUDA_VERSION} VERSION_LESS "5.0")
     set(__cuda_arch_bin "1.1 1.2 1.3 2.0 2.1(2.0) 3.0")
elseif(${CUDA_VERSION} VERSION_GREATER "6.5")
     set(__cuda_arch_bin "2.0 2.1(2.0) 3.0 3.5")

替换为:

if(${CUDA_VERSION} VERSION_LESS "5.0")
        set(__cuda_arch_bin "1.1 1.2 1.3 2.0 2.1(2.0) 3.0")
elseif(${CUDA_VERSION} VERSION_GREATER "6.5")
        set(__cuda_arch_bin "3.0 3.5")

然后 cmake 成功。

参考

https://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html

https://stackoverflow.com/questions/46584000/cmake-error-variables-are-set-to-notfound

本人博客:寸山河
本人GitHub:王顺
欢迎大家一起交流学习。

猜你喜欢

转载自blog.csdn.net/wgshun616/article/details/81019536