1.编译环境
1)系统环境:Win10( i7-8700 CPU, 16GB RAM)+RTX 2070+VS2017 Enterprise+CUDA 11.6+cuDNN v8.5.0
2)CMake 3.24.1:https://github.com/Kitware/CMake/releases/download/v3.24.1/cmake-3.24.1-windows-x86_64.zip
3)OpenCV 4.6.0:https://github.com/opencv/opencv/archive/refs/tags/4.6.0.tar.gz
4)opencv_contrib 4.6.0:https://github.com/opencv/opencv_contrib/archive/refs/tags/4.6.0.tar.gz
5)oneTBB 2021.5.0:https://github.com/oneapi-src/oneTBB/releases/download/v2021.5.0/oneapi-tbb-2021.5.0-win.zip
2.编译过程
编译过程参考知乎文章。同样遇到了“Couldn’t resolve host name”的错误,也就是在下载部分第三方库或者文件时,由于是国外的服务器,出现下载失败的情况。作者使用了文章中分享的.cache文件包进行替换,成功完成编译(过程中仍然出现的问题见第3节的问题记录)。为了编译contrib模块和CUDA模块,需要进行如下设置,这里再次记录一下:
1)OPENCV_EXTRA_MODULES_PATH:这里填写opencv_contrib-4.6.0目录下的modules文件夹的路径,例如:D:/Software/opencv/opencv_contrib-4.6.0/modules;
2)WITH_CUDA、BUILD_CUDA_STUBS、OPENCV_DNN_CUDA:全部勾选;
3)OPENCV_ENABLE_NONFREE:勾选;
4)BUILD_opencv_world:勾选;
5)CUDA_ARCH_BIN:修改为GPU支持的算力版本,例如:7.5。注意:必须将这里修改为所使用的GPU支持的算力版本,否则会编译成支持很多种算力的库,编译过程会浪费非常多的时间;
6)CUDA_FAST_MATH、ENABLE_FAST_MATH:勾选;
7)WITH_TBB:勾选;
8)TBB_ENV_INCLUDE:修改为TBB库的include文件夹路径,例如:D:/Software/opencv/oneapi-tbb-2021.5.0/include;
9)TBB_ENV_LIB:修改为TBB库的lib文件路径,例如:D:/Software/opencv/oneapi-tbb-2021.5.0/lib/intel64/vc14/tbb.lib;
9)TBB_ENV_LIB_DEBUG:修改为TBB库的lib文件路径(Debug版),例如:D:/Software/opencv/oneapi-tbb-2021.5.0/lib/intel64/vc14/tbb_debug.lib
备注:已经勾选了WITH_CUDA后再点击Configure才会出现CUDA_ARCH_BIN的设置,作者根据RTX2070的算力,将其修改为7.5。
配置完成后,点击Generate就可以生成VS2017的工程;点击Open Project就可以通过VS2017打开工程;在解决方案资源管理器中,首先编译“ALL_BUILD”,再编译“INSTALL”就可以生成自己编译的库。
3.问题记录
1)作者使用的是OpenCV4.6.0版本进行编译,也就是作者编写本博客时OpenCV的最新版本。使用了上面提到的知乎文章分享的cache文件包解决CMake Configure过程中文件无法下载的问题,但是Configure完成后,ffmpeg库始终无法找到。尝试了多次之后,使用知乎文章中介绍的方法处理后ffmpeg成功下载;在ffmpeg成功下载之后会自动生成3rdparty/ffmpeg文件夹,其中内容如下:
需要提醒的是,对于OpenCV4.6.0,直接使用知乎文章分享的cache包的话,Configure过程中会始终找不到ffmpeg!作者对比文件发现,上面成功下载的ffmpeg库为较新的版本(新版本为2022.5.0,cache包中的版本为2020.9.0);
2)最终的Configure结果如下所示:
General configuration for OpenCV 4.6.0 ===================================== Version control: unknown Extra modules: Location (extra): D:/Software/opencv/opencv_contrib-4.6.0/modules Version control (extra): unknown Platform: Timestamp: 2022-09-11T12:38:53Z Host: Windows 10.0.19044 AMD64 CMake: 3.24.1 CMake generator: Visual Studio 15 2017 CMake build tool: C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/MSBuild/15.0/Bin/MSBuild.exe MSVC: 1910 Configuration: Debug Release CPU/HW features: Baseline: SSE SSE2 SSE3 requested: SSE3 Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX SSE4_1 (18 files): + SSSE3 SSE4_1 SSE4_2 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX AVX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX AVX2 (33 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 C/C++: Built as dynamic libs?: YES C++ standard: 11 C++ Compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.10.25017/bin/HostX86/x64/cl.exe (ver 19.10.25017.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:fast /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP /MD /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:fast /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP /MDd /Zi /Ob0 /Od /RTC1 C Compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.10.25017/bin/HostX86/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:fast /MP /MD /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:fast /MP /MDd /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/v11.6/lib/x64 -LIBPATH:C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.6/lib 3rdparty dependencies: OpenCV modules: To be built: aruco barcode 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 quality rapid reg rgbd saliency shape 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 cvv freetype hdf java julia matlab ovis python2 python3 sfm viz Applications: tests perf_tests examples apps Documentation: NO Non-free algorithms: YES Windows RT support: NO GUI: Win32 UI: YES VTK support: NO Media I/O: ZLib: build (ver 1.2.12) JPEG: build-libjpeg-turbo (ver 2.1.2-62) WEBP: build (ver encoder: 0x020f) PNG: build (ver 1.6.37) TIFF: build (ver 42 - 4.2.0) JPEG 2000: build (ver 2.4.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: TBB (ver 2021.5 interface 12050) Trace: YES (with Intel ITT) Other third-party libraries: Lapack: NO Eigen: NO Custom HAL: NO Protobuf: build (3.19.1) NVIDIA CUDA: YES (ver 11.6, CUFFT CUBLAS FAST_MATH) NVIDIA GPU arch: 75 NVIDIA PTX archs: cuDNN: YES (ver 8.5.0) OpenCL: YES (NVD3D11) Include path: D:/Software/opencv/opencv-4.6.0/3rdparty/include/opencl/1.2 Link libraries: Dynamic load Python (for build): NO Java: ant: NO JNI: NO Java wrappers: NO Java tests: NO Install to: D:/Software/opencv/opencv-full-4.6.0/install
需要关注的项目有一下几点:1、FFMPEG:这里应为YES,如果为NO说明ffmpeg接口添加失败,会影响视频处理;2、NVIDIA GPU arch:这里一般只有1个参数,比如作者设置的75(即算力为7.5)。如果出现了多种,意味着需要编译支持多种算力的库,这样的话编译会耗费大量的时间;3、Parallel framework:TBB,表示TBB库会参与编译(主要用于图像处理在多核CPU上的并行加速);
3)关于CMake Configure过程中文件下载失败,作者始终有一点不解:首先,将Configure过程中文件下载的地址拷贝至Chrome浏览器进行下载,均可以下载成功;其次,尝试了修改host文件的域名映射,Configure过程中文件下载失败的代码变了,但是依旧大部分文件下载不成功;此外,还尝试了其他破除限制访问网络的方法,Configure过程文件下载都没有成功。
4.共享资源
最后,分享一下包含install文件夹的整个编译结果文件夹供参考:
链接:https://pan.baidu.com/s/1YnP1oPHW_5TbvdnPA0vBzw?pwd=g1i4
提取码:g1i4