Ubuntu16.04 编译OpenCV3.4.1 CUDA8.0

1、下载OpenCV3.4.1

OpenCV3.4.1包括稳定功能模块版本opencv3.4.1和未稳定功能模块版本opencv_contrib3.4.1,在opencv3.4.1中,主要增强了dnn模块,特别是添加了对Faster R-CNN的支持,但有些算法还没有加进来,比如DPM,KCF等,因此本次编译加上了opencv_contrib3.4.1。可以自行去OpenCV github上打包下载opencv3.4.1和opencv_contrib3.4.1,也可以通过如下命令下载压缩包,解压后将两个文件夹放在同一目录下:

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

2、安装依赖

官方给出的依赖包如下:

  • 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 cmake git libgtk2.0-dev pkg-config
sudo apt-get install 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

3、配置编译opencv

cd opencv-3.4.1
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv3.4.1 -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.1/modules -D WITH_CUDA=ON -D WITH_CUBLAS=1 -D CUDA_ARCH_BIN="6.1" -D CUDA_ARCH_PTX="6.1" -D INSTALL_C_EXAMPLES=OFF -D INSTALL_PYTHON_EXAMPLES=ON -D WITH_OPENGL=ON -D WITH_V4L=ON WITH_NVCUVID=ON ..

make
sudo make install
sudo sh -c 'echo "/usr/local/opencv3.4.1/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

注意
1.CMAKE_INSTALL_PREFIX默认目录为/usr/local,可自行选择,只需要在更新环境变量的时候对应更改就行。
2.CUDA_ARCH_BIN和CUDA_ARCH_PTX这里是指的显卡计算能力,GeForce GTX 1080 Ti的计算能力为6.1,其他型号可以自行上NVIDIA官网查询对应版本:
https://developer.nvidia.com/cuda-gpus
3.cmake的时候,有些contrib模块编译时需要的文件可能下载不下来,具体信息可以到build目录下CMakeDownloadLog.txt查看,去对应网址下载好文件放到opencv-3.4.1/.cache目录下(注意这是个隐藏文件)的对应文件夹里,并重命名(加上前面的一串字符,注意要一一对应),再重新cmake。这里我将自己下载的缺少的文件(data,ippicv,tiny_dnn,dnn_face_detector,xfeatures2d已经重命名好)放到网盘上:
https://pan.baidu.com/s/1MSZIrVzl38Xj6rRRzfA0Kg

4、编译时遇到的一些错误提示

1.opencv_cudawarping和libopencv_cudafilter.so

[ 54%] Linking CXX shared library ../../lib/libopencv_cudawarping.so
[ 54%] Built target opencv_cudawarping
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2

## libopencv_cudafilter.so
[ 54%] Linking CXX shared library ../../lib/libopencv_cudafilters.so
[ 54%] Built target opencv_cudafilters
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2

这里是我在用到make -j16或make -j8时碰到的,最后编译成功那次我只用了make
参考链接:https://devtalk.nvidia.com/default/topic/1016293/jetson-tx1/error-while-compiling-opencv-on-jetson-tx1/

2.undefined reference to 'inflateValidate@ZLIB_1.2.9'

//home/mmap/anaconda3/lib/libpng16.so.16:undefined reference to'inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status

每次将make -j8 改成make的时候就提示这个错误,不知道为什么一开始不提示,(可能上面的错误主要原因也是这个,可以试试解决这个问题后用make -j4 或 make -j8)这里是cmake的时候zlib和libpng索引到anaconda下面的库文件了,而anaconda下的这两个库文件版本不知道是太新了还是有冲突,最后直接conda卸载这两个库文件,让cmake的时候索引到系统默认版本(/usr/local/lib目录下)

你可能感兴趣的:(Ubuntu16.04 编译OpenCV3.4.1 CUDA8.0)