ubuntu 18.04配置opencv3.4.5+contrib+cuda10.0

一:准备工作

先配置好opencv3.4.5+contrib,这样,只需要在此基础上继续配置cuda就行,关于如何配置opencv-3.4.5+opencv-3.4.5_contrib可以看这篇博客:opencv-3.4.5+contrib
然后就是下载cuda 10.0,下载地址:cuda10.0下载
ubuntu 18.04配置opencv3.4.5+contrib+cuda10.0_第1张图片

二 安装CUDA

安装

在你下载cuda的位置打开终端,并输入:

sudo sh cuda_10.0.130_410.48_linux.run

然后疯狂回车,直至100%,接下来,会进行选择,如下

Do you accept the previously read EULA?
accept
Install NVIDIA Accelerated Graphics Driver for linux-x86_64 410.48?
n
Install the CUDA 10.0 Toolkit?
y
Enter Toolkit Location?
[default is /usr/local/cuda-10.0]:
Do you want to install a symbolic link at /usr/local/cuda?
y
Install the CUDA 10.0 Samples?
y
Enter CUDA Samples Location
[ default is /home/txz ]:

接下来会提示:
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 384.00 is required for CUDA 10.0 functionality to work.忽略就行

配置环境变量

在主目录下按ctrl+h,找到 .bashrc文件并打开,在其中添加以下内容:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-10.0/lib64
export PATH=$PATH:/usr/local/cuda-10.0/bin
export CUDA_HOME=$CUDA_HOME:/usr/local/cuda-10.0

用su切换到root,再运行

source ~/.bashrc

检测是否安装成功

用cuda中的程序进行检测

cd /usr/local/cuda/samples/1_Utilities/deviceQuery
sudo make 
./deviceQuery

pass说明安装成功。

三 配置CUDA版opencv

首先进入安装opencv的文件夹下,然后新建文件夹build_cuda,再进入该文件夹。

cd opencv-3.4.5
mkdir build_cuda
cd build_cuda

然后进行cmake,cmake参数如下:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.4.5/modules/ -D CUDA_CUDA_LIBRARY=/usr/local/cuda/lib64/stubs/libcuda.so -D CUDA_ARCH_BIN=7.5 -D CUDA_ARCH_PTX=""   -D WITH_CUDA=ON -D WITH_TBB=ON -D WITH_V4L=ON -D INSTALL_C_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 ..

唯一要注意的是CUDA_ARCH_BIN.该参数可以通过终端输入nvidia-smi得到显卡信息,然后百度得到计算能力。或者去NVIDIA官网查询自己显卡对应能力。
然后

sudo make -j8
sudo make install

配置环境变量

也就是配置.bashrc文件

    echo '/usr/local/lib' | sudo tee -a /etc/ld.so.conf.d/opencv.conf    
    sudo ldconfig    
    printf '# OpenCV\nPKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig\nexport PKG_CONFIG_PATH\n' >> ~/.bashrc    
    source ~/.bashrc    

ok,到这一步基本可以宣告结束了,最后再测试一波。

测试

通用CMakeList.txt.

# cmake needs this line
cmake_minimum_required(VERSION 2.8)

# Define project name
project(test)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV 3.3.0 REQUIRED)

# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

if(CMAKE_VERSION VERSION_LESS "2.8.11")
  # Add OpenCV headers location to your include paths
  include_directories(${OpenCV_INCLUDE_DIRS})
endif()

# Declare the executable target built from your sources
add_executable(opencv_example main.cpp)

# Link your application with OpenCV libraries
target_link_libraries(opencv_example ${OpenCV_LIBS})


测试代码

using namespace std;
#include "opencv2/opencv.hpp"
#include "opencv2/core/cuda.hpp"
using namespace cv;
using namespace cv::cuda;
int main()
{
    int num_devices = cv::cuda::getCudaEnabledDeviceCount();

    cout<<num_devices<<endl;
}

结果为1就可以啦。

你可能感兴趣的:(总结)