windows:Opencv使用Cmake & VS 编译, C++、python、Cuda、Qt 环境详细教学及踩坑 一 :编译篇
Bethxone 2024-08-19 15:05:04 阅读 74
求赞求赞求赞,若有帮助,鼓励一下
测试篇:windows:Opencv使用Cmake & VS 编译, C++、python、Cuda、Qt 环境详细教学及踩坑 二 :测试篇-CSDN博客
一、准备:
本文使用的软件版本均是撰写时的最新或次新版,对过旧版本不一定通用
0、环境配置:
Windows | Windows 11 专业版 23H2 |
opencv | 4.10 |
Qt | 6.7.2 |
Cmake | 3.3.0 |
python | 3.12.4 |
cuda | 12.5 |
cudnn | 9.2.1 |
vs studio | 2022 |
1、 Opencv,Opencv_contrib 源码下载 本文版本:4.10
GitHub - opencv/opencv: Open Source Computer Vision Library
GitHub - opencv/opencv_contrib: Repository for OpenCV's extra modules
下载完成后解压opencv 和 opencv_contrib,将opencv_contrib文件夹移入opencv文件夹下,并在opencv文件夹下新建build文件夹。
软件安装教程引用其他大神的教程,很详细
2、Qt 安装 本文版本:Qt 6.7.2
Qt下载安装及配置教程(非常详细),从零基础入门到精通,看完这一篇就够了-CSDN博客
3、Cmake 安装: 本文版本:Cmake 3.3.0
Windows下的CMake详细安装教程(2024/1/24)_windows 安装cmake-CSDN博客
4、python 安装: 本文版本:python 3.12.4
Download Python | Python.org
1、选择自定义安装
2、选择添加python路径到环境变量
3、选择为所有用户安装
其他选项如图所示,如果cmake需要debug版本的一定要勾选debug有关的选项,否则生成过程中会缺少python3xx_d.lib 文件。
5、Cuda 、cudNN 安装:本文版本:Cuda 12.5 cudNN 9.2
先按该教程安装cuda、cudnn:Cuda和cuDNN安装教程(超级详细)-CSDN博客
cudnn再按官方文档安装一遍:英伟达官方cudnn安装教程
注意:cudnn一定要按教程,将cudnn文件复制到cuda文件夹下。
否则:如果只是按照官方文档中的教程,编译成功后,python import cv2 时无法调用cudnn中的dll文件。报错:
ImportError: DLL load failed: The specified module could not be found.
!!注意:Windows下使用cmake编译cuda只能使用VS,否则会无法找到cuda位置
6、VS studio 安装:
Thank You for Downloading Visual Studio Community Edition
7、上述软件安装完成后重启计算机!!!
二、设置 Cmake 选项
参考:AMD Customer Community
从“开始”菜单打开 CMake (cmake-gui)将“Where is the source code”设置为提取的 opencv 文件夹。将“Where to build the binaries”设置为提取的 opencv 文件夹,并在末尾附加 /build。这将是编译文件的构建位置,您可以将其设置为所需的任何文件夹点击“configure”弹出窗口选择vs2019作为编译器
generate平台选择x64
注:此处 opencv_cp312_410、opencv_410_cp312_amd64_bulid、opencv_410_cp312_amd64为笔者按习惯重命名。
对应关系为:
opencv_cp312_410 ——>opencv_x.xx opencv主文件夹
opencv_410_cp312_amd64_bulid ——> build
opencv_410_cp312_amd64 ——> install
此处的目录结构为:
----opencv_x.xx
----3rdparty
---- .....
----build
----install
---- .....
---- opencv_contrib
1、通用编译选项:
参考链接:AMD Customer Community
使用“搜索”框更改以下属性:
BUILD_PROTOBUF:– 取消选中WITH_PROTOBUF:– 取消选中BUILD_TESTS:– 取消选中WITH_OPENEXR:– 取消选中BUILD_OPENEXR:– 取消选中OPENCV_ENABLE_ALLOCATOR_STATS:– 取消选中CMAKE_BUILD_TYPE:– RELEASECMAKE_INSTALL_PREFIX:– 这默认为 build/install 目录,但您可以将其更改为您希望将 OpenCV 库安装到的任何目录。BUILD_opencv_world:建议勾选,build_opencv_world可以将所有opencv的库都打包编译在一起。OPENCV_ENABLE_NONFREE:勾选,否则无法使用sift。WITH_OPENGL :勾选OPENCV_EXTRA_MODULES_PATH – 设置为提取的 opencv_contrib/modules 文件夹的路径。如图所示:
10、配置完成点击“configure”
报错信息1:
CMake Warning (dev) at CMakeLists.txt:127 (enable_language):
project() should be called prior to this enable_language() call.
This warning is for project developers. Use -Wno-dev to suppress it.
Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.22631.
CMake Warning (dev) at C:/Program Files/CMake/share/cmake-3.30/Modules/Platform/Windows-MSVC.cmake:539 (enable_language):
project() should be called prior to this enable_language() call.
Call Stack (most recent call first):
C:/Program Files/CMake/share/cmake-3.30/Modules/Platform/Windows-MSVC.cmake:509 (__windows_compiler_msvc_enable_rc)
C:/Program Files/CMake/share/cmake-3.30/Modules/Platform/Windows-MSVC-CXX.cmake:6 (__windows_compiler_msvc)
C:/Program Files/CMake/share/cmake-3.30/Modules/CMakeCXXInformation.cmake:48 (include)
CMakeLists.txt:127 (enable_language)
This warning is for project developers. Use -Wno-dev to suppress it.
报错信息2:
CMake Warning at cmake/OpenCVGenSetupVars.cmake:54 (message):
CONFIGURATION IS NOT SUPPORTED: validate setupvars script in install
directory
Call Stack (most recent call first):
CMakeLists.txt:1180 (include)
解决方案:
这两类警告,不影响正常编译过程。对此未作处理。
2、cuda 编译选项
参考链接:windows11编译OpenCV4.5.0 with CUDA(附注意事项)-CSDN博客
第一次搜cuda时将带cuda的都勾选,点击“configure”
搜索:WITH_NVCUVENC——取消选中
WITH_NVCUVID——取消选中
注意:第一次搜索时,如图所示的部分选项不存在是正常现象,因为每次"configure"时,cmake都会根据已经勾选的选项重新加载,所以及时的重新"configure"是一个好习惯。
将已经存在的选项按图勾选后,再次点击"configure"后重新搜索,即可出现全部选项。
配置CUDA_ARCH_BIN中将显卡的算力内容改成自己显卡的算力,显卡算力查看链接查看自己显卡的算力,我的显卡是4060laptop,官网算力为8.9。CUDA_ARCH_BIN中将低于8.9数值的删除。
cuda编译选项按图进行勾选。
报错信息1:WITH_NVCUVENC、WITH_NVCUVID
CMake Warning at opencv_contrib_4_10/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 opencv_contrib_4_10/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)
解决方法:
搜索:WITH_NVCUVENC——取消选中
WITH_NVCUVID——取消选中
报错信息2:OPENCV_DNN_CUDA
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)
解决方法:
搜索CUDNN_LIBRARY,找到并输入cudnn.lib的安装位置
CUDA 参考配置:
打开 Advance,配置供参考(不一定要一致,只是提供参考)
3、python 编译选项
参考:【opencv】【GPU】windows10下opencv4.8.0-cuda Python版本源码编译教程_cv2的版本4.8.0-CSDN博客
注意:有的同学即使按图勾选后依然无法出现某些选项。
例如:
若无法出现BUILD_opencv_python3,(此选项为python编译的必需选项)此时可以进行手动添加。点击 Add Entry ,在Name 中输入BUILD_opencv_python3,点击OK,即可出现该选项。
python的选项设置如图所示,其中numpy需提前进行安装,安装完成python后,打开cmd,输入
pip install numpy
python 参考配置:
4、Qt 编译选项
参考:基于QT5.14.2的OPENCV4.8.0的cuda11.4开发环境搭建和源码编译_opencv4.8编译-CSDN博客
1、搜索Qt,设置Qt6_DIR目录为:Qt安装路径/6.7.2/msvc2019_64/lib/cmake/Qt6,如图所示。点击“configure”。
2、查找Qt安装路径\6.7.2\msvc2019_64\lib\cmake\Qtest文件夹,将其压缩备份后删除。
参考:https://github.com/opencv/opencv/issues/23826#issuecomment-2227167370
否则会报错。点击“configure”。 此时搜索 Qt6Test_DIR 为 Qt6Test_DIR-NOTFOUND。
报错信息:Qtest
CMake Error at C:/Program Files/CMake/share/cmake-3.30/Modules/FindCUDA.cmake:1793 (file):
Error evaluating generator expression:
$<TARGET_PROPERTY:QT_TESTCASE_BUILDDIR>
$<TARGET_PROPERTY:prop> may only be used with binary targets. It may not
be used with add_custom_command or add_custom_target. Specify the target
to read a property from using the $<TARGET_PROPERTY:tgt,prop> signature
instead.
Call Stack (most recent call first):
C:/Program Files/CMake/share/cmake-3.30/Modules/FindCUDA.cmake:2111 (CUDA_WRAP_SRCS)
C:/Program Files/CMake/share/cmake-3.30/Modules/FindCUDA.cmake:2124 (cuda_compile_base)
cmake/OpenCVDetectCUDA.cmake:164 (CUDA_COMPILE)
cmake/OpenCVUtils.cmake:1578 (ocv_cuda_compile)
cmake/OpenCVModule.cmake:989 (ocv_add_library)
cmake/OpenCVModule.cmake:905 (_ocv_create_module)
modules/world/CMakeLists.txt:78 (ocv_create_module)
解决方案:
查找Qt安装路径\6.7.2\msvc2019_64\lib\cmake\Qtest文件夹,将其压缩备份后删除。
参考:https://github.com/opencv/opencv/issues/23826#issuecomment-2227167370
Qt 参考配置:
注意:每次点击"configure"都要注意报错信息,除了一开始的两个警告外,其他警告一定要及时解决,否则编译会失败
三、检查配置报告
!!!检查具有背景色的行,看是否与自己预期一致。
General configuration for OpenCV 4.10.0 =====================================
Version control: unknown
Extra modules:
Location (extra): D:/Program Files/opencv/opencv_cp312_410/opencv_contrib_4_10/modules
Version control (extra): unknown
Platform:
Timestamp: 2024-07-19T05:23:16Z
Host: Windows 10.0.22631 AMD64
CMake: 3.30.0
CMake generator: Visual Studio 17 2022
CMake build tool: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/MSBuild/Current/Bin/amd64/MSBuild.exe
MSVC: 1940
Configuration: Debug Release
CPU/HW features:
Baseline: SSE SSE2 SSE3
requested: SSE3
Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
SSE4_1 (16 files): + SSSE3 SSE4_1
SSE4_2 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2
FP16 (0 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
AVX (8 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
AVX2 (36 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
AVX512_SKX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
C/C++:
Built as dynamic libs?: YES
C++ standard: 11
C++ Compiler: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe (ver 19.40.33812.0)
C++ flags (Release): /DWIN32 /D_WINDOWS /W4 /GR /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:precise /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /MP /O2 /Ob2 /DNDEBUG
C++ flags (Debug): /DWIN32 /D_WINDOWS /W4 /GR /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:precise /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /MP /Zi /Ob0 /Od /RTC1
C Compiler: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe
C flags (Release): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:precise /MP /O2 /Ob2 /DNDEBUG
C flags (Debug): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:precise /MP /Zi /Ob0 /Od /RTC1
Linker flags (Release): /machine:x64 /INCREMENTAL:NO
Linker flags (Debug): /machine:x64 /debug /INCREMENTAL
ccache: NO
Precompiled headers: NO
Extra dependencies: cudart_static.lib nppc.lib nppial.lib nppicc.lib nppidei.lib nppif.lib nppig.lib nppim.lib nppist.lib nppisu.lib nppitc.lib npps.lib cublas.lib cudnn.lib cufft.lib -LIBPATH:C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.5/lib/x64 -LIBPATH:C:/Program Files/NVIDIA GPU Computing Toolkit/CUDNN_9.2/lib/x64
3rdparty dependencies:
OpenCV modules:
To be built: aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape signal stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode world xfeatures2d ximgproc xobjdetect xphoto
Disabled: -
Disabled by dependency: -
Unavailable: alphamat cannops cvv freetype hdf java julia matlab ovis python2 python2 sfm viz
Applications: perf_tests apps
Documentation: NO
Non-free algorithms: YES
Windows RT support: NO
GUI:
QT: NO
Win32 UI: YES
OpenGL support: YES (opengl32 glu32)
VTK support: NO
Media I/O:
ZLib: build (ver 1.3.1)
JPEG: build-libjpeg-turbo (ver 3.0.3-70)
SIMD Support Request: YES
SIMD Support: NO
WEBP: build (ver encoder: 0x020f)
PNG: build (ver 1.6.43)
SIMD Support Request: YES
SIMD Support: YES (Intel SSE)
TIFF: build (ver 42 - 4.6.0)
JPEG 2000: build (ver 2.5.0)
OpenEXR: build (ver 2.3.0)
HDR: YES
SUNRASTER: YES
PXM: YES
PFM: YES
Video I/O:
DC1394: NO
FFMPEG: YES (prebuilt binaries)
avcodec: YES (58.134.100)
avformat: YES (58.76.100)
avutil: YES (56.70.100)
swscale: YES (5.9.100)
avresample: YES (4.0.0)
GStreamer: NO
DirectShow: YES
Media Foundation: YES
DXVA: YES
Parallel framework: Concurrency
Trace: YES (with Intel ITT)
Other third-party libraries:
Intel IPP: 2021.11.0 [2021.11.0]
at: D:/Program Files/opencv/opencv_cp312_410/opencv_410_cp312_amd64_bulid/3rdparty/ippicv/ippicv_win/icv
Intel IPP IW: sources (2021.11.0)
at: D:/Program Files/opencv/opencv_cp312_410/opencv_410_cp312_amd64_bulid/3rdparty/ippicv/ippicv_win/iw
Lapack: NO
Eigen: NO
Custom HAL: NO
Flatbuffers: builtin/3rdparty (23.5.9)
NVIDIA CUDA: YES (ver 12.5, CUFFT CUBLAS)
NVIDIA GPU arch: 89 90
NVIDIA PTX archs: 90
cuDNN: YES (ver 9.2.1)
OpenCL: YES (NVD3D11)
Include path: D:/Program Files/opencv/opencv_cp312_410/3rdparty/include/opencl/1.2
Link libraries: Dynamic load
Python 3:
Interpreter: C:/Program Files/Python312/python.exe (ver 3.12.4)
Libraries: optimized C:/Program Files/Python312/libs/python312.lib debug C:/Program Files/Python312/libs/python312_d.lib (ver 3.12.4)
Limited API: NO
numpy: C:/Program Files/Python312/Lib/site-packages/numpy/_core/include (ver 2.0.0)
install path: C:/Program Files/Python312/Lib/site-packages/cv2/python-3.12
Python (for build): C:/Program Files/Python312/python.exe
Java:
ant: NO
Java: NO
JNI: NO
Java wrappers: NO
Java tests: NO
Install to: D:/Program Files/opencv/opencv_cp312_410/opencv_410_cp312_amd64_bulid/install
-----------------------------------------------------------------
Configuring done (10.3s)
Generating done (13.0s)
四、使用VS Studio 进行编译
1、点击open project 自动打开vs studio
注意:该opencv_python3 是不可或缺的。没有的话检查是否勾选BUILD_opencv_python3
2、点击选择Release。
3、右键Install,点击生成
时间大概1个小时-2个小时左右,按cpu性能而定。若没有失败,则编译成功。
至此编译完成!!!
五、添加环境变量,修改config.py文件
1、修改config.py文件
编译完成后只有opencv/build/Install 文件夹(本文命名为opencv_cp312_410\opencv_410_cp312_amd64_bulid\opencv_410_cp312_amd64 文件夹)是我们所需要的,将Install 文件夹移动到你所需的位置后,需要进行如下更改:
1、打开python 安装路径,或者在 cmake配置信息中取得,打开安装路径中的cv2文件夹(本文 install path 为C:/Program Files/Python312/Lib/site-packages/cv2/python-3.12,则打开C:/Program Files/Python312/Lib/site-packages/cv2 )
2、打开cv2文件夹下的config.py下的
3、编辑该路径为install的文件路径
2、修改环境变量
1、打开系统变量中的Path
2、将*(install的文件路径)*/install/x64/vc17/bin 添加到系统变量中
3、修改环境变量后重启计算机!!!!
编译完成文件下载链接:(仅参考)
百度云链接opencv_cp312_410_win64 提取码:toi4
下一篇:测试篇
windows:Opencv使用Cmake & VS 编译, C++、python、Cuda、Qt 环境详细教学及踩坑 二 :测试篇-CSDN博客
参考链接:
AMD Customer Community
最新 | OpenCV4.8 + CUDA + 扩展模块支持编译指南-腾讯云开发者社区-腾讯云
https://www.cnblogs.com/guojin-blogs/p/17939955#4-%E9%A1%B9%E7%9B%AE%E6%B5%8B%E8%AF%95
基于QT5.14.2的OPENCV4.8.0的cuda11.4开发环境搭建和源码编译_opencv4.8编译-CSDN博客
Windows下配置CMake(入门级教程,适合新人收藏学习)-阿里云开发者社区
windows下cmake的安装与环境变量配置教程_win10配置 cmake环境变量-CSDN博客
windows下 C++ openCV配置及x86编译(傻瓜式教程)_opencv x86-CSDN博客
opencv源码编译及配置完整版教程(win10+vs2019+opencv-4.4.0+opencv_contrib-4.4.0)_、 .4 ,vcvedv-CSDN博客
WIN10下使用MinGW编译安装运行OpenCV_mingw编译opencv-CSDN博客
https://www.cnblogs.com/liushuiruobing/p/17695059.html
【opencv】【GPU】windows10下opencv4.8.0-cuda Python版本源码编译教程_cv2的版本4.8.0-CSDN博客
OpenCV4.8 GPU版本CMake编译详细步骤 与CUDA代码演示-CSDN博客
【opencv】【GPU】windows10下opencv4.8.0-cuda Python版本源码编译教程_opencv_牙牙要健康-开放原子开发者工作坊
在windows上编译并使用opencv的正确姿势 | Aynakeya's Blog
Windows下Visual Studio 编译OpenCV详细教程(win10+vs2017+opencv4.5.2)(OpenCV最新版)(特别适合无法科学上网的童鞋们)_windows环境下vsstudio编译opencv-CSDN博客
Windows编译OpenCV及扩展模块-CSDN博客
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。