opencv目前已经支持caffe训练模型的读取,以及使用模型进行预测,这个功能是dnn模块实现的,而这个模块位于opencv_contrib中,此前编译的opencv3.2.0并没有将opencv_contrib中的模块加进来。因此,这里重新将opencv_contrib加入到opencv3.2.0进行编译。
这里假定已经安装了vs2013(或vs2015)和cmake等,没有安装的要先行安装好,再继续接下来的操作。
1、下载opencv和opencv_contrib源码
1.1 下载opencv3.2.0源码(https://github.com/opencv/opencv/releases/tag/3.2.0)。
1.2 下载opencv_contrib源码(https://github.com/opencv/opencv_contrib/releases)
注意:一定要下载和OpenCV源码版本一致的版本(这里均是3.2.0版本)。
2、Cmake配置与编译
2.1 将opencv源码和opencv_contrib源码均解压到编译文件目录下(这里是D:\Libraries\OpenCV320)。
2.2 在编译文件夹下新建opencv320-build和msvc2013_64文件夹,分别作为编译目录和安装目录。
打开Cmake,添加源码目录和编译目录,configure,选择Visual Studio 12 2013 Win64作为生成工具,finish,如下图。(如报错,请参考第5部分的常见问题与解决方案)
2.3 在OPENCV_EXTRA_MODULES_PATH选项中添加opencv_contribute中的modules路径。
同时,修改安装路径:
添加debug后缀,以避免安装时,release版本的将debug版本的覆盖掉。
继续configure,成功后,点generate,生成编译工程成功。(如报错,请参考第5部分的常见问题与解决方案)
3、vs2013编译与安装
generate成功以后,在opencv320-build文件夹下,会生成如下众多文件,打开OpenCV.sln。
分别在Debug和Release环境下,先BUILD->Build Solution,再将INSTALL设为启动项,BUILD->Project Only->Build Only Install。
编译安装成功,在msvc2013_64文件夹下,会看到如下文件夹:
4、配置opencv的环境。
4.1 设置环境变变量,将安装文件夹下的bin文件夹目录添加到环境变量路径中。
4.2 在编译文件夹下添加opencv320.props文件(具体位置和名称可以根据需要设定),并向该文件中添加如下内容(主要是头文件和静态库),保存。在vs2013中使用时opencv时,只需要将改文件添加到工程的property manager中即可。
D:\Libraries\OpenCV320\msvc2013-64\include;$(IncludePath)
D:\Libraries\OpenCV320\msvc2013-64\x64\vc12\lib;$(LibraryPath)
opencv_calib3d320d.lib;opencv_core320d.lib;
opencv_cudaarithm320d.lib;opencv_cudabgsegm320d.lib;opencv_cudacodec320d.lib;
opencv_cudafeatures2d320d.lib;opencv_cudafilters320d.lib;opencv_cudaimgproc320d.lib;
opencv_cudalegacy320d.lib;opencv_cudaobjdetect320d.lib;opencv_cudaoptflow320d.lib;
opencv_cudastereo320d.lib;opencv_cudawarping320d.lib;opencv_cudev320d.lib;
opencv_features2d320d.lib;opencv_flann320d.lib;opencv_highgui320d.lib;
opencv_imgcodecs320d.lib;opencv_imgproc320d.lib;opencv_ml320d.lib;
opencv_objdetect320d.lib;opencv_photo320d.lib;opencv_shape320d.lib;
opencv_stitching320d.lib;opencv_superres320d.lib;opencv_video320d.lib;
opencv_videoio320d.lib;opencv_videostab320d.lib;
opencv_aruco320d.lib;opencv_bgsegm320d.lib;opencv_bioinspired320d.lib;
opencv_ccalib320d.lib;opencv_datasets320d.lib;opencv_dnn320d.lib;
opencv_dpm320d.lib;opencv_face320d.lib;opencv_fuzzy320d.lib;
opencv_line_descriptor320d.lib;opencv_optflow320d.lib;opencv_phase_unwrapping320d.lib;
opencv_plot320d.lib;opencv_reg320d.lib;opencv_rgbd320d.lib;opencv_saliency320d.lib;
opencv_stereo320d.lib;opencv_structured_light320d.lib;opencv_superres320d.lib;
opencv_surface_matching320d.lib;opencv_text320d.lib;opencv_tracking320d.lib;
opencv_xfeatures2d320d.lib;opencv_ximgproc320d.lib;opencv_xobjdetect320d.lib;
opencv_xphoto320d.lib;
%(AdditionalDependencies)
opencv_calib3d320.lib;opencv_core320.lib;
opencv_cudaarithm320.lib;opencv_cudabgsegm320.lib;opencv_cudacodec320.lib;
opencv_cudafeatures2d320.lib;opencv_cudafilters320.lib;opencv_cudaimgproc320.lib;
opencv_cudalegacy320.lib;opencv_cudaobjdetect320.lib;opencv_cudaoptflow320.lib;
opencv_cudastereo320.lib;opencv_cudawarping320.lib;opencv_cudev320.lib;
opencv_features2d320.lib;opencv_flann320.lib;opencv_highgui320.lib;
opencv_imgcodecs320.lib;opencv_imgproc320.lib;opencv_ml320.lib;
opencv_objdetect320.lib;opencv_photo320.lib;opencv_shape320.lib;
opencv_stitching320.lib;opencv_superres320.lib;opencv_video320.lib;
opencv_videoio320.lib;opencv_videostab320.lib;
opencv_aruco320.lib;opencv_bgsegm320.lib;opencv_bioinspired320.lib;
opencv_ccalib320.lib;opencv_datasets320.lib;opencv_dnn320.lib;
opencv_dpm320.lib;opencv_face320.lib;opencv_fuzzy320.lib;
opencv_line_descriptor320.lib;opencv_optflow320.lib;opencv_phase_unwrapping320.lib;
opencv_plot320.lib;opencv_reg320.lib;opencv_rgbd320.lib;opencv_saliency320.lib;
opencv_stereo320.lib;opencv_structured_light320.lib;opencv_superres320.lib;
opencv_surface_matching320.lib;opencv_text320.lib;opencv_tracking320.lib;
opencv_xfeatures2d320.lib;opencv_ximgproc320.lib;opencv_xobjdetect320.lib;
opencv_xphoto320.lib;
%(AdditionalDependencies)
5、编译中常见的问题与解决方案:
a) Cmake编译,加入opencv_contrib中的modules后,进行configure,有些模块会报错,只需要将相应的模块勾选掉继续configure即可。
b) 如果编译的过程中出现反复,虽然configure成功,但是generate失败,或者generate成功,但是使用vs编译时出错。最好的办法是将此前的文件都删除,重新解压源码,进行Cmake配置和编译。
c) 如果此前系统中已经配置过opencv,建议将opencv的执行目录从环境变量里清除掉。
d) Cmake配置的过程中要保证网络的通畅,如果由于长时间没有下载第三方依赖库文件不成功而报错,可以直接在谷歌或度娘上搜索相关文件,下载下来手动放到相关文件夹下,再继配置即可。
e) opencv和opencv_contrib版本一定要一致,否则配置和编译会出错。
至此,编译成功,下一篇将介绍,如何在dnn中调用caffe的训练模型。
----------------------
参考:
[1] http://docs.opencv.org/3.2.0/de/d25/tutorial_dnn_build.html
[2] http://answers.opencv.org/question/147923/build-error-open-cv32-with-extra-libs/
2017.07.19