20201017更新
前置条件
vs2019
也就是所谓的vc16. 要检测是否安装了完整的C++功能和cmake支持,用vs 的installer可以选上。
否则cmake的configure都会报错。
1 下载源码
2 configure
选上
BUILD_opencv_world
NONFREE必须开,否则即使下载的opencv_contrib也不包含sift等等nonfree库了
注意路径中的分割线,必须是linux的
D:/opencv/opencv_contrib-4.5.0/modules
cuda:
选上
WITH_CUDA
WITH_CUDNN
修改 CUDA_ARCH_BIN
这个针对不同计算能力的GPU,编译不同版本,但是这部分特别慢,只能单线程编译。
只编译自己用到的,或者可能部署用到的就可以。
我能用到的显卡基本也就是
所以只保留这2个就可以啦。
其他
基本上多点几次Configure,直到不报错就OK。
如果报warning:
CMake Warning at cmake/OpenCVGenSetupVars.cmake:54 (message):
CONFIGURATION IS NOT SUPPORTED: validate setupvars script in install directory Call Stack (most recent call first)
去掉这个编译选项,应该没啥用
python3路径现在已经能自动识别
python3的路径一个一个补上:注意library 很迷惑,有python3.lib和 .a的。注意区别
其他要注意的也就是这些了,那个OPENCV_PYTHON3_VERSION没啥用,选上好像就编译cv2.pyd不过了。保持空就可以。
1 需要迷信下载的缓存文件1:
打开 D:/opencv/opencv-4.5.0/build/CMakeDownloadLog.txt
如果正常,应该看到:
#use_cache "D:/opencv/opencv-4.5.0/.cache"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_IPPICV_HASH_3rdparty_ippicv_ippicv_2020_win_intel64_20191018_general_zip"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_ADE_HASH_3rdparty_ade_v0_1_1f_zip"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_FFMPEG_HASH_3rdparty_ffmpeg_opencv_videoio_ffmpeg_dll"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_FFMPEG_HASH_3rdparty_ffmpeg_opencv_videoio_ffmpeg_64_dll"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_FFMPEG_HASH_3rdparty_ffmpeg_ffmpeg_version_cmake"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_BOOSTDESC_HASH_downloads_xfeatures2d_boostdesc_bgm_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_BOOSTDESC_HASH_downloads_xfeatures2d_boostdesc_bgm_bi_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_BOOSTDESC_HASH_downloads_xfeatures2d_boostdesc_bgm_hd_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_BOOSTDESC_HASH_downloads_xfeatures2d_boostdesc_binboost_064_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_BOOSTDESC_HASH_downloads_xfeatures2d_boostdesc_binboost_128_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_BOOSTDESC_HASH_downloads_xfeatures2d_boostdesc_binboost_256_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_BOOSTDESC_HASH_downloads_xfeatures2d_boostdesc_lbgm_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_VGG_HASH_downloads_xfeatures2d_vgg_generated_48_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_VGG_HASH_downloads_xfeatures2d_vgg_generated_64_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_VGG_HASH_downloads_xfeatures2d_vgg_generated_80_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_VGG_HASH_downloads_xfeatures2d_vgg_generated_120_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_DATA_HASH_testdata_cv_face_face_landmark_model_dat"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_NVIDIA_OPTICAL_FLOW_HASH_3rdparty_NVIDIAOpticalFlowSDK_1_0_Headers_79c6cee80a2df9a196f20afd6b598a9810964c32_zip"
如果有墙,则看到各种错误和网址,形如:
#do_copy "ffmpeg_version.cmake" "ad57c038ba34b868277ccbe6dd0f9602" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/a66a24e9f410ae05da4baeeb8b451912664ce49c/ffmpeg/ffmpeg_version.cmake" "D:/soft/dev/opencv/opencv-master/build/3rdparty/ffmpeg"
#missing "D:/soft/dev/opencv/opencv-master/build/3rdparty/ffmpeg/ffmpeg_version.cmake"
根据 下载地址依次下载,保存到制定地点
建立D:/opencv/opencv-4.5.0/.cache
1 ippicv
保存到
D:/opencv/opencv-4.5.0/.cache/ippicv/879741a7946b814455eee6c6ffde2984-ippicv_2020_win_intel64_20191018_general.zip
2 ade
https://github.com/opencv/ade/archive/v0.1.1f.zip
保存为
D:/opencv/opencv-4.5.0/.cache/ade/b624b995ec9c439cbc2e9e6ee940d3a2-v0.1.1f.zip
3 ffmpeg
创建 D:/opencv/opencv-4.5.0/build/3rdparty/ffmpeg
下载 3个文件 不需要改md5码
4 xfeatures2d
D:\soft\dev\opencv\4.5\downloads\opencv-4.5.0\build\downloads\xfeatures2d
下载
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_bi.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_hd.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_064.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_128.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_256.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_lbgm.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_48.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_64.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_80.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_120.i
5 testdata
创建
D:/opencv/opencv-4.5.0/build/testdata/cv/face/
下载
https://raw.githubusercontent.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat
6 nvidia_optical_flow
创建
D:/opencv/opencv-4.5.0/.cache/nvidia_optical_flow
下载
https://github.com/NVIDIA/NVIDIAOpticalFlowSDK/archive/79c6cee80a2df9a196f20afd6b598a9810964c32.zip
改名
ca5acedee6cb45d0ec610a6732de5c15-79c6cee80a2df9a196f20afd6b598a9810964c32.zip
#------------------------------------
20200826 更新
opencv 4.4 cuda 10.2
小坑仍然若干(迷信下载,python),但总体平顺多了。漫长的编辑 2小时。
遇到下载不了的,就迷信上网:
ffmpeg ippicv xfeatures2d保存到
/build/downloads 相应文件夹里
face_landmark_model.dat 下载到
/build/testdata\cv\face
具体下载参考下面
20191231更新:
FFMPEG: Download: ffmpeg_version.cmake
Try 1 failed
cd 到
XXX\opencv-master\.cache\ffmpeg
发现
3 Generate 编译安装
然后Generate,然后不要直接在cmake里Open Project,
在开始菜单,在vs2019图标上右键,更多->以管理员身份运行 (否则,如果python安装在C盘,win10权限管理会让python安装失败)
在vs解决方案管理器
解决方案OpenCV->CMakeTargets
"ALL_BUILD" 右键"生成",经过漫长编译,如果正常,应该全都编译成功。之前因为python编译不过,卡了半天。
“INSTALL” “生成”,(debug 也生成一下 难免要调试)
此时产生
opencv-4.5.0\build\install 文件夹
把
D:\opencv\opencv-4.5.0\build\install\x64\vc16\bin
添加到系统环境变量。
保证opencv_world450.dll能被 搜索到(这个直接300M了。。。)。
4 备用 手动安装opencv_python
但C:\Program Files\Python38\Lib\site-packages下仍然没有看见cv2.pyd
想起应该是win10加强了C盘权限的管理,进命令行都得用管理员权限了。
那么:直接用管理员权限启动PowerShell,cd到之前设置的 opencv的build路径(我给设置了D:\soft\dev\opencv\build)
cd D:\soft\dev\opencv\build\python_loader
python setup.py develop
要用develop来安装,不是常用的install否则会报错
ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.
如果C:\Program Files\Python38\Lib\site-packages还是没有cv2.pyd
那就去
D:\opencv\opencv-4.4.0\build\lib\python3\Release
下
把cv2.cp38-win_amd64.pyd 手工改名成 cv2.pyd
然后复制到
C:\Program Files\Python38\Lib\site-packages
5 检查确认安装成功
在python环境里查看信息
>>> import cv2
>>> print(cv2.getBuildInformation())
General configuration for OpenCV 4.5.0 =====================================
Version control: unknown
Extra modules:
Location (extra): D:/opencv/opencv_contrib-4.5.0/modules
Version control (extra): unknown
Platform:
Timestamp: 2020-10-28T07:34:00Z
Host: Windows 10.0.19042 AMD64
CMake: 3.18.4
CMake generator: Visual Studio 16 2019
CMake build tool: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/MSBuild/Current/Bin/MSBuild.exe
MSVC: 1927
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 (17 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 (31 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
AVX512_SKX (7 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/2019/Professional/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe (ver 19.27.29112.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 /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:precise /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP /MDd /Zi /Ob0 /Od /RTC1
C Compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.27.29110/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 /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:precise /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.1/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 stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab world xfeatures2d ximgproc xobjdetect xphoto
Disabled: -
Disabled by dependency: -
Unavailable: alphamat cnn_3dobj cvv freetype hdf java js julia matlab ovis python2 python2 sfm viz
Applications: tests perf_tests 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.11)
JPEG: build-libjpeg-turbo (ver 2.0.5-62)
WEBP: build (ver encoder: 0x020f)
PNG: build (ver 1.6.37)
TIFF: build (ver 42 - 4.0.10)
JPEG 2000: build (ver 2.3.1)
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.91.100)
avformat: YES (58.45.100)
avutil: YES (56.51.100)
swscale: YES (5.7.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: 2020.0.0 Gold [2020.0.0]
at: D:/opencv/opencv-4.5.0/build/3rdparty/ippicv/ippicv_win/icv
Intel IPP IW: sources (2020.0.0)
at: D:/opencv/opencv-4.5.0/build/3rdparty/ippicv/ippicv_win/iw
Lapack: NO
Eigen: NO
Custom HAL: NO
Protobuf: build (3.5.1)
NVIDIA CUDA: YES (ver 11.1, CUFFT CUBLAS)
NVIDIA GPU arch: 61
NVIDIA PTX archs:
cuDNN: YES (ver 8.0.3)
OpenCL: YES (NVD3D11)
Include path: D:/opencv/opencv-4.5.0/3rdparty/include/opencl/1.2
Link libraries: Dynamic load
Python 3:
Interpreter: C:/Program Files/Python39/python.exe (ver 3.9)
Libraries: C:/Program Files/Python39/libs/python39.lib (ver 3.9.0)
numpy: C:/Program Files/Python39/lib/site-packages/numpy/core/include (ver 1.19.2)
install path: C:/Program Files/Python39/Lib/site-packages/cv2/python-3.9
Python (for build): C:/Program Files/Python39/python.exe
Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NO
Install to: D:/opencv/opencv-4.5.0/build/install
-----------------------------------------------------------------
编译完成。
然后就能又在py上使用shift了