从源码编译安装OpenCV并支持GPU加速

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zizi7/article/details/80539247

环境

Ubuntu16.04 x64
NVIDIA 1080Ti,driver 390.59,CUDA 9.0,cudnn 7.0.5
OpenCV 3.4.1


安装依赖包

参考文章《Ubuntu16.04下安装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 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

下载源码

$ wget https://github.com/opencv/opencv/archive/3.4.1.zip 
$ wget https://github.com/opencv/opencv_contrib/archive/3.4.1.zip

opencv3以后SIFT和SURF之类的属性被移到了contrib中


修复一些错误

link错误

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 D:/Cproject/opencv/opencv/sources/modules/cudev
linked by target "opencv_cudev" in directory D:/Cproject/opencv/opencv/sources/modules/cudev
linked by target "opencv_test_cudev" in directory D:/Cproject/opencv/opencv/sources/modules/cudev/test
...

参考文章《OpenCV3.3+CUDA9.0+Cmake3.9 环境搭建》
原因是 CUDA9.0 不再支持2.0架构

1)找到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文件
修改

 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")

3)修改 opencv/modules/cudev/include/opencv2/cudev/common.hpp
添加

#include <cuda_fp16.h>

编译example错误

[ 87%] Built target example_gpu_super_resolution
make -f samples/gpu/CMakeFiles/example_gpu_cascadeclassifier_nvidia_api.dir/build.make samples/gpu/CMakeFiles/example_gpu_cascadeclassifier_nvidia_api.dir/depend
CMakeFiles/example_gpu_driver_api_multi.dir/driver_api_multi.cpp.o: In function `destroyContexts()':
driver_api_multi.cpp:(.text._Z15destroyContextsv+0xc): undefined reference to `cuCtxDestroy_v2'
driver_api_multi.cpp:(.text._Z15destroyContextsv+0x1c): undefined reference to `cuCtxDestroy_v2'
...

参考《Sample build failed with CUDA 5.5 and OpenCV 2.4.6.1》
修改 opencv/samples/gpu/CMakeLists.txt

target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS} ${OPENCV_GPU_SAMPLES_REQUIRED_DEPS})

target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS} ${OPENCV_GPU_SAMPLES_REQUIRED_DEPS} cuda)

编译安装

$ 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.4.1/modules \
    -D PYTHON_EXCUTABLE=/usr/bin/python \
    -D WITH_CUDA=ON \    # 使用CUDA
    -D WITH_CUBLAS=ON \
    -D DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" \
    -D CUDA_ARCH_BIN="5.3" \    # 这个需要去官网确认使用的GPU所对应的版本[查看这里](https://developer.nvidia.com/cuda-gpus)
    -D CUDA_ARCH_PTX="" \
    -D CUDA_FAST_MATH=ON \    # 计算速度更快但是相对不精确
    -D WITH_TBB=ON \
    -D WITH_V4L=ON \
    -D WITH_QT=ON \    # 如果qt未安装可以删去此行;若因为未正确安装qt导致的Qt5Gui报错,可将build内文件全部删除后重新cmake,具体可以参考[这里](http://stackoverflow.com/questions/17420739/opencv-2-4-5-and-qt5-error-s)
    -D WITH_GTK=ON \
    -D WITH_OPENGL=ON \
    -D BUILD_EXAMPLES=ON ..

然后

make -j8
sudo make install

展开阅读全文

没有更多推荐了,返回首页