在使用CMake编译OpenCV的CUDA时可能会出现一些问题,本文针对
CMake-gui
总结了部分问题并尝试解决。
本文给出的下载地址均为官方地址。
1. OpenCV CUDA 编译
- 安装CUDA Toolkit
CUDA Toolkit下载地址 - 安装cuDNN (版本要求高于7.5)
cuDNN下载地址 - 下载OpenCV源码和contrib包
OpenCV源码下载地址
contrib包下载地址(注意选则正确的版本) - CMake编译
分享一个编译教程,编译的版本与你的可能不一致,但流程完整。
win10编译opencv4.5.1+opencv_contrib+CUDA(带编译文件)
使用最新的
CUDA Toolkit
与cuDNN
,在VS编译时可能会失败,这里给出历史版本的下载地址:
2. CMake可能出现的问题
1) 找不到cuDNN
错误信息:
CMake Error at modules/dnn/CMakeLists.txt:53 (message):
DNN: CUDA backend requires cuDNN. Please resolve dependency or disable
OPENCV_DNN_CUDA=OFF
Call Stack (most recent call first):
modules/world/CMakeLists.txt:13 (include)
modules/world/CMakeLists.txt:50 (include_one_module)
解决方法:
- 首先确认
CUDA Toolkit
和cuDNN
是否正确安装。上文提到的编译教程中有检查方法。 - 若正确安装则,勾选
Advance
选项,搜索cudnn
,将CUDNN_LIBRARY
项设置为cudnn.lib
位置一般在C:\Program Files\NVIDIA\CUDNN\v9.0\lib\???\x64
下。
2) 找不到 Nvidia Video Codec SDK
错误信息:
CMake Warning at E:/opencv-4.9.0/opencv_contrib-4.x/opencv_contrib-4.x/modules/cudacodec/CMakeLists.txt:26 (message):
cudacodec::VideoReader requires Nvidia Video Codec SDK. Please resolve
dependency or disable WITH_NVCUVID=OFF
Call Stack (most recent call first):
modules/world/CMakeLists.txt:13 (include)
modules/world/CMakeLists.txt:50 (include_one_module)
CMake Warning at E:/opencv-4.9.0/opencv_contrib-4.x/opencv_contrib-4.x/modules/cudacodec/CMakeLists.txt:30 (message):
cudacodec::VideoWriter requires Nvidia Video Codec SDK. Please resolve
dependency or disable WITH_NVCUVENC=OFF
Call Stack (most recent call first):
modules/world/CMakeLists.txt:13 (include)
modules/world/CMakeLists.txt:50 (include_one_module)
解决方法:
- (首选)此项为英伟达视频编解码库,若不需要可以在CMake中取消勾选
WITH_NVCUVID
与WITH_NVCUVENC
解决。 - 若需要安装,则需要下载Nvidia Video Codec SDK(下载地址)下载完成后解压,将
Lib
和Interface
中所有文件分别复制到CUDA Toolkit文件夹下的lib
和include
中解决。
3)各种因为网络的错误
错误信息:
wechat_qrcode: Downloading detect.prototxt from https://raw.githubusercontent.com/WeChatCV/opencv_3rdparty/a8b69ccc738421293254aec5ddb38bd523503252/detect.prototxt
Try 1 failed
=======================================================================
Couldn't connect to server from the Internet.
Perhaps direct connections are not allowed in the current network.
To use proxy please check/specify these environment variables:
- http_proxy/https_proxy
- and/or HTTP_PROXY/HTTPS_PROXY
=======================================================================
解决方法:
- 当然是可以在CMake取消模块编译来解决。
- 如果要安装该模块。首先,观察错误信息发现是
wechat_qrcode
模块出错,手动下载后面的链接:https://raw.githubusercontent.com/WeChatCV/opencv_3rdparty/a8b69ccc738421293254aec5ddb38bd523503252/detect.prototxt
然后,进入源码文件夹里的.cache
文件夹,里面存放各个模块所需下载的文件。
最后,将手动下载的文件放入对应文件夹并且按照文件夹内文件命名解决。
可以参考CMake编译OpenCV4.0时opencv_ffmpeg.dll等下载失败的解决思路总结
题外话:好像没办法使用mingw编译支持多平台的OpenCV CUDA,有没有大佬解惑orz。