在Ubuntu16.04/18.04 安装opencv3.3.0(可选带contrib和cuda)

Optional:contrib库、cuda库

注意版本对应:Ubuntu16.04对应安装cuda9,Ubuntu18.04对应安装cuda10

安装opencv前需验证cuda和cudnn是否正确安装。

Step 1: 更新包

sudo apt-get update
sudo apt-get upgrade

Step 2: 安装依赖项

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

Step 3:下载OpenCV 和 OpenCV_contrib 源码包

https://github.com/opencv/opencv/tree/3.3.0

https://github.com/opencv/opencv_contrib/tree/3.3.0

Step 4:修改源码包中某些文件

(参考https://blog.csdn.net/qq_15505637/article/details/79622968)

  • 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)  
  1. 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}")  
  1. 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文件

在common.hpp中添加#include

Step 5:编译安装

cd opencv-3.3.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.3.0/modules/ \
    -D CUDA_CUDA_LIBRARY=/usr/local/cuda/lib64/stubs/libcuda.so \
    -D CUDA_ARCH_BIN=7.5 \   # CUDA_ARCH_BIN 需要和自己的gpu对应 !!!
    -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 ..
make -j8
sudo make install

你可能感兴趣的:(软件安装)