找回密码
 会员注册
查看: 27|回复: 0

windows:Opencv使用Cmake&VS编译,C++、python、Cuda、Qt环境详细教学及踩坑一:编译篇

[复制链接]

2万

主题

0

回帖

6万

积分

超级版主

积分
67462
发表于 2024-9-10 13:58:45 | 显示全部楼层 |阅读模式
求赞求赞求赞,若有帮助,鼓励一下测试篇:windows:Opencv使用Cmake&VS编译,C++、python、Cuda、Qt环境详细教学及踩坑二:测试篇-CSDN博客一、准备:本文使用的软件版本均是撰写时的最新或次新版,对过旧版本不一定通用0、环境配置:WindowsWindows11专业版 23H2opencv4.10Qt  6.7.2Cmake3.3.0python3.12.4cuda12.5cudnn9.2.1vsstudio20221、Opencv,Opencv_contrib 源码下载   本文版本:4.10GitHub-opencv/opencv:OpenSourceComputerVisionLibraryGitHub-opencv/opencv_contrib:RepositoryforOpenCV'sextramodules下载完成后解压opencv和opencv_contrib,将opencv_contrib文件夹移入opencv文件夹下,并在opencv文件夹下新建build文件夹。 软件安装教程引用其他大神的教程,很详细2、Qt安装          本文版本:Qt6.7.2Qt下载安装及配置教程(非常详细),从零基础入门到精通,看完这一篇就够了-CSDN博客3、Cmake安装:    本文版本:Cmake3.3.0Windows下的CMake详细安装教程(2024/1/24)_windows安装cmake-CSDN博客4、python安装:        本文版本:python3.12.4 DownloadPython|Python.org1、选择自定义安装2、选择添加python路径到环境变量3、选择为所有用户安装其他选项如图所示,如果cmake需要debug版本的一定要勾选debug有关的选项,否则生成过程中会缺少python3xx_d.lib 文件。   5、Cuda、cudNN安装:本文版本:Cuda 12.5   cudNN9.2 先按该教程安装cuda、cudnn:Cuda和cuDNN安装教程(超级详细)-CSDN博客 cudnn再按官方文档安装一遍:英伟达官方cudnn安装教程 注意:cudnn一定要按教程,将cudnn文件复制到cuda文件夹下。否则:如果只是按照官方文档中的教程,编译成功后,pythonimportcv2时无法调用cudnn中的dll文件。报错:ImportErrorLLloadfailed:Thespecifiedmodulecouldnotbefound.!!注意:Windows下使用cmake编译cuda只能使用VS,否则会无法找到cuda位置6、VSstudio安装:    ThankYouforDownloadingVisualStudioCommunityEdition7、上述软件安装完成后重启计算机!!!二、设置Cmake选项    参考:AMDCustomerCommunity从“开始”菜单打开CMake(cmake-gui)将“Whereisthesourcecode”设置为提取的opencv文件夹。将“Wheretobuildthebinaries”设置为提取的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、通用编译选项:参考链接:AMDCustomerCommunity        使用“搜索”框更改以下属性: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:CMakeWarning(dev)atCMakeLists.txt:127(enable_language): project()shouldbecalledpriortothisenable_language()call.Thiswarningisforprojectdevelopers. Use-Wno-devtosuppressit.SelectingWindowsSDKversion10.0.22621.0totargetWindows10.0.22631.CMakeWarning(dev)atC:/ProgramFiles/CMake/share/cmake-3.30/Modules/Platform/Windows-MSVC.cmake:539(enable_language): project()shouldbecalledpriortothisenable_language()call.CallStack(mostrecentcallfirst): C:/ProgramFiles/CMake/share/cmake-3.30/Modules/Platform/Windows-MSVC.cmake:509(__windows_compiler_msvc_enable_rc) C:/ProgramFiles/CMake/share/cmake-3.30/Modules/Platform/Windows-MSVC-CXX.cmake:6(__windows_compiler_msvc) C:/ProgramFiles/CMake/share/cmake-3.30/Modules/CMakeCXXInformation.cmake:48(include) CMakeLists.txt:127(enable_language)Thiswarningisforprojectdevelopers. Use-Wno-devtosuppressit.报错信息2: CMakeWarningatcmake/OpenCVGenSetupVars.cmake:54(message): CONFIGURATIONISNOTSUPPORTED:validatesetupvarsscriptininstall directoryCallStack(mostrecentcallfirst): CMakeLists.txt:1180(include)​解决方案: 这两类警告,不影响正常编译过程。对此未作处理。2、cuda编译选项参考链接:windows11编译OpenCV4.5.0withCUDA(附注意事项)-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_NVCUVIDCMakeWarningatopencv_contrib_4_10/modules/cudacodec/CMakeLists.txt:26(message): cudacodec::VideoReaderrequiresNvidiaVideoCodecSDK. leaseresolve dependencyordisableWITH_NVCUVID=OFFCallStack(mostrecentcallfirst): modules/world/CMakeLists.txt:13(include) modules/world/CMakeLists.txt:50(include_one_module)CMakeWarningatopencv_contrib_4_10/modules/cudacodec/CMakeLists.txt:30(message): cudacodec::VideoWriterrequiresNvidiaVideoCodecSDK. leaseresolve dependencyordisableWITH_NVCUVENC=OFFCallStack(mostrecentcallfirst): modules/world/CMakeLists.txt:13(include) modules/world/CMakeLists.txt:50(include_one_module)解决方法:               搜索:WITH_NVCUVENC——取消选中          WITH_NVCUVID——取消选中报错信息2:OPENCV_DNN_CUDACMakeErroratmodules/dnn/CMakeLists.txt:53(message): DNN:CUDAbackendrequirescuDNN. leaseresolvedependencyordisable OPENCV_DNN_CUDA=OFFCallStack(mostrecentcallfirst): 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-cudaPython版本源码编译教程_cv2的版本4.8.0-CSDN博客注意:有的同学即使按图勾选后依然无法出现某些选项。例如:    若无法出现BUILD_opencv_python3,(此选项为python编译的必需选项)此时可以进行手动添加。点击AddEntry ,在Name中输入BUILD_opencv_python3,点击OK,即可出现该选项。python的选项设置如图所示,其中numpy需提前进行安装,安装完成python后,打开cmd,输入pipinstallnumpy​​​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。 报错信息:QtestCMakeErroratC:/ProgramFiles/CMake/share/cmake-3.30/Modules/FindCUDA.cmake:1793(file):Errorevaluatinggeneratorexpression$mayonlybeusedwithbinarytargets.Itmaynotbeusedwithadd_custom_commandoradd_custom_target.Specifythetargettoreadapropertyfromusingthe$signatureinstead.CallStack(mostrecentcallfirst):C:/ProgramFiles/CMake/share/cmake-3.30/Modules/FindCUDA.cmake:2111(CUDA_WRAP_SRCS)C:/ProgramFiles/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-2227167370Qt参考配置:注意:每次点击"configure"都要注意报错信息,除了一开始的两个警告外,其他警告一定要及时解决,否则编译会失败三、检查配置报告 !!!检查具有背景色的行,看是否与自己预期一致。GeneralconfigurationforOpenCV4.10.0=====================================Versioncontrol:unknownExtramodulesocation(extra):/ProgramFiles/opencv/opencv_cp312_410/opencv_contrib_4_10/modulesVersioncontrol(extra):unknownPlatform:Timestamp:2024-07-19T05:23:16ZHost:Windows10.0.22631AMD64CMake:3.30.0CMakegenerator:VisualStudio172022CMakebuildtool:C:/ProgramFiles(x86)/MicrosoftVisualStudio/2022/BuildTools/MSBuild/Current/Bin/amd64/MSBuild.exeMSVC:1940ConfigurationebugReleaseCPU/HWfeatures:Baseline:SSESSE2SSE3requested:SSE3Dispatchedcodegeneration:SSE4_1SSE4_2FP16AVXAVX2AVX512_SKXrequested:SSE4_1SSE4_2AVXFP16AVX2AVX512_SKXSSE4_1(16files):+SSSE3SSE4_1SSE4_2(1files):+SSSE3SSE4_1POPCNTSSE4_2FP16(0files):+SSSE3SSE4_1POPCNTSSE4_2FP16AVXAVX(8files):+SSSE3SSE4_1POPCNTSSE4_2AVXAVX2(36files):+SSSE3SSE4_1POPCNTSSE4_2FP16FMA3AVXAVX2AVX512_SKX(5files):+SSSE3SSE4_1POPCNTSSE4_2FP16FMA3AVXAVX2AVX_512FAVX512_COMMONAVX512_SKXC/C++:Builtasdynamiclibs?:YESC++standard:11C++Compiler:C:/ProgramFiles(x86)/MicrosoftVisualStudio/2022/BuildTools/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe(ver19.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/DNDEBUGC++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/RTC1CCompiler:C:/ProgramFiles(x86)/MicrosoftVisualStudio/2022/BuildTools/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exeCflags(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/DNDEBUGCflags(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/RTC1Linkerflags(Release):/machine:x64/INCREMENTAL:NOLinkerflags(Debug):/machine:x64/debug/INCREMENTALccache:NOPrecompiledheaders:NOExtradependencies:cudart_static.libnppc.libnppial.libnppicc.libnppidei.libnppif.libnppig.libnppim.libnppist.libnppisu.libnppitc.libnpps.libcublas.libcudnn.libcufft.lib-LIBPATH:C:/ProgramFiles/NVIDIAGPUComputingToolkit/CUDA/v12.5/lib/x64-LIBPATH:C:/ProgramFiles/NVIDIAGPUComputingToolkit/CUDNN_9.2/lib/x643rdpartydependencies:OpenCVmodules:Tobebuilt:arucobgsegmbioinspiredcalib3dccalibcorecudaarithmcudabgsegmcudacodeccudafeatures2dcudafilterscudaimgproccudalegacycudaobjdetectcudaoptflowcudastereocudawarpingcudevdatasetsdnndnn_objdetectdnn_superresdpmfacefeatures2dflannfuzzygapihfshighguiimg_hashimgcodecsimgprocintensity_transformline_descriptormccmlobjdetectoptflowphase_unwrappingphotoplotpython3qualityrapidregrgbdsaliencyshapesignalstereostitchingstructured_lightsuperressurface_matchingtexttrackingtsvideovideoiovideostabwechat_qrcodeworldxfeatures2dximgprocxobjdetectxphotoDisabled:-Disabledbydependency:-Unavailable:alphamatcannopscvvfreetypehdfjavajuliamatlabovispython2python2sfmvizApplications:perf_testsappsDocumentation:NONon-freealgorithms:YESWindowsRTsupport:NOGUIT:NOWin32UI:YESOpenGLsupport:YES(opengl32glu32)VTKsupport:NOMediaI/O:ZLib:build(ver1.3.1)JPEG:build-libjpeg-turbo(ver3.0.3-70)SIMDSupportRequest:YESSIMDSupport:NOWEBP:build(verencoder:0x020f)PNG:build(ver1.6.43)SIMDSupportRequest:YESSIMDSupport:YES(IntelSSE)TIFF:build(ver42-4.6.0)JPEG2000:build(ver2.5.0)OpenEXR:build(ver2.3.0)HDR:YESSUNRASTER:YESPXM:YESPFM:YESVideoI/OC1394:NOFFMPEG:YES(prebuiltbinaries)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:NODirectShow:YESMediaFoundation:YESDXVA:YESParallelframework:ConcurrencyTrace:YES(withIntelITT)Otherthird-partylibraries:IntelIPP:2021.11.0[2021.11.0]at:/ProgramFiles/opencv/opencv_cp312_410/opencv_410_cp312_amd64_bulid/3rdparty/ippicv/ippicv_win/icvIntelIPPIW:sources(2021.11.0)at:/ProgramFiles/opencv/opencv_cp312_410/opencv_410_cp312_amd64_bulid/3rdparty/ippicv/ippicv_win/iwLapack:NOEigen:NOCustomHAL:NOFlatbuffers:builtin/3rdparty(23.5.9)NVIDIACUDA:YES(ver12.5,CUFFTCUBLAS)NVIDIAGPUarch:8990NVIDIAPTXarchs:90cuDNN:YES(ver9.2.1)OpenCL:YES(NVD3D11)Includepath:/ProgramFiles/opencv/opencv_cp312_410/3rdparty/include/opencl/1.2LinklibrariesynamicloadPython3:Interpreter:C:/ProgramFiles/Python312/python.exe(ver3.12.4)LibrariesptimizedC:/ProgramFiles/Python312/libs/python312.libdebugC:/ProgramFiles/Python312/libs/python312_d.lib(ver3.12.4)LimitedAPI:NOnumpy:C:/ProgramFiles/Python312/Lib/site-packages/numpy/_core/include(ver2.0.0)installpath:C:/ProgramFiles/Python312/Lib/site-packages/cv2/python-3.12Python(forbuild):C:/ProgramFiles/Python312/python.exeJava:ant:NOJava:NOJNI:NOJavawrappers:NOJavatests:NOInstallto:/ProgramFiles/opencv/opencv_cp312_410/opencv_410_cp312_amd64_bulid/install-----------------------------------------------------------------Configuringdone(10.3s)Generatingdone(13.0s) 四、使用VS Studio进行编译1、点击openproject自动打开vsstudio​注意:该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文件夹(本文installpath为C:/ProgramFiles/Python312/Lib/site-packages/cv2/python-3.12,则打开C:/ProgramFiles/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博客参考链接:AMDCustomerCommunity最新|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编译(傻瓜式教程)_opencvx86-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-cudaPython版本源码编译教程_cv2的版本4.8.0-CSDN博客OpenCV4.8GPU版本CMake编译详细步骤与CUDA代码演示-CSDN博客【opencv】【GPU】windows10下opencv4.8.0-cudaPython版本源码编译教程_opencv_牙牙要健康-开放原子开发者工作坊在windows上编译并使用opencv的正确姿势|Aynakeya'sBlogWindows下VisualStudio编译OpenCV详细教程(win10+vs2017+opencv4.5.2)(OpenCV最新版)(特别适合无法科学上网的童鞋们)_windows环境下vsstudio编译opencv-CSDN博客Windows编译OpenCV及扩展模块-CSDN博客
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-3 02:25 , Processed in 0.488178 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表