目录
1.系统环境
2.安装一些基础命令
2.1更新包
2.2安装依赖项
3.下载OpenCV 和 OpenCV_contrib 源码包
3.1下载地址
3.2解压并移动opencv_contrib-3.3.0到opencv-3.3.0
4.修改源码包中某些文件
5.编译安装opencv-3.3.0
5.1获取配置参数
5.2生成配置
5.3编译
5.4安装
系统:ubuntu16.04
GPU驱动:nvidia-driver-418-server
CUDA版本:cuda10.1
CUDNN版本:cudnn7.6.4
Anaconda版本:Anaconda5.2 (python3.6)
sudo apt update
sudo apt upgrade
sudo apt-get remove x264 libx264-dev
sudo apt-get install build-essential checkinstall cmake pkg-config yasm
sudo apt-get install git gfortran
sudo apt-get install libjpeg8-dev libjasper-dev libpng12-dev
sudo apt-get install libtiff5-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt-get install libxine2-dev libv4l-dev
sudo apt-get install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
sudo apt-get install qt5-default libgtk2.0-dev libtbb-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install libvorbis-dev libxvidcore-dev
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install x264 v4l-utils
# Optional dependencies
sudo apt-get install libprotobuf-dev protobuf-compiler
sudo apt-get install libgoogle-glog-dev libgflags-dev
sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
GitHub - opencv/opencv at 3.3.0
GitHub - opencv/opencv_contrib at 3.3.0
lu@computer:~/project$ ll
total 137564
drwxrwxr-x 4 lu lu 4096 5月 24 14:06 ./
drwxr-xr-x 35 lu lu 4096 5月 24 09:58 ../
drwxrwxr-x 11 lu lu 4096 8月 4 2017 opencv-3.3.0/
-rw-rw-r-- 1 lu lu 84873317 5月 24 14:05 opencv-3.3.0.zip
drwxrwxr-x 6 lu lu 4096 7月 31 2017 opencv_contrib-3.3.0/
-rw-rw-r-- 1 lu lu 55968854 5月 24 14:05 opencv_contrib-3.3.0.zip
lu@computer:~/project$ mv opencv_contrib-3.3.0 opencv-3.3.0
lu@computer:~/project$ cd opencv-3.3.0/
lu@computer:~/project/opencv-3.3.0$ ll
total 128
drwxrwxr-x 12 lu lu 4096 5月 24 14:07 ./
drwxrwxr-x 3 lu lu 4096 5月 24 14:07 ../
drwxrwxr-x 18 lu lu 4096 8月 4 2017 3rdparty/
drwxrwxr-x 8 lu lu 4096 8月 4 2017 apps/
drwxrwxr-x 5 lu lu 4096 8月 4 2017 cmake/
-rw-rw-r-- 1 lu lu 64394 8月 4 2017 CMakeLists.txt
-rw-rw-r-- 1 lu lu 191 8月 4 2017 CONTRIBUTING.md
drwxrwxr-x 7 lu lu 4096 8月 4 2017 data/
drwxrwxr-x 5 lu lu 4096 8月 4 2017 doc/
drwxrwxr-x 4 lu lu 4096 8月 4 2017 include/
-rw-rw-r-- 1 lu lu 2234 8月 4 2017 LICENSE
drwxrwxr-x 36 lu lu 4096 8月 4 2017 modules/
drwxrwxr-x 6 lu lu 4096 7月 31 2017 opencv_contrib-3.3.0/
drwxrwxr-x 9 lu lu 4096 8月 4 2017 platforms/
-rw-rw-r-- 1 lu lu 569 8月 4 2017 README.md
drwxrwxr-x 19 lu lu 4096 8月 4 2017 samples/
-rw-rw-r-- 1 lu lu 33 8月 4 2017 .tgitconfig
lu@computer:~/project/opencv-3.3.0$
(参考https://blog.csdn.net/qq_15505637/article/details/79622968)
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)
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")
common.hpp文件
u@computer:~/project/opencv-3.3.0$ find ./ -name common.hpp
./3rdparty/carotene/src/common.hpp
./samples/android/tutorial-4-opencl/jni/common.hpp
./modules/cudev/include/opencv2/cudev/common.hpp
./modules/core/include/opencv2/core/cuda/common.hpp
lu@computer:~/project/opencv-3.3.0$
在3rdparty和modules模块中的common.hpp中添加#include
找到自己的cuda库安装路径CUDA_CUDA_LIBRARY:
我的本地路径是:
/usr/local/cuda/lib64/stubs/libcuda.so
然后确定CUDA_ARCH_BIN ,需要和自己的gpu对应,假设安装的是cuda-10.1,则
lu@computer:~$
lu@computer:~$ cd /usr/local/cuda/samples/1_Utilities/deviceQuery
lu@computer:/usr/local/cuda/samples/1_Utilities/deviceQuery$
lu@computer:/usr/local/cuda/samples/1_Utilities/deviceQuery$ sudo make
[sudo] password for lu:
/usr/local/cuda/bin/nvcc -ccbin g++ -I../../common/inc -m64 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_75,code=compute_75 -o deviceQuery.o -c deviceQuery.cpp
/usr/local/cuda/bin/nvcc -ccbin g++ -m64 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_75,code=compute_75 -o deviceQuery deviceQuery.o
mkdir -p ../../bin/x86_64/linux/release
cp deviceQuery ../../bin/x86_64/linux/release
lu@computer:/usr/local/cuda/samples/1_Utilities/deviceQuery$ ./deviceQuery
./deviceQuery Starting...
CUDA Device Query (Runtime API) version (CUDART static linking)
Detected 1 CUDA Capable device(s)
Device 0: "GeForce 940M"
CUDA Driver Version / Runtime Version 10.1 / 10.1
CUDA Capability Major/Minor version number: 5.0
Total amount of global memory: 2004 MBytes (2101870592 bytes)
( 3) Multiprocessors, (128) CUDA Cores/MP: 384 CUDA Cores
GPU Max Clock rate: 1150 MHz (1.15 GHz)
Memory Clock rate: 900 Mhz
Memory Bus Width: 64-bit
L2 Cache Size: 1048576 bytes
Maximum Texture Dimension Size (x,y,z) 1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)
Maximum Layered 1D Texture Size, (num) layers 1D=(16384), 2048 layers
Maximum Layered 2D Texture Size, (num) layers 2D=(16384, 16384), 2048 layers
Total amount of constant memory: 65536 bytes
Total amount of shared memory per block: 49152 bytes
Total number of registers available per block: 65536
Warp size: 32
Maximum number of threads per multiprocessor: 2048
Maximum number of threads per block: 1024
Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535)
Maximum memory pitch: 2147483647 bytes
Texture alignment: 512 bytes
Concurrent copy and kernel execution: Yes with 1 copy engine(s)
Run time limit on kernels: Yes
Integrated GPU sharing Host Memory: No
Support host page-locked memory mapping: Yes
Alignment requirement for Surfaces: Yes
Device has ECC support: Disabled
Device supports Unified Addressing (UVA): Yes
Device supports Compute Preemption: No
Supports Cooperative Kernel Launch: No
Supports MultiDevice Co-op Kernel Launch: No
Device PCI Domain ID / Bus ID / location ID: 0 / 4 / 0
Compute Mode:
< Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 10.1, CUDA Runtime Version = 10.1, NumDevs = 1
Result = PASS
lu@computer:/usr/local/cuda/samples/1_Utilities/deviceQuery$
然后找到输出信息中的CUDA Capability Major/Minor version number。
我的是5.0
cd opencv-3.3.0
mkdir build_cuda
cd build_cuda
cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.3.0/modules/ \
-D CUDA_CUDA_LIBRARY=/usr/local/cuda/lib64/stubs/libcuda.so \
-D CUDA_ARCH_BIN=5.0 \
-D CUDA_ARCH_PTX="" \
-D WITH_CUDA=ON \
-D WITH_TBB=ON \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D WITH_V4L=ON \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_EXAMPLES=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_NVCUVID=1 \
-D BUILD_opencv_cudacodec=OFF ..
可能遇到的问题:
编译器版本不对报错
"not supported c++11"
opencv-3.3.0编译要用到C++11,需要把编译器版本升级到gcc-7和g++-7
卡在IPPICV下载
-- IPPICV: Download: ippicv_2017u2_lnx_intel64_20170418.tgz
多等待一会一般会自动跳过,或者手动下载,网上有很多教程,这里不再赘述
opencv_3rdparty/ippicv at ippicv/master_20170418 · opencv/opencv_3rdparty · GitHub
缺少videodev.h
linux/videodev.h not found
执行下面命令
sudo ln -s /usr/include/libv4l1-videodev.h /usr/include/linux/videodev.h
缺少videoio.h
sys/videoio.h not found
执行下面命令
sudo cp /usr/include/opencv2/videoio/videoio_c.h /usr/include/x86_64-linux-gnu/sys
sudo ln -s /usr/include/x86_64-linux-gnu/sys/videoio_c.h /usr/include/x86_64-linux-gnu/sys/videoio.h
配置成功如下
-- To be built: cudev core cudaarithm flann hdf imgproc ml objdetect phase_unwrapping plot reg surface_matching video xphoto bgsegm cudabgsegm cudafilters cudaimgproc cudawarping dnn face fuzzy img_hash imgcodecs photo shape videoio xobjdetect highgui ts bioinspired dpm features2d line_descriptor saliency text calib3d ccalib cudafeatures2d cudalegacy cudaobjdetect cudaoptflow cudastereo cvv datasets rgbd stereo structured_light superres tracking videostab xfeatures2d ximgproc aruco optflow stitching
-- Disabled: cudacodec world contrib_world
-- Disabled by dependency: -
-- Unavailable: java python2 python3 viz cnn_3dobj dnn_modern freetype matlab sfm xfeatures2d
--
-- GUI:
-- QT 5.x: YES (ver 5.9.5)
-- QT OpenGL support: YES (Qt5::OpenGL 5.9.5)
-- OpenGL support: YES (/usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/x86_64-linux-gnu/libGLU.so)
-- VTK support: NO
--
-- Media I/O:
-- ZLib: zlib (ver 1.2.8)
-- JPEG: libjpeg (ver 90)
-- WEBP: build (ver encoder: 0x020e)
-- PNG: build (ver 1.6.24)
-- TIFF: build (ver 42 - 4.0.2)
-- JPEG 2000: build (ver 1.900.1)
-- OpenEXR: build (ver 1.7.1)
-- GDAL: NO
-- GDCM: NO
--
-- Video I/O:
-- DC1394 1.x: NO
-- DC1394 2.x: NO
-- FFMPEG: NO
-- avcodec: NO
-- avformat: NO
-- avutil: NO
-- swscale: NO
-- avresample: NO
-- GStreamer: NO
-- OpenNI: NO
-- OpenNI PrimeSensor Modules: NO
-- OpenNI2: NO
-- PvAPI: NO
-- GigEVisionSDK: NO
-- Aravis SDK: NO
-- UniCap: NO
-- UniCap ucil: NO
-- V4L/V4L2: NO/YES
-- XIMEA: NO
-- Xine: NO
-- Intel Media SDK: NO
-- gPhoto2: NO
--
-- Parallel framework: pthreads
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Use Intel IPP: 2017.0.2 [2017.0.2]
-- at: /home/lu/project/opencv-3.3.0/build_cuda/3rdparty/ippicv/ippicv_lnx
-- Use Intel IPP IW: prebuilt binaries (2017.0.2)
-- Use Intel IPP Async: NO
-- Use VA: NO
-- Use Intel VA-API/OpenCL: NO
-- Use Lapack: NO
-- Use Eigen: NO
-- Use Cuda: YES (ver 10.1)
-- Use OpenCL: YES
-- Use OpenVX: NO
-- Use custom HAL: NO
--
-- NVIDIA CUDA
-- Use CUFFT: YES
-- Use CUBLAS: YES
-- USE NVCUVID: YES
-- NVIDIA GPU arch: 50
-- NVIDIA PTX archs:
-- Use fast math: YES
--
-- OpenCL:
-- Include path: /home/lu/project/opencv-3.3.0/3rdparty/include/opencl/1.2
-- Use AMDFFT: NO
-- Use AMDBLAS: NO
--
-- Python 2:
-- Interpreter: NO
--
-- Python 3:
-- Interpreter: /home/lu/anaconda3/bin/python3 (ver 3.6.5)
--
-- Python (for build): /home/lu/anaconda3/bin/python3
--
-- Java:
-- ant: NO
-- JNI: NO
-- Java wrappers: NO
-- Java tests: NO
--
-- Matlab: Matlab not found or implicitly disabled
--
-- Documentation:
-- Doxygen: NO
--
-- Tests and samples:
-- Tests: YES
-- Performance tests: YES
-- C/C++ Examples: YES
--
-- Install path: /usr/local
--
-- cvconfig.h is in: /home/lu/project/opencv-3.3.0/build_cuda
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lu/project/opencv-3.3.0/build_cuda
lu@computer:~/project/opencv-3.3.0/build_cuda$
make -j8
可能报错:
在编译stitching模块时会提示:
找不到cuda.hpp即matchers.hpp:52:42: fatal error: opencv2/xfeatures2d/cuda.hpp: No such file or directory
opencv-3.3.0/modules/stitching/CMakeLists.txt文件中加入一条语句使其include opencv_contrib-3.3.0/modules/xfeatures2d/include,可以是绝对路径,如
INCLUDE_DIRECTORIES("/home/lu/project/opencv-3.3.0/opencv_contrib-3.3.0/modules/xfeatures2d/include")
缺失错误:
opencv_contrib/modules/xfeatures2d/src/boostdesc.cpp:673:20: fatal error: boostdesc_bgm.i: No such file or directory
先检查本地download目录下是否下载成功,有的话直接拷贝源码并生存同名文件,放在 opencv_contrib/modules/xfeatures2d/src/ 路径下
lu@computer:~/project/opencv-3.3.0/build_cuda$ ls downloads/xfeatures2d/ -l
total 3296
-rw-rw-r-- 1 lu lu 14864 5月 24 16:06 boostdesc_bgm_bi.i
-rw-rw-r-- 1 lu lu 7832 5月 24 14:37 boostdesc_bgm_hd.i
-rw-rw-r-- 1 lu lu 14852 5月 24 14:36 boostdesc_bgm.i
-rw-rw-r-- 1 lu lu 138093 5月 24 14:37 boostdesc_binboost_064.i
-rw-rw-r-- 1 lu lu 275322 5月 24 14:37 boostdesc_binboost_128.i
-rw-rw-r-- 1 lu lu 549754 5月 24 14:39 boostdesc_binboost_256.i
-rw-rw-r-- 1 lu lu 426280 5月 24 14:39 boostdesc_lbgm.i
-rw-rw-r-- 1 lu lu 915012 5月 24 14:40 vgg_generated_64.i
-rw-rw-r-- 1 lu lu 1012932 5月 24 14:43 vgg_generated_80.i
lu@computer:~/project/opencv-3.3.0/build_cuda$
lu@computer:~/project/opencv-3.3.0/build_cuda$ cp -rf downloads/xfeatures2d/* ../opencv_contrib-3.3.0/modules/xfeatures2d/src/
lu@computer:~/project/opencv-3.3.0/build_cuda$
如果本地没有可以单独下载
在日志文件CMakeDownloadLog.txt中搜索 boostdesc_bgm.i 关键词 ,日志文件里就有它的下载地址,直接下载拷贝放在 opencv_contrib/modules/xfeatures2d/src/ 路径下即可
如果基于cuda11.1编译opencv3.3会报错如下:
**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 /home/lu/project/opencv-3.3.0/modules/cudev
find ./ -name FindCUDA.cmake指令找到FindCUDA.cmake文件
找到find_cuda_helper_libs(nppicom)改成:
if(CUDA_VERSION VERSION_LESS "11.0")
find_cuda_helper_libs(nppicom)
endif()
之所以会报错是因为在CUDA 11.0中,移除了这个库。但是CMAKE好像没及时更新。因此,信息不同步,导致报错。
编译完成如下
[ 99%] Linking CXX executable ../../bin/cpp-example-3calibration
[ 99%] Built target tutorial_Drawing_2
[ 99%] Built target tutorial_file_input_output
[100%] Linking CXX executable ../../bin/cpp-tutorial-camera_calibration
[100%] Built target example_3calibration
[100%] Built target tutorial_camera_calibration
[100%] Linking CXX executable ../../bin/cpp-tutorial-planar_tracking
[100%] Built target tutorial_planar_tracking
lu@computer:~/project/opencv-3.3.0/build_cuda$
sudu make install
参考:
在Ubuntu16.04/18.04 安装opencv3.3.0(可选带contrib和cuda)_仰望天空-脚踏实地-CSDN博客
Ubuntu18.04安装Opencv3.3.0(带CUDA10.1)_博博有个大大大的Dream-CSDN博客