[环境配置]Ubuntu 16.04 源码编译安装OpenCV-3.2.0+OpenCV_contrib-3.2.0及产生的问题

Posted RichardYao1995

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[环境配置]Ubuntu 16.04 源码编译安装OpenCV-3.2.0+OpenCV_contrib-3.2.0及产生的问题相关的知识,希望对你有一定的参考价值。

1.OpenCV-3.2.0+OpenCV_contrib-3.2.0编译安装过程

1)下载官方要求的依赖包

  • GCC 4.4.x or later
  • CMake 2.6 or higher
  • Git
  • GTK+2.x or higher, including headers (libgtk2.0-dev) # 控制opencv GUI
  • 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
$ 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)下载OpenCV-3.2.0+OpenCV_contrib-3.2.0

$ cd /the_path_you_would_install
$ wget https://github.com/opencv/opencv/archive/3.2.0.zip
$ wget https://github.com/opencv/opencv_contrib/archive/3.2.0.zip

直接右键解压,然后进行安装。

$ cd opencv-3.2.0
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.2.0/modules/ ..
$ make -j8 #如果线程足够多可以使用 make -j12
$ sudo make install

第四行最后的 .. 一定不能忘记,因为我们是在/build文件夹中编译上层文件夹的程序。

$ sudo ldconfig -v
$ pkg-config --modversion opencv #确认OpenCV的版本,如果显示3.2.0说明安装完成

2.遇到的问题及解决方案

1)关于opencv_lapack.h缺失的问题

问题如下 

In file included from /home/yao/opencv-3.2.0/modules/core/src/hal_internal.cpp:49:0:
/home/yao/opencv-3.2.0/build/opencv_lapack.h:2:45: fatal error: LAPACKE_H_PATH-NOTFOUND/lapacke.h: No such file or directory
compilation terminated.
modules/core/CMakeFiles/opencv_core.dir/build.make:114: recipe for target ‘modules/core/CMakeFiles/opencv_core.dir/src/hal_internal.cpp.o‘ failed
make[2]: *** [modules/core/CMakeFiles/opencv_core.dir/src/hal_internal.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....

[ 27%] Built target pch_Generate_opencv_test_optflow
[ 27%] Built target pch_Generate_opencv_perf_optflow
[ 27%] Built target pch_Generate_opencv_test_phase_unwrapping
[ 27%] Built target pch_Generate_opencv_phase_unwrapping
[ 27%] Built target pch_Generate_opencv_test_stitching
[ 27%] Built target pch_Generate_opencv_test_structured_light
[ 27%] Built target pch_Generate_opencv_stitching
[ 27%] Built target pch_Generate_opencv_perf_stitching
[ 27%] Built target pch_Generate_opencv_structured_light
CMakeFiles/Makefile2:1901: recipe for target ‘modules/core/CMakeFiles/opencv_core.dir/all‘ failed
make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2
Makefile:160: recipe for target ‘all‘ failed
make: *** [all] Error 2

解决方案

$ sudo apt-get install liblapacke-dev checkinstall
  • 在/build文件夹中找到opencv_lapack.h文件,把#include "LAPACKE_H_PATH-NOTFOUND/lapacke.h"改为#include "lapacke.h"
  • 重新编译

(2)CUDA 9.0环境下cmake编译时产生的问题

问题如下

在cmake时会产生关于CUDA版本的问题,这种情况在已装CUDA的条件下会出现,未安装时不会有。

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

这是由于CUDA 9.0不支持2.0架构,尝试过网上其他方法,包括在cmake时给命令行加入配置属性如CUDA的路径以及配置,皆无效,而以下方案有效。

解决方案:

1) 在/opencv-3.2.0/cmake文件夹下找到FindCUDA.cmake文件

  • 找到
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)
  • 找到
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}")
  • 找到
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)

2) 找到文件OpenCVDetectCUDA.cmake

删除以下几句

if(CUDA_GENERATION STREQUAL "Fermi")
    set(__cuda_arch_bin "2.0")

然后将下一行的elsif改为if

3) 找到文件opencvmodulescudevincludeopencv2cudevcommon.hpp

添加头文件

#include <cuda_fp16.h>

(3)不支持的GPU architecture问题

问题如下

nvcc fatal : Unsupported gpu architecture ‘compute_20‘

解决方案

在cmake的时候命令行的参数中加入如下一句

-D CUDA_GENERATION=Kepler 

(4)编译到99%或100%时卡住的问题

问题如下

[100%] Built target opencv_perf_stitching
[100%] Built target opencv_python2

这个时候,会一直卡着

解决方案

  • 不要终止安装,等一等,或者make -j8甚至make -j12多线程安装可以快一点,一般几分钟可以安装完成

5)在CMakeLists.txt中设置指定的OpenCV版本

解决方案

set(OpenCV_DIR "/your_opencv_path/opencv-3.2.0/build")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

其中your_opencv_path指你的opencv的安装路径,注意区分大小写。

以上是关于[环境配置]Ubuntu 16.04 源码编译安装OpenCV-3.2.0+OpenCV_contrib-3.2.0及产生的问题的主要内容,如果未能解决你的问题,请参考以下文章

[Android 编译] Ubuntu 16.04 LTS 成功编译 Android 6.0 源码教程

Ubuntu 16.04 LTS 成功编译 Android 6.0 源码教程 (转)

ubuntu16.04源码方式安装配置nginx

[Android 编译] Ubuntu 16.04 LTS 成功编译 Android 6.0 源码教程

Ubuntu 16.04LTS下内核编译与安装

Ubuntu源码安装php