编译opencv3.2.0前需要先安装cuda10.2
本人电脑配置
ubuntu 18.04
GTX960M
nvidia-460驱动
cuda10.2
cudnn8.0
1、安装opencv3.2.0依赖
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 build-essential qt5-default ccache libv4l-dev libavresample-dev libgphoto2-dev libopenblas-base libopenblas-dev doxygen openjdk-8-jdk pylint libvtk6-dev
sudo apt-get install pkg-config
2、下载opencv3.2.0和opencv_contrib
opencv3.2.0 github链接
https://github.com/opencv/opencv/releases/tag/3.2.0
opencv_contrib github链接
https://github.com/opencv/opencv_contrib/tags?after=3.4.3
将opencv_contrib放到opencv3.2.0根目录下,如下图
3、因为cuda10.2版本比较高,cmake之前需要对opencv文件进行更改
A、在opencv3.2.0的cmake文件夹中找到FindCUDA.cmake文件
1)找到命令行
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)
2)找到命令行
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}")
3)找到命令行
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)
B、在opencv3.2.0的cmake中找到文件OpenCVDetectCUDA.cmake
修改以下命令行
set(__cuda_arch_ptx "")
if(CUDA_GENERATION STREQUAL "Fermi")
set(__cuda_arch_bin "2.0")
elseif(CUDA_GENERATION STREQUAL "Kepler")
set(__cuda_arch_bin "3.0 3.5 3.7")
将以上命令改为
set(__cuda_arch_ptx "")
if(CUDA_GENERATION STREQUAL "Kepler")
set(__cuda_arch_bin "3.0 3.5 3.7")
elseif(CUDA_GENERATION STREQUAL "Maxwell")
set(__cuda_arch_bin "5.0 5.2")
C、将头文件cuda_fp16.h添加至 opencv3.2.0/modules/cudev/include/opencv2/cudev/common.hpp
在common.hpp中添加
#include <cuda_fp16.h>
4、编译opencv3.2.0 with cuda10.2
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv320 -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_CUDA=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES" -D WITH_CUBLAS=1 -D OPENCV_EXTRA_MODULES_PATH=/home/x1/TOOL/opencv-3.2.0/opencv_contrib-3.2.0/modules/ -D CUDA_GENERATION=Kepler -D OPENCV_ENABLE_NONFREE=True \..
make -j8
cmake命令比较长
注意:
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv320 命令设置opencv安装位置
-D OPENCV_EXTRA_MODULES_PATH=/home/x1/TOOL/opencv-3.2.0/opencv_contrib-3.2.0/modules/ 命令为opencv_contrib中modules的文件夹位置,根据自己路径设置
最后的 ..不要忘记
5、这里因为网络问题会缺少很多文件
ippicv_linux_20151201.tgz
boostdesc_bgm.i
boostdesc_bgm_bi.i
boostdesc_bgm_hd.i
boostdesc_lbgm.i
boostdesc_binboost_064.i
boostdesc_binboost_128.i
boostdesc_binboost_256.i
vgg_generated_120.i
vgg_generated_64.i
vgg_generated_80.i
vgg_generated_48.i
这些文件的资源放在了我的百度网盘里
链接: https://pan.baidu.com/s/1GKiWUDXgYwc9AGergzAs-w 密码: gbfc
下载后的文件放到opencv-3.2.0/opencv_contrib-3.2.0/modules/xfeatures2d/src目录下
但是继续编译程序还是可能会尝试继续下载以上文件
这里面就需要注释xfeatures2d/CMakeList.txt中的最后两行,然后保存
(opencv3.4.0不需要注释):
include(cmake/download_vgg.cmake) include(cmake/download_boostdesc.cmake)
接着cmake就不会报错了
6、最后编译
用以下命令进行编译安装
make -j8
sudo make install
如果make时候出现错误
/usr/include/c++/7/cstdlib:75:15: fatal error: stdlib.h: No such file or directory
就需要重新cmake
并且将cmake选项中加入
-D ENABLE_PRECOMPILED_HEADERS=OFF