因为VS配置OpenCV好多新手都很难一次配置成功,而且OpenCV库每新建一个项目都要配置很是麻烦,所以今天就给大家介绍一个“一劳永逸”的方法。
注:理论上只要VS和OpenCV是版本兼容的,该方法都起作用。如果不能成功,请记得将OpenCV安装目录添加到系统路径。
时间仓促、水平有限......欢迎指正!
- VS和OpenCV安装
说重点,鄙人VS安装到D:\Program Files\Microsoft Visual Studio 10.0(你们随意)。OpenCV安装到:D:\opencv(你们也随意)。
2.修改 Microsoft.Cpp.Win32.user.props 文件
用Everything(http://yunpan.cn/cAFQmN8LQ3ig5 提取码 e10c)这个软件在本地查找 Microsoft.Cpp.Win32.user.props 文件,并打开编辑。鄙人的文件内容如下(咱们可能 不一样,不过没关系),只需添加红色部分(注意分号隔开),将其替换成自己OpenCV目录下的对应目录(分别是可执行文件bin目录,包含文件include目录,库文件lib目录)。
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup>
<ExecutablePath>D:\opencv\build\x86\vc10\bin;$(VCInstallDir)bin;$(WindowsSdkDir)bin\NETFX 4.0 Tools;$(WindowsSdkDir)bin;$(VSInstallDir)Common7\Tools\bin;$(VSInstallDir)Common7\tools;$(VSInstallDir)Common7\ide;$(ProgramFiles)\HTML Help Workshop;$(FrameworkSDKDir)\bin;$(MSBuildToolsPath32);$(VSInstallDir);$(SystemRoot)\SysWow64;$(FxCopDir);$(PATH);</ExecutablePath>
<IncludePath>D:\opencv\build\include;D:\opencv\build\include\opencv;D:\opencv\build\include\opencv2;$(IncludePath)</IncludePath>
<ReferencePath>$(VCInstallDir)atlmfc\lib;$(VCInstallDir)lib</ReferencePath>
<LibraryPath>D:\opencv\build\x86\vc10\lib;$(LibraryPath)</LibraryPath>
<SourcePath>$(VCInstallDir)atlmfc\src\mfc;$(VCInstallDir)atlmfc\src\mfcm;$(VCInstallDir)atlmfc\src\atl;$(VCInstallDir)crt\src;</SourcePath>
<ExcludePath>$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSdkDir)include;$(FrameworkSDKDir)\include;$(MSBuildToolsPath32);$(VCInstallDir)atlmfc\lib;$(VCInstallDir)lib;</ExcludePath>
</PropertyGroup>
</Project>
3.新建工程、添加头文件
在stdafx.h文件中添加如下代码(这些包含的头文件不一定都能用得到,但是一般的处理都能够满足,把红色部分替换成你的OpenCV版本,接下来可以在main函数中写你的代码了):
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include <opencv/highgui.h>
#pragma comment(lib, "opencv_core249d.lib")
#pragma comment(lib, "opencv_highgui249d.lib")
#pragma comment(lib, "opencv_imgproc249d.lib")