Windows环境使用VS2017编译opencv-4.2.0和opencv_contrib-4.2.0源码,支持TBB

一、准备工作

cmake v3.15.7 x64:https://cmake.org/files/v3.15/

opencv-4.2.0: https://github.com/opencv/opencv/releases 

opencv_contrib-4.2.0: https://github.com/opencv/opencv_contrib/releases 

tbb-2019_U9: https://github.com/intel/tbb/releases

opencv分为x86和x64编译,VS2017

Windows环境使用VS2017编译opencv-4.2.0和opencv_contrib-4.2.0源码,支持TBB_第1张图片

Windows环境使用VS2017编译opencv-4.2.0和opencv_contrib-4.2.0源码,支持TBB_第2张图片

Windows环境使用VS2017编译opencv-4.2.0和opencv_contrib-4.2.0源码,支持TBB_第3张图片

 

二、VS2017编译TBB

去官网下载TBB的源码,解压,然后:

使用VS2017打开工程文件tbb-2019_U9\build\vs2013\makefile.sln,直接编译即可,分别编译x86和x64,生成动态库。

 

三、配置CMake的编译选项

去掉勾选 BUILD_DOCS、BUILD_EXAMPLES、BUILD_TESTS,为了节省时间

勾选WITH_TBB, 实现并行处理。不需勾选BUILD_TBB和MKL_WITH_TBB,这两项维持默认值即可。

F:\Machine Vision\OpenCV\source\opencv-4.2.0\modules\core\src\parallel.cpp里面有很多宏定义#if defined HAVE_TBB

如果不使能TBB的话,opencv-4.2.0自带并默认使用了微软的PPL并行库。#define HAVE_CONCURRENCY

编译平台对应关系是Visual Studio 2010(即宏 _MSC_VER = 1600)

#if defined _MSC_VER && _MSC_VER >= 1600
    #define HAVE_CONCURRENCY
#endif

/* IMPORTANT: always use the same order of defines
   - HAVE_TBB         - 3rdparty library, should be explicitly enabled
   - HAVE_HPX         - 3rdparty library, should be explicitly enabled
   - HAVE_OPENMP      - integrated to compiler, should be explicitly enabled
   - HAVE_GCD         - system wide, used automatically        (APPLE only)
   - WINRT            - system wide, used automatically        (Windows RT only)
   - HAVE_CONCURRENCY - part of runtime, used automatically    (Windows only - MSVS 10, MSVS 11)
   - HAVE_PTHREADS_PF - pthreads if available
*/

#if defined HAVE_TBB
    #include "tbb/tbb.h"
    #include "tbb/task.h"
    #include "tbb/tbb_stddef.h"
    #if TBB_INTERFACE_VERSION >= 8000
        #include "tbb/task_arena.h"
    #endif
    #undef min
    #undef max
#elif defined HAVE_HPX
    #include 
    #include 
    //
    #include 
    #include 
    #include 
    #include 
    #include 

#elif defined HAVE_OPENMP
    #include 
#elif defined HAVE_GCD
    #include 
    #include 
#elif defined WINRT && _MSC_VER < 1900
    #include 
#elif defined HAVE_CONCURRENCY
    #include 
#endif

勾选OPENCV_ENABLE_NONFREE,如果没有勾选,那么类似SIFT这种已经被申请专利的方法就无法使用。

搜索OPENCV_EXTRA_MODULES_PATH
然后在value中填入源码包opencv_contrib解压目录中的modules路径(注意:此处路径的反斜杠\要改成正斜杠/,反斜杠\有转义字符的意思)。
错误的 F:\Machine Vision\OpenCV\source\opencv_contrib-4.2.0\modules
正确的 F:/Machine Vision/OpenCV/source/opencv_contrib-4.2.0/modules

可选项,默认已勾选 BUILD_SHARED_LIBS,取决于你想把库编译为动态库还是静态库,我在本篇教程使用默认值:动态库(已勾选)

可选项,默认未勾选 BUILD_opencv_world,取决于你是否想把所有的dll动态库都整合为1个文件,我在本篇教程使用默认值,没勾选

以上配置选项准备完毕后,点击按钮“Configure”。

 

四、下载ipp和ffmpeg文件

点击按钮“Configure”之后,准备过程会下载以下几个文件:

ippicv_2019_win_ia32_20180723_general.zip

opencv_videoio_ffmpeg.dll

opencv_videoio_ffmpeg_64.dll

ffmpeg_version.cmake

这几个文件下载完,会分别存储在路径:

opencv-4.2.0\.cache\ippicv
opencv-4.2.0\.cache\ffmpeg

cmake configure过程的输出日志是:

opencv-4.2.0\build_x86\CMakeDownloadLog.txt

如果上述文件下载失败,CMake会红色字体报错,打开日志CMakeDownloadLog.txt,可以看到:

https://raw.githubusercontent.com/opencv/opencv_3rdparty/32e315a5b106a7b89dbed51c28f8120a48b368b4/ippicv/ippicv_2019_win_ia32_20180723_general.zip
https://raw.githubusercontent.com/opencv/opencv_3rdparty/a66a24e9f410ae05da4baeeb8b451912664ce49c/ffmpeg/opencv_videoio_ffmpeg.dll
https://raw.githubusercontent.com/opencv/opencv_3rdparty/a66a24e9f410ae05da4baeeb8b451912664ce49c/ffmpeg/opencv_videoio_ffmpeg_64.dll
https://raw.githubusercontent.com/opencv/opencv_3rdparty/a66a24e9f410ae05da4baeeb8b451912664ce49c/ffmpeg/ffmpeg_version.cmake

#check_md5 "F:/Machine Vision/OpenCV/source/opencv-4.2.0/.cache/ippicv/0157251a2eb9cd63a3ebc7eed0f3e59e-ippicv_2019_win_ia32_20180723_general.zip"
#check_md5 "F:/Machine Vision/OpenCV/source/opencv-4.2.0/.cache/ffmpeg/5de6044cad9398549e57bc46fc13908d-opencv_videoio_ffmpeg.dll"
#check_md5 "F:/Machine Vision/OpenCV/source/opencv-4.2.0/.cache/ffmpeg/55c0bc8ad27db00116fabf06508de196-opencv_videoio_ffmpeg_64.dll"
#check_md5 "F:/Machine Vision/OpenCV/source/opencv-4.2.0/.cache/ffmpeg/ad57c038ba34b868277ccbe6dd0f9602-ffmpeg_version.cmake"

opencv-4.2.0\build_x86\3rdparty\ffmpeg
opencv-4.2.0\build_x86\3rdparty\ippicv

日志记录了需要下载的url,和它们对应的MD5值。可以自己打开网址下载文件,然后重命名为

0157251a2eb9cd63a3ebc7eed0f3e59e-ippicv_2019_win_ia32_20180723_general.zip

5de6044cad9398549e57bc46fc13908d-opencv_videoio_ffmpeg.dll

55c0bc8ad27db00116fabf06508de196-opencv_videoio_ffmpeg_64.dll

ad57c038ba34b868277ccbe6dd0f9602-ffmpeg_version.cmake

此外,以下文件也是需要下载的,网址在CMakeDownloadLog.txt都有描述。

boostdesc_bgm.i
boostdesc_bgm_bi.i
boostdesc_bgm_hd.i
boostdesc_lbgm.i
boostdesc_binboost_064.i
boostdesc_binboost_128.i
boostdesc_binboost_256.i
vgg_generated_120.i
vgg_generated_64.i
vgg_generated_80.i
vgg_generated_48.i

这里附上我自己下载成功的文件,如果读者下载失败,可以使用我的。[见文章末尾附件]

 

五、配置CMake的TBB头文件和库

经过前面步骤的Configure,搜索TBB_,会有TBB的选项跑出来,需要填写:(截图仅举例x86的情况,x64一样)

Windows环境使用VS2017编译opencv-4.2.0和opencv_contrib-4.2.0源码,支持TBB_第4张图片

x86

F:/Machine Vision/OpenCV/source/tbb-2019_U9/include
F:/Machine Vision/OpenCV/source/tbb-2019_U9/build/vs2013/Win32/Release/tbb.lib
F:/Machine Vision/OpenCV/source/tbb-2019_U9/build/vs2013/Win32/Debug/tbb_debug.lib
F:/Machine Vision/OpenCV/source/tbb-2019_U9/include/tbb/tbb_stddef.h

x64

F:/Machine Vision/OpenCV/source/tbb-2019_U9/include
F:/Machine Vision/OpenCV/source/tbb-2019_U9/build/vs2013/x64/Release/tbb.lib
F:/Machine Vision/OpenCV/source/tbb-2019_U9/build/vs2013/x64/Debug/tbb_debug.lib
F:/Machine Vision/OpenCV/source/tbb-2019_U9/include/tbb/tbb_stddef.h

执行Configure,再次执行Configure(因为TBB_VER_FILE字段此时才跑出来),然后Generate,最后Open Project,打开VS工程...

源码F:\Machine Vision\OpenCV\source\opencv-4.2.0\build_x86\cvconfig.h,可以看到宏HAVE_TBB已定义!

/* Intel Threading Building Blocks */
#define HAVE_TBB

 

六、VS-生成-批生成

1、\opencv-4.2.0\build_x86\OpenCV.sln

2、\opencv-4.2.0\build_x64\OpenCV.sln

Windows环境使用VS2017编译opencv-4.2.0和opencv_contrib-4.2.0源码,支持TBB_第5张图片

 

七、附件

1、编译opencv-4.2.0需要的opencv_videoio_ffmpeg.dll,opencv_videoio_ffmpeg_64.dll

https://download.csdn.net/download/libaineu2004/12150698

2、tbb-2019_U9源码和vs2017编译生成的库

https://download.csdn.net/download/libaineu2004/12150701

3、opencv-4.2.0编译的库文件和测试案例

https://download.csdn.net/download/libaineu2004/12150708

 

八、姊妹篇
《解决MFC使用OpenCV动态库会误报内存泄露的问题(太多人遇到这个问题了)》

《macOS环境使用clang/makefile编译opencv-4.2.0和opencv_contrib-4.2.0源码》

 

 

---

参考文献

https://blog.csdn.net/weijifen000/article/details/93377143

https://blog.csdn.net/gggttt222/article/details/79482033

 

你可能感兴趣的:(OpenCV,vs2017,opencv,opencv_contrib,tbb,ffmpeg)