windows 源码安装 opencv + opencv_contrib

文章目录

  • 准备
  • CMake
  • Visual Studio 2017
  • 检查
  • 环境变量
  • 整理文件
  • 现成下载

自从这次安装OpenCV后,我明白了一个道理:教程得看官方的。毕竟网上满天飞的教程背后的作者水平未知,不知道每一步为什么要这么干,也就不可能知道需要注意什么,也就导致了教程里说的头头是道,看教程的人死活安装不上。

强烈建议有能力的话自己看OpenCV官方教程Installation in Windows中的Installation by Making Your Own Libraries from the Source Files那部分

我在OpenCV官方教程的基础上,根据自己的操作和需求,记录了这次安装过程。

准备

  1. 软件:CMake,Visual Studio 2017(其他版本的都可以)
  2. 按自己的需求创建文件夹OpenCV。我创建在D:\Library下面。
  3. OpenCV下面创建tempbuild文件夹。temp用来存放编译时的临时文件,后面会删掉;build用来存放最终安装好的文件(就跟opencv官网上已经打包好的window版本的opencv一样)
  4. 到Github上下载opencv和opencv_contrib源码,分别放在OpenCV文件夹下的sourcesopencv_contrib-3.4两个文件夹中
    注意:默认的branch是master,表示最新的版本(4.x)。如果要选择3.x版本的话,修改branch就好了,而且opencv和opencv_contrib的版本要对应
    windows 源码安装 opencv + opencv_contrib_第1张图片
  5. (可选)在后续CMake编译的时候会下载一些文件,但是网络问题可能会导致下载不下来(如下图),所以如果你需要那些文件对应的功能的话,就需要自己手动下载那些文件,然后修改代码中的路径。
    windows 源码安装 opencv + opencv_contrib_第2张图片
    "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"
    windows 源码安装 opencv + opencv_contrib_第3张图片
    将所有下载好的文件统一放在一个文件夹,比如我放在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

  1. 打开CMake-GUIWhere is the source code选择opencv的源文件,比如D:/Library/OpenCV/sourcesWhere to build the binaries选择保存路径,比如D:/Library/OpenCV/temp。然后点击左下角的Configure(这是第一次),弹出窗口让你选择编译器,我选择的时是vs2017x64。其他默认。
    windows 源码安装 opencv + opencv_contrib_第4张图片
    完成后显示如下:一片红色(参数) + Configuring done。
    windows 源码安装 opencv + opencv_contrib_第5张图片
  2. 红色的一片表示要编译的参数,根据自己的心情和需求选择。
  • 找到BUILD_opencv_world,打勾。这个参数可以只生成一个包含了所有的.lib文件,方便后面配置的时候添加。
  • 找到OPENCV_EXTRA_MODULES_PATH,填入opencv_contrib文件夹的路径,比如D:/Library/OpenCV/opencv_contrib-3.4/modules注意:是/,不是\)
  • 由于我用不到python的,所以我把所有python的参数都清空了。
    windows 源码安装 opencv + opencv_contrib_第6张图片
  • 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.
  1. 再次点击Configure(这是第二次),如果参数都填正确的话,所有的红色都会变成白色(否则表示填错了)。
    windows 源码安装 opencv + opencv_contrib_第7张图片

  2. 没有报错(红色)的情况下,点击Generate,完成后如下图。windows 源码安装 opencv + opencv_contrib_第8张图片

Visual Studio 2017

  1. temp文件夹下会有一个工程文件,打开。如果用的是Visual Studio,文件名为OpenCV.sln。等待IDE读取完所有文件,然后左下角会显示“就绪”
    在这里插入图片描述
  2. 选择配置为Debug,平台为x64。平台必须跟CMake中选的一致。
    windows 源码安装 opencv + opencv_contrib_第9张图片
  3. 解决方案资源管理器中,找到ALL_BUILD,右键,点击生成
    windows 源码安装 opencv + opencv_contrib_第10张图片
  4. 等待完成。如果没有报错,则进行下一步。报错了的话,可以查看错误列表
    windows 源码安装 opencv + opencv_contrib_第11张图片
  5. 将第二步的Debug改成Release,然后重复第二步~第四步。
  6. 在上面都没有出错的情况下,在解决方案资源管理器中,找到INSTALL,右键,点击生成
    windows 源码安装 opencv + opencv_contrib_第12张图片

检查

进入D:\Library\OpenCV\temp\bin\Debug(Debug可以换为Release),可以看到.exe、dll等一些文件。

  1. 在当前目录下打开cmd或者Powershell
  2. 运行.\example_cpp_opencv_version.exe,效果如下
    在这里插入图片描述
  3. D:\Library\OpenCV\sources\samples\data中的left01.jpg-left14.jpgright01.jpg-right14.jpgstereo_calib.xml拷贝到当前目录,然后运行example_cpp_stereo_calib.exe,效果如下
  4. 其他的.exe可以自己试试,如果不知道需要什么文件,直接运行.exe一般都会有提示信息的。例如
    windows 源码安装 opencv + opencv_contrib_第13张图片

环境变量

系统变量或者用户变量

  1. 新建一个变量,变量名OPENCV_DIR变量值D:\Library\OpenCV\build\x64\vc15
  2. 在Path中添加一个值,为D:\Library\OpenCV\build\x64\vc15\bin
  • 方法一:打开cmd或powershell,输入

整理文件

  1. .\temp\install文件夹里的所有内容剪贴到build文件夹中
  2. temp文件夹下的bin文件夹,剪贴到build文件夹中(最后我嫌弃bin\Debug占用空间太大了,把除.dll文件以外的所有文件都删了)
  3. 删除temp文件夹

现成下载

如果你的需求是opencv 3.4.9 + opencv_contrib 3.4 + vs2017 + x64,我把最终的OpenCV打包了,需要的自取:
链接:https://pan.baidu.com/s/1GS_8-wkBH37GjCtFQtV1-A 提取码:yemg

如果你想支持一下我,我把build打包上传到csdn了(因为有大小限制,所以删除了bin\Debug里面除了.dll以外所有的文件),一个积分哦:https://download.csdn.net/download/OTZ_2333/12265696
使用方法:解压到任何你想存放的地方。

你可能感兴趣的:(windows 源码安装 opencv + opencv_contrib)