前言
项目几乎都是图像相关的,一般都会用到opencv开源库,就涉及到windows下opencv的环境配置问题,本文对此进行介绍。
环境
系统环境:win10_x64(其他windows系统类似);
opencv版本:2.4.10(其他版本类似);
VS版本:VS2013;
实现步骤
1.下载安装opencv开源库;
2.配置环境变量;
3.配置项目属性;
4.测试工程;
操作过程
1.下载安装opencv开源库;
下载对应版本的opencv源here,解压到某一目录即可,最好的是固定的,这样可以同样应用于其他工程的配置;
博主的目录是D:\lib\OpenCV\opencv2410,也是方便将所有可能用到的库安装在一个固定的路径。
2.配置环境变量;
2.1 打开win10的设置信息的系统一栏,关于 -> 系统信息 -> 高级系统设置 -> 环境变量;
2.2 用户变量一栏,新建用户变量如下,注意变量值要根据自己的安装目录进行修改;
2.3 系统变量一栏,点击path进行编辑如下图所示,注意目录按安装目录进行修改;
2.4 到此环境变量配置完毕。重启电脑或注销电脑,使环境变量生效。
注意,其他系统可能与win10的配置过程有些微不同,但大致都一样的。
3.配置项目属性;
3.1 编辑opencv工程属性表;
新建两个文件opencvdebug.props和opencvrelease.props,分别表示debug和release版本工程的opencv属性表,
主要内容含有工程使用opencv的c++目录中的包含目录和附加依赖项,后续还需要在工程中添加opencv的库目录;
opencvdebug.props
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<IncludePath>D:\lib\OpenCV\opencv2410\build\include;D:\lib\OpenCV\opencv2410\build\include\opencv;D:\lib\OpenCV\opencv2410\build\include\opencv2;$(IncludePath)</IncludePath>
<LibraryPath>$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup>
<Link>
<AdditionalDependencies>opencv_ml2410d.lib;opencv_calib3d2410d.lib;opencv_contrib2410d.lib;opencv_core2410d.lib;opencv_features2d2410d.lib;opencv_flann2410d.lib;opencv_gpu2410d.lib;opencv_highgui2410d.lib;opencv_imgproc2410d.lib;opencv_legacy2410d.lib;opencv_objdetect2410d.lib;opencv_ts2410d.lib;opencv_video2410d.lib;opencv_nonfree2410d.lib;opencv_ocl2410d.lib;opencv_photo2410d.lib;opencv_stitching2410d.lib;opencv_superres2410d.lib;opencv_videostab2410d.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup />
</Project>
opencvrelease.props
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<IncludePath>D:\lib\OpenCV\opencv2410\build\include;D:\lib\OpenCV\opencv2410\build\include\opencv;D:\lib\OpenCV\opencv2410\build\include\opencv2;$(IncludePath)</IncludePath>
<LibraryPath>$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup>
<Link>
<AdditionalDependencies>opencv_objdetect2410.lib;opencv_ts2410.lib;opencv_video2410.lib;opencv_nonfree2410.lib;opencv_ocl2410.lib;opencv_photo2410.lib;opencv_stitching2410.lib;opencv_superres2410.lib;opencv_videostab2410.lib;opencv_calib3d2410.lib;opencv_contrib2410.lib;opencv_core2410.lib;opencv_features2d2410.lib;opencv_flann2410.lib;opencv_gpu2410.lib;opencv_highgui2410.lib;opencv_imgproc2410.lib;opencv_legacy2410.lib;opencv_ml2410.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup />
</Project>
注意,属性表中的路径是对应的安装路径,另外,也可以以其他方式进行配置,详见参考部分;
3.2 将属性表分别调价到工程的属性管理器中;
属性管理器的打开方式,视图 -> 其他窗口 -> 属性管理器;
3.3 在项目属性的VC++目录的库目录中添加工程的库目录,D:\lib\OpenCV\opencv2410\build\x64\vc12\lib;
注意,所有属性的配置都需要在debug和release版本分别进行配置,这样两个版本的才均能够正常运行;
4.测试工程;
新建工程,按照以上步骤配置环境和项目属性,测试代码如下,能够正常运行即测试成功。
#include<cv.h>
#include<highgui.h>
int main(int argc, char** argv[])
{
IplImage* src = cvLoadImage("lena.jpg", CV_LOAD_IMAGE_GRAYSCALE);
cvNamedWindow("Src", CV_WINDOW_AUTOSIZE);
cvShowImage("Src", src);
cvWaitKey();
cvReleaseImage(&src);
cvDestroyAllWindows();
return ;
}
问题及注意事项
1.注意工程的平台是win32还是x64的;
2.注意需要对应工程的debug和release版本;
3.各种目录都需要对应准确;
参考
1.opencv配置;
2.百度百科的介绍;
3.完美配置攻略;
完