自从这次安装OpenCV后,我明白了一个道理:教程得看官方的。毕竟网上满天飞的教程背后的作者水平未知,不知道每一步为什么要这么干,也就不可能知道需要注意什么,也就导致了教程里说的头头是道,看教程的人死活安装不上。
强烈建议有能力的话自己看OpenCV官方教程Installation in Windows中的Installation by Making Your Own Libraries from the Source Files那部分
我在OpenCV官方教程的基础上,根据自己的操作和需求,记录了这次安装过程。
准备
- 软件:CMake,Visual Studio 2017(其他版本的都可以)
- 按自己的需求创建文件夹
OpenCV
。我创建在D:\Library
下面。 - 在
OpenCV
下面创建temp
、build
文件夹。temp
用来存放编译时的临时文件,后面会删掉;build
用来存放最终安装好的文件(就跟opencv官网上已经打包好的window版本的opencv一样) - 到Github上下载opencv和opencv_contrib源码,分别放在
OpenCV
文件夹下的sources
和opencv_contrib-3.4
两个文件夹中
注意:默认的branch是master,表示最新的版本(4.x)。如果要选择3.x版本的话,修改branch就好了,而且opencv和opencv_contrib的版本要对应
- (可选)在后续CMake编译的时候会下载一些文件,但是网络问题可能会导致下载不下来(如下图),所以如果你需要那些文件对应的功能的话,就需要自己手动下载那些文件,然后修改代码中的路径。
"Download"后面对应的就是文件名,比如bootstdesc_lbgm.i
。那个文件的下载地址所在的代码文件在“Call Stack (most recent call first)”的下一行,比如D:\Library\OpenCV\opencv_contrib-3.4\modules\xfeatures2d\cmake\download_boostdesc.cmake
。打开这个代码文件,找到需要下载文件的URL中带http字样的那行,例"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${OPENCV_3RDPARTY_COMMIT}/"
。其中,$(·)
表示变量,去上文找到它的取值,带入URL中。最后在URL后面接上文件名,得到下载路径,比如"https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/bootstdesc_lbgm.i"
。
将所有下载好的文件统一放在一个文件夹,比如我放在OpenCV
文件夹下的opencv_need
文件夹下面。然后把所有的URL改成"file:///D:/Library/OpenCV/opencv_need/"
我把所有手动下载的文件整理了一个表格
文件名 | .cmake文件位置 | 下载连接 |
---|---|---|
opencv_ffmpeg.dll | D:\Library\OpenCV\sources\3rdparty\ffmpeg\ffmpeg.cmake | https://raw.githubusercontent.com/opencv/opencv_3rdparty/548e3c997a80d65f710b9048f1d33371e3a203ac/ffmpeg/opencv_ffmpeg.dll |
opencv_ffmpeg_64.dll | 同上 | https://raw.githubusercontent.com/opencv/opencv_3rdparty/548e3c997a80d65f710b9048f1d33371e3a203ac/ffmpeg/opencv_ffmpeg_64.dll |
ffmpeg_version.cmake | 同上 | https://raw.githubusercontent.com/opencv/opencv_3rdparty/548e3c997a80d65f710b9048f1d33371e3a203ac/ffmpeg/ffmpeg_version.cmake |
ippicv_2019_win_intel64_20180723_general.zip | D:\Library\OpenCV\sources\3rdparty\ippicv\ippicv.cmake | https://raw.githubusercontent.com/opencv/opencv_3rdparty/32e315a5b106a7b89dbed51c28f8120a48b368b4/ippicv/ippicv_2019_win_intel64_20180723_general.zip |
boostdesc_bgm.i | D:\Library\OpenCV\opencv_contrib-3.4\modules\xfeatures2d\cmake\download_boostdesc.cmake | https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm.i |
boostdesc_bgm_bi.i | 同上 | https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_bi.i |
boostdesc_bgm_hd.i | 同上 | https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_hd.i |
boostdesc_binboost_064.i | 同上 | https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_064.i |
boostdesc_binboost_128.i | 同上 | https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_128.i |
boostdesc_binboost_256.i | 同上 | https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_256.i |
boostdesc_lbgm.i | 同上 | https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_lbgm.i |
vgg_generated_48.i | D:\Library\OpenCV\opencv_contrib-3.4\modules\xfeatures2d\cmake\download_vgg.cmake | https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_48.i |
vgg_generated_64.i | 同上 | https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_64.i |
vgg_generated_80.i | 同上 | https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_80.i |
vgg_generated_120.i | 同上 | https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_120.i |
face_landmark_model.dat | D:\Library\OpenCV\opencv_contrib-3.4\modules\face\CMakeLists.txt | https://raw.githubusercontent.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat |
CMake
- 打开CMake-GUI,Where is the source code选择opencv的源文件,比如
D:/Library/OpenCV/sources
;Where to build the binaries选择保存路径,比如D:/Library/OpenCV/temp
。然后点击左下角的Configure(这是第一次),弹出窗口让你选择编译器,我选择的时是vs2017和x64。其他默认。
完成后显示如下:一片红色(参数) + Configuring done。
- 红色的一片表示要编译的参数,根据自己的心情和需求选择。
- 找到BUILD_opencv_world,打勾。这个参数可以只生成一个包含了所有的.lib文件,方便后面配置的时候添加。
- 找到OPENCV_EXTRA_MODULES_PATH,填入opencv_contrib文件夹的路径,比如
D:/Library/OpenCV/opencv_contrib-3.4/modules
(注意:是/,不是\) - 由于我用不到python的,所以我把所有python的参数都清空了。
- 带WITH的参数表示库(packages ),根据自己需求选择,我没有改动。
从官方教程上搬运了一些参数的意思,参考参考,根据自己的需求选择:
- BUILD_DOCS -> It creates two projects for building the documentation of OpenCV (there will be a separate project for building the HTML and the PDF files). Note that these are not built together with the solution. You need to make an explicit build project command on these to do so.
- BUILD_EXAMPLES -> OpenCV comes with many example applications from which you may learn most of the libraries capabilities. This will also come handy to easily try out if OpenCV is fully functional on your computer.
- BUILD_PACKAGE -> Prior to version 2.3 with this you could build a project that will build an OpenCV installer. With this, you can easily install your OpenCV flavor on other systems. For the latest source files of OpenCV, it generates a new project that simply creates a zip archive with OpenCV sources.
- BUILD_SHARED_LIBS -> With this you can control to build DLL files (when turned on) or static library files (*.lib) otherwise.
- BUILD_TESTS -> Each module of OpenCV has a test project assigned to it. Building these test projects is also a good way to try out, that the modules work just as expected on your system too.
- BUILD_PERF_TESTS -> There are also performance tests for many OpenCV functions. If you are concerned about performance, build them and run.
- BUILD_opencv_python -> Self-explanatory. Create the binaries to use OpenCV from the Python language.
- BUILD_opencv_world -> Generate a single “opencv_world” binary (a shared or static library, depending on BUILD_SHARED_LIBS) including all the modules instead of a collection of separate binaries, one binary per module.
-
再次点击Configure(这是第二次),如果参数都填正确的话,所有的红色都会变成白色(否则表示填错了)。
-
没有报错(红色)的情况下,点击Generate,完成后如下图。
Visual Studio 2017
- 在
temp
文件夹下会有一个工程文件,打开。如果用的是Visual Studio,文件名为OpenCV.sln
。等待IDE读取完所有文件,然后左下角会显示“就绪”
- 选择配置为Debug,平台为x64。平台必须跟CMake中选的一致。
- 在解决方案资源管理器中,找到ALL_BUILD,右键,点击生成
- 等待完成。如果没有报错,则进行下一步。报错了的话,可以查看错误列表。
- 将第二步的Debug改成Release,然后重复第二步~第四步。
- 在上面都没有出错的情况下,在解决方案资源管理器中,找到INSTALL,右键,点击生成
检查
进入D:\Library\OpenCV\temp\bin\Debug
(Debug可以换为Release),可以看到.exe、dll等一些文件。
- 在当前目录下打开cmd或者Powershell
- 运行
.\example_cpp_opencv_version.exe
,效果如下
- 把
D:\Library\OpenCV\sources\samples\data
中的left01.jpg-left14.jpg、right01.jpg-right14.jpg和stereo_calib.xml拷贝到当前目录,然后运行example_cpp_stereo_calib.exe
,效果如下
- 其他的.exe可以自己试试,如果不知道需要什么文件,直接运行.exe一般都会有提示信息的。例如
环境变量
在系统变量或者用户变量中
- 新建一个变量,变量名为
OPENCV_DIR
,变量值为D:\Library\OpenCV\build\x64\vc15
- 在Path中添加一个值,为
D:\Library\OpenCV\build\x64\vc15\bin
- 方法一:打开cmd或powershell,输入
整理文件
- 将
.\temp\install
文件夹里的所有内容剪贴到build
文件夹中 - 将
temp
文件夹下的bin
文件夹,剪贴到build
文件夹中(最后我嫌弃bin\Debug
占用空间太大了,把除.dll文件以外的所有文件都删了) - 删除
temp
文件夹