1.安装 Visual Studio 2017
勾选适用于桌面的 VC++ 2015.3 v14.00(v140) 工具集
2.安装 CUDA 9.2
先安装 Base Installer
再安装 Patch 1 (Released Aug 16, 2018)
设置环境变量
CUDA_PATH = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2
CUDA_PATH_V9_2 = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2
CUDA_LIB_PATH = %CUDA_PATH%\lib\x64
CUDA_BIN_PATH = %CUDA_PATH%\bin
CUDA_SDK_PATH = C:\ProgramData\NVIDIA Corporation\CUDA Samples\v9.2
CUDA_SDK_BIN_PATH = %CUDA_SDK_PATH%\bin\win64
CUDA_SDK_LIB_PATH = %CUDA_SDK_PATH%\common\lib\x64
检查环境变量 set cuda
验证安装,进入
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\extras\demo_suite
目录,执行 deviceQuery.exe
执行
bandwidthTest.exe
Result = PASS 说明安装成功
3.安装 cuDNN 7.5, for CUDA 9.2
解压 cudnn-9.2-windows10-x64-v7.5.0.56.zip
,将 cudnn-9.2-windows10-x64-v7.5.0.56\cuda\bin\cudnn64_7.dll
复制到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\bin
将 cudnn-9.2-windows10-x64-v7.5.0.56\cuda\include\cudnn.h
复制到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\include
将 cudnn-9.2-windows10-x64-v7.5.0.56\cuda\lib\x64\cudnn.lib
复制到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\lib\x64
检查环境变量
Variable Name: CUDA_PATH
Variable Value: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2
4.安装 OpenCV 3.4.5
下载完成后解压
5.使用 CMake 编译 OpenCV
勾选
WITH_CUDA
,否则使用 GPU 模块时会出现问题重新生成解决方案
选择
仅生成 INSTALL
添加环境变量
D:\Programming\OpenCV\opencv\build\install\x64\vc15\bin
6.测试 CUDA
进入 C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64
执行 cl.exe
:
看到
cl.exe
的版本为 19.16.27027.1进入
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64>cd C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\include\crt
查看 host_config.h
,发现第 131 行为:
将 1913 修改为 1916
7.配置 Visual Studio
新建空项目,配置项目属性:
在属性 - 配置属性 - VC++目录 - 包含目录中添加路径:
D:\Programming\OpenCV\opencv\build\install\include
D:\Programming\OpenCV\opencv\build\install\include\opencv
D:\Programming\OpenCV\opencv\build\install\include\opencv2
在属性 - 配置属性 - VC++目录 - 库目录中添加路径:
D:\Programming\OpenCV\opencv\build\install\x64\vc15\lib
在属性 - 配置属性 - 链接器 - 输入 - 附加依赖项中添加:
opencv_calib3d345d.lib opencv_core345d.lib opencv_cudaarithm345d.lib opencv_cudabgsegm345d.lib opencv_cudacodec345d.lib opencv_cudafeatures2d345d.lib opencv_cudafilters345d.lib opencv_cudaimgproc345d.lib opencv_cudalegacy345d.lib opencv_cudaobjdetect345d.lib opencv_cudaoptflow345d.lib opencv_cudastereo345d.lib opencv_cudawarping345d.lib opencv_cudev345d.lib opencv_dnn345d.lib opencv_features2d345d.lib opencv_flann345d.lib opencv_highgui345d.lib opencv_imgcodecs345d.lib opencv_imgproc345d.lib opencv_ml345d.lib opencv_objdetect345d.lib opencv_photo345d.lib opencv_shape345d.lib opencv_stitching345d.lib opencv_superres345d.lib opencv_video345d.lib opencv_videoio345d.lib opencv_videostab345d.lib
右键项目 - 生成依赖项 - 生成自定义 - CUDA 9.2(.targets, .props)
在属性 - 配置属性 - VC++目录 - 包含目录中添加路径:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\include
在属性 - 配置属性 - 链接器 - 常规 - 附加库目录中添加路径:
$(CUDA_PATH_V9_2)\lib\$(Platform)
在属性 - 配置属性 - 链接器 - 输入 - 附加依赖项中添加库:
cublas.lib;cublas_device.lib;cuda.lib;cudadevrt.lib;cudart.lib;cudart_static.lib;cufft.lib;cufftw.lib;curand.lib;cusolver.lib;cusparse.lib;nppc.lib;nppial.lib;nppicc.lib;nppicom.lib;nppidei.lib;nppif.lib;nppig.lib;nppim.lib;nppist.lib;nppisu.lib;nppitc.lib;npps.lib;nvblas.lib;nvcuvid.lib;nvgraph.lib;nvml.lib;nvrtc.lib;OpenCL.lib;
设置完成后右键
源文件 - 添加 - 新建项 - CUDA C/C++ File
7.导出模板
8.执行程序
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/cuda.hpp>
using namespace std;
using namespace cv;
using namespace cuda;
int main() {
DeviceInfo deviceInfo;
bool isDeviceOK = deviceInfo.isCompatible();
cout << "Is GPU OK:" << isDeviceOK << endl;
system("pause");
return 0;
}
参考链接:
https://blog.csdn.net/caihaocong/article/details/80360410
https://blog.csdn.net/u013165921/article/details/77891913
https://www.cnblogs.com/wayne793377164/p/8185404.html
https://blog.csdn.net/wolffytom/article/details/49976487
https://blog.csdn.net/u010763864/article/details/81632469