在之前的几篇文章中,我提到了在Android、Linux中编译opencv + opencv_contrib,这篇文章主要讲在Windows中编译opencv + opencv_contrib。
首先需要准备的环境有:
- Window 10 64位:这个大家都知道,就不多说了啊;
- Cmake:编译工具,可以https://cmake.org/download/下载安装,当前的最新版本为3.10.1;
- Visual Studio:开发工具,编译的时候也需要用到,我安装的是Visual Studio 2015。
- OpenCV Windows包,下载地址:https://opencv.org/releases.html;
- OpenCV_Contrib包,下载地址:https://github.com/opencv/opencv_contrib(注意版本对应)。
将下载得到的OpenCV Windows包解压,目录为opencv,然后将下载的OpenCV_Contrib包解压放入opencv目录下,新建new_build文件夹(用来放编译之后结果):
使用cmake生成OpenCV.sln:
打开安装之后的cmake,在where is the source code中选择openCV的源代码目录:F:\opencv\sources;在where to build the binaries中选择编译为Visual Studio项目的目录:F:\opencv\new_build(这里我选择刚刚特地建立的new_build目录),如下图所示:
点击Configure按钮后,弹出对话框,选择编译器,根据本地计算机的CPU架构,这里特别要注意的是,自己机器上是否装有相应的VS版本,如果没有装,还是要编译就会出错,可能是找不到对应的工具原因,以及选择X86和X64),这里用的是VS 2015。
设置完成之后点击“Generate”开始生成工程,.第一次编译完成之后,我们需要将额外的opencv_contrib加到工程中进行第二次编译,在配置表中找到“OPENCV_EXTRA_MODULES_PATH”,设置其参数值为open_contrib源码包中的modles目录,我的目录是“F:\opencv\opencv_contrib\modules”:
再次点击“Generate”进行第二次编译:
这时候我们已经可以看见用cmake工具编译得到的OpenCV.sln:
用VS打开OpenCV.sln工程,编译生成Debug和Release库:
用VS 2015打开OpenCV.sln工程,在解决方案中可以查看工程目录:
编译生成debug版本的库,记得在此之前要选择编译的平台信息,这就是编译生成debug版本和release版本的区别,也可以选择release,因为自己的工程可能要用到相应的动态链接库:
在解决方案中选中工程,右键选择重新生成解决方案:
编译成功:
.找到CMakeTargets中的INSTALL,然后右键选择“仅限于项目”-->“仅生成INSTALL”:
完成编译后,Release模式下同理。此时,有了install目录。该目录包含了我们需要的头文件、库文件。
把新的库文件配置到到项目中:
VC++目录-->包含目录,添加:
E:\OpenCV320\opencv\new_build\install\include
VC++目录-->库目录,添加:
E:\OpenCV320\opencv\new_build\install\x64\vc14\lib
链接器-->输入-->附加依赖项,添加: (注意添加的库与编译选项要一致,需要注意debug比release的文件名多了个d)
opencv_aruco320.lib
opencv_aruco320d.lib
opencv_bgsegm320.lib
opencv_bgsegm320d.lib
opencv_bioinspired320.lib
opencv_bioinspired320d.lib
opencv_calib3d320.lib
opencv_calib3d320d.lib
opencv_ccalib320.lib
opencv_ccalib320d.lib
opencv_core320.lib
opencv_core320d.lib
opencv_datasets320.lib
opencv_datasets320d.lib
opencv_dnn320.lib
opencv_dnn320d.lib
opencv_dpm320.lib
opencv_dpm320d.lib
opencv_face320.lib
opencv_face320d.lib
opencv_features2d320.lib
opencv_features2d320d.lib
opencv_flann320.lib
opencv_flann320d.lib
opencv_fuzzy320.lib
opencv_fuzzy320d.lib
opencv_highgui320.lib
opencv_highgui320d.lib
opencv_imgcodecs320.lib
opencv_imgcodecs320d.lib
opencv_line_descriptor320.lib
opencv_line_descriptor320d.lib
opencv_ml320.lib
opencv_ml320d.lib
opencv_objdetect320.lib
opencv_objdetect320d.lib
opencv_optflow320.lib
opencv_optflow320d.lib
opencv_phase_unwrapping320.lib
opencv_phase_unwrapping320d.lib
opencv_photo320.lib
opencv_photo320d.lib
opencv_plot320.lib
opencv_plot320d.lib
opencv_reg320.lib
opencv_reg320d.lib
opencv_rgbd320.lib
opencv_rgbd320d.lib
opencv_saliency320.lib
opencv_saliency320d.lib
opencv_shape320.lib
opencv_shape320d.lib
opencv_stereo320.lib
opencv_stereo320d.lib
opencv_stitching320.lib
opencv_stitching320d.lib
opencv_structured_light320.lib
opencv_structured_light320d.lib
opencv_superres320.lib
opencv_superres320d.lib
opencv_surface_matching320.lib
opencv_surface_matching320d.lib
opencv_text320.lib
opencv_text320d.lib
opencv_tracking320.lib
opencv_tracking320d.lib
opencv_video320.lib
opencv_video320d.lib
opencv_videoio320.lib
opencv_videoio320d.lib
opencv_videostab320.lib
opencv_videostab320d.lib
opencv_xfeatures2d320.lib
opencv_xfeatures2d320d.lib
opencv_ximgproc320.lib
opencv_ximgproc320d.lib
opencv_xobjdetect320.lib
opencv_xobjdetect320d.lib
opencv_xphoto320.lib
opencv_xphoto320d.lib
kernel32.lib
user32.lib
gdi32.lib
winspool.lib
comdlg32.lib
advapi32.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
odbc32.lib
odbccp32.lib
这样,我们就可以在VS中使用OpenCV了。
需要提到的一个点,所需要使用Sift等算法,需要引入xfeatures2d命名空间:
using namespace xfeatures2d;