下面开始介绍如何配置,我用的系统是win8.1 64位系统,vs用的是vs3013 ultimate,先到官网下载opencv 我用的的版本是最新的版本3.0 ALPHA,下载下来直接执行即可,实际上就是解压,我解压到D:Program Files文件夹下。
配置环境变量,右击我的电脑选择属性,在左侧选择高级系统设置,会弹出系统属性对话框,
点击环境变量,点击新建环境变量,变量名可以任意,我在这起的是OPENCV,将变量值设为opencv/build的路径
添加路径,点击编辑path,在path中添加;%OPENCV%x86vc12in,其中vc12对应的是vs2013,vc11对应的是vs2012,vc10对应的是vs2010,根据自己的版本进行选择
新建opencv的工程属性表,在opencv目录下新建opencv300. props,可以新建一个txt文档,将后缀名改为.props即可,下面是我的属性工程表,第7,8行分别是32位和64位平台,如果没有定义系统变量,里面的$(OPENCV)可以换成绝对路径,我的是D:Program Filesopencvuild,在vc12下面有3个文件夹bin,lib,staticlib,将lib和staticlib添加到LibraryPath中,可能版本不同需要添加的也不同,主要是查看vc12有哪些文件夹,并且文件夹里包含文件名含有300的lib文件添加到LibraryPath中,300是指opencv的版本为opencv3.0.0,vc12根据vs版本不同也不同,第12行和第15行开始的是包含的lib文件,其中第12开始包含的是文件名含有300但不以d结尾的lib文件,其中第15开始包含的是文件名含有300以d结尾的lib文件,你可以根据自己的opencv版本进行修改。
1 <?xml version="1.0" encoding="utf-8"?> 2 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 3 <ImportGroup Label="PropertySheets" /> 4 <PropertyGroup Label="UserMacros" /> 5 <PropertyGroup> 6 <IncludePath>$(OPENCV)include;$(IncludePath)</IncludePath> 7 <LibraryPath Condition="'$(Platform)'=='Win32'">$(OPENCV)x86vc12staticlib;$(OPENCV)x86vc12lib;$(LibraryPath)</LibraryPath> 8 <LibraryPath Condition="'$(Platform)'=='X64'">$(OPENCV)x64vc12staticlib;$(OPENCV)x64vc12lib;$(LibraryPath)</LibraryPath> 9 </PropertyGroup> 10 <ItemDefinitionGroup> 11 <Link Condition="'$(Configuration)'=='Debug'"> 12 <AdditionalDependencies>opencv_ts300.lib;opencv_world300.lib;opencv_calib3d300.lib;opencv_core300.lib;opencv_features2d300.lib;opencv_flann300.lib;opencv_highgui300.lib;opencv_imgcodecs300.lib;opencv_imgproc300.lib;opencv_ml300.lib;opencv_objdetect300.lib;opencv_photo300.lib;opencv_shape300.lib;opencv_stitching300.lib;opencv_superres300.lib;opencv_ts300.lib;opencv_video300.lib;opencv_videoio300.lib;opencv_videostab300.lib;%(AdditionalDependencies)</AdditionalDependencies> 13 </Link> 14 <Link Condition="'$(Configuration)'=='Release'"> 15 <AdditionalDependencies>opencv_ts300d.lib;opencv_world300d.lib;opencv_calib3d300d.lib;opencv_core300d.lib;opencv_features2d300d.lib;opencv_flann300d.lib;opencv_highgui300d.lib;opencv_imgcodecs300d.lib;opencv_imgproc300d.lib;opencv_ml300d.lib;opencv_objdetect300d.lib;opencv_photo300d.lib;opencv_shape300d.lib;opencv_stitching300d.lib;opencv_superres300d.lib;opencv_ts300d.lib;opencv_video300d.lib;opencv_videoio300d.lib;opencv_videostab300d.lib;%(AdditionalDependencies)</AdditionalDependencies> 16 </Link> 17 </ItemDefinitionGroup> 18 <ItemGroup /> 19 </Project>
编辑完项目属性表后,在vs中新建空项目test, 打开视图->其他窗口->属性管理器,
在属性管理器中添加写好的opencv300. props属性文件,在右侧边栏中选择属性管理器,右击test,选择添加现有属性表,
找到opencv300.props添加就可以了,
至此环境就配好了,运行一个实例程序吧,还有要说的是每次新建项目都需要添加opencv300.props,要将图片放进项目里面的test文件中,是里面的test文件夹,即test est下。
1 #include <opencv2/core/core.hpp> 2 #include <opencv2/highgui/highgui.hpp> 3 #include <iostream> 4 5 using namespace cv; 6 using namespace std; 7 8 int main(int argc, char** argv) 9 { 10 11 Mat image; 12 image = imread("opencv.jpg", IMREAD_COLOR); // Read the file 13 14 if (!image.data) // Check for invalid input 15 { 16 cout << "Could not open or find the image" << std::endl; 17 return -1; 18 } 19 20 namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display. 21 imshow("Display window", image); // Show our image inside it. 22 23 waitKey(0); // Wait for a keystroke in the window 24 return 0; 25 }
结显示出的图片是这样的
本文参考《vs2010+Opencv-2.4.0的配置攻略》 ,《Opencv 完美配置攻略 2014 (Win8.1 + Opencv 2.4.8 + VS 2013)》和《opencv教程》