1.
下载 Opencv for Windows 最新版本,本文是 Opencv 2.4.5。双击后会出现解压,实际上就是“安装”了,路径填写为 E:\Opencv2.4.5,然后确定。
下载 VS 2010,并安装。
2.配置环境变量
在系统变量里面新建变量,名为 OPENCV ,值为自己解压opencv路径下的build路径,如 E:\Opencv2.4.5\opencv\build。
这一步是方便以后如果opencv路径改变了,只需要修改此变量就可以了,而不必做大范围修改。
在系统变量里面编辑 Path 变量,在末尾添加 ;%OPENCV%\x86\vc10\bin
x86和x64分别表示32bit和64bit的VS工程,根据自己的工程来修改,否则虽编译成功但会运行错误;vc10, vc11, vc12 分别表示VS2010, VS2012, VS2013的Visual Studio使用的编译器版本,根据自己的VS版本来填写正确的编译器版本号。;%OPENCV%表示opencv的路径。
3.编写opencv的vs工作属性表
新建一个text文件,把下面的内容拷进去,并把文件命名为 opencv245.props ,放在E:\Opencv2.4.5\opencv目录下即可。
opencv245.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>$(OPENCV)\include;$(IncludePath)</IncludePath> <LibraryPath Condition="'$(Platform)'=='Win32'">$(OPENCV)\x86\vc10\lib;$(LibraryPath)</LibraryPath> <LibraryPath Condition="'$(Platform)'=='X64'">$(OPENCV)\x64\vc10\lib;$(LibraryPath)</LibraryPath> </PropertyGroup> <ItemDefinitionGroup> <Link Condition="'$(Configuration)'=='Debug'"> <AdditionalDependencies>opencv_calib3d245d.lib;opencv_contrib245d.lib;opencv_core245d.lib;opencv_features2d245d.lib;opencv_flann245d.lib;opencv_gpu245d.lib;opencv_highgui245d.lib;opencv_imgproc245d.lib;opencv_legacy245d.lib;opencv_ml245d.lib;opencv_nonfree245d.lib;opencv_objdetect245d.lib;opencv_photo245d.lib;opencv_stitching245d.lib;opencv_superres245d.lib;opencv_ts245d.lib;opencv_video245d.lib;opencv_videostab245d.lib;%(AdditionalDependencies)</AdditionalDependencies> </Link> <Link Condition="'$(Configuration)'=='Release'"> <AdditionalDependencies>opencv_calib3d245.lib;opencv_contrib245.lib;opencv_core245.lib;opencv_features2d245.lib;opencv_flann245.lib;opencv_gpu245.lib;opencv_highgui245.lib;opencv_imgproc245.lib;opencv_legacy245.lib;opencv_ml245.lib;opencv_nonfree245.lib;opencv_objdetect245.lib;opencv_ocl245.lib;opencv_photo245.lib;opencv_stitching245.lib;opencv_superres245.lib;opencv_ts245.lib;opencv_video245.lib;opencv_videostab245.lib;%(AdditionalDependencies)</AdditionalDependencies> </Link> </ItemDefinitionGroup> <ItemGroup /> </Project>
4.以后后在VS工程中配置Opencv只需要导入这份属性表就可以了
导入方法:
属性管理器 -> 右键 "test"(工程名) -> 添加现有属性表,找到刚才放到E:\Opencv2.4.5\opencv目录下的opencv245.props即可。
以后只要调用opencv只要导入上面的属性表即可使用,如图所示,已导入后的结果
5.测试
文件 -> 新建 -> 项目 -> Visual C++ -> Win32 控制台应用程序(输入名称test)
确定 -> 下一步 -> 附加选项选“空项目” -> 完成
解决方案资源管理器 -> 源文件(右键)-> 添加 -> 新建项
添加如下代码:
#include <opencv2\opencv.hpp> #include <iostream> #include <string> using namespace cv; using namespace std; int main() { Mat img = imread("pp.jpg"); if (img.empty()) { cout << "error"; return -1; } imshow("pp的靓照", img); waitKey(); return 0; }
按照第4步的步骤 添加现有属性表
把自己的靓照改名为pp.jpg,然后放到工程项目的test文件夹里面(是里面那个test文件夹)