下载opencv 249
下载安装vs2010旗舰版
(vs2013配置类似,需要变化的主要是版本号,就不再赘述)
一 配置系统环境:
配置环境变量
将opencv的主目录添加为新的系统变量OPENCV
G:Program Files (x86)opencvopencv249
向系统变量‘path’中添加opencv关于vs2010的动态链接库
;%OPENCV%uildx86vc10in
( 可以不执行此步骤:将opencv关于vs2010的动态链接库目录G:Program Files (x86)opencvopencv249uildx86vc10in所有文件复制到系统目录C:WindowsSystem32下 )
完成后注销计算机或者重启计算机使环境变量生效
二 配置vs2010项目
创建控制台应用程序
创建cpp(步骤省略)
设置项目属性,加入需要的文件目录或者引用的库文件
方法一: 手动设置
包含目录
$(OPENCV)uildinclude
$(OPENCV)uildincludeopencv
$(OPENCV)uildincludeopencv2
库目录
$(OPENCV)uildx86vc10lib
设置要包含的库文件
opencv249中所有依赖lib
dubug 19个
opencv_calib3d249d.lib
opencv_contrib249d.lib
opencv_core249d.lib
opencv_features2d249d.lib
opencv_flann249d.lib
opencv_gpu249d.lib
opencv_highgui249d.lib
opencv_imgproc249d.lib
opencv_legacy249d.lib
opencv_ml249d.lib
opencv_nonfree249d.lib
opencv_objdetect249d.lib
opencv_ocl249d.lib
opencv_photo249d.lib
opencv_stitching249d.lib
opencv_superres249d.lib
opencv_ts249d.lib
opencv_video249d.lib
opencv_videostab249d.lib
release 19个
opencv_calib3d249.lib
opencv_contrib249.lib
opencv_core249.lib
opencv_features2d249.lib
opencv_flann249.lib
opencv_gpu249.lib
opencv_highgui249.lib
opencv_imgproc249.lib
opencv_legacy249.lib
opencv_ml249.lib
opencv_nonfree249.lib
opencv_objdetect249.lib
opencv_ocl249.lib
opencv_photo249.lib
opencv_stitching249.lib
opencv_superres249.lib
opencv_ts249.lib
opencv_video249.lib
opencv_videostab249.lib
方法二:加载配置文件批量导入相关配置
vc10
1. 新建文本文档,放入以下代码后保存为后缀为.props的文件,文件名是导入后项目中显示的名字
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)uildinclude;$(IncludePath)</IncludePath> 7 <LibraryPath Condition="'$(Platform)'=='Win32'">$(OPENCV)uildx86vc10lib;$(LibraryPath)</LibraryPath> 8 <LibraryPath Condition="'$(Platform)'=='X64'">$(OPENCV)x64vc10lib;$(LibraryPath)</LibraryPath> 9 </PropertyGroup> 10 <ItemDefinitionGroup> 11 <Link Condition="'$(Configuration)'=='Debug'"> 12 <AdditionalDependencies>opencv_calib3d249d.lib;opencv_contrib249d.lib;opencv_core249d.lib;opencv_features2d249d.lib;opencv_flann249d.lib;opencv_gpu249d.lib;opencv_highgui249d.lib;opencv_imgproc249d.lib;opencv_legacy249d.lib;opencv_ml249d.lib;opencv_nonfree249d.lib;opencv_objdetect249d.lib;opencv_ocl249d.lib;opencv_photo249d.lib;opencv_stitching249d.lib;opencv_superres249d.lib;opencv_ts249d.lib;opencv_video249d.lib;opencv_videostab249d.lib;%(AdditionalDependencies)</AdditionalDependencies> 13 </Link> 14 <Link Condition="'$(Configuration)'=='Release'"> 15 <AdditionalDependencies>opencv_calib3d249.lib;opencv_contrib249.lib;opencv_core249.lib;opencv_features2d249.lib;opencv_flann249.lib;opencv_gpu249.lib;opencv_highgui249.lib;opencv_imgproc249.lib;opencv_legacy249.lib;opencv_ml249.lib;opencv_nonfree249.lib;opencv_objdetect249.lib;opencv_ocl249.lib;opencv_photo249.lib;opencv_stitching249.lib;opencv_superres249.lib;opencv_ts249.lib;opencv_video249.lib;opencv_videostab249.lib;%(AdditionalDependencies)</AdditionalDependencies> 16 </Link> 17 </ItemDefinitionGroup> 18 <ItemGroup /> 19 </Project>
2. 打开项目的属性管理器(默认就在解决方案窗口那几个标签窗口中)
右键项目,选择 添加现有属性表,先择刚刚保存的配置文件即可。如图:
导入成功之后发现所有配置都已经完成:
至此vs2010项目配置完毕
接下来验证环境是否成功
准备:将图片像素为512*512重命名为test.jpg存储到项目目录下
运行代码
1 #include<iostream> 2 #include<opencv/highgui.h> 3 using namespace std; 4 5 int main(int argc,char ** argv) 6 { 7 argv[1]="test.jpg"; 8 try 9 { 10 if(!argv[1]) 11 throw argv[1]; 12 IplImage* img = cvLoadImage(argv[1]); 13 cvShowImage("cvtest",img); 14 cvWaitKey(0); 15 cvReleaseImage(&img); 16 } 17 catch(...) 18 { 19 cout<<"File doesn't exist!"<<endl; 20 cin.get(); 21 exit(1); 22 } 23 }
成功运行
可能出现的问题:
1. 报错:
错误 1 error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 D:Documentsvisual studio 2010opencvTestopencvTestLIN
原因:
windows自动更新造成vs编译器的cvtres.exe的版本过低,如果windows再次更新,则还可能出现这样的问题,届时再重复一下上述步骤即可
解决方案:
用 目录 C:Windowswinsxsx86_netfx-cvtres_for_vc_and_vb_xxxxxxxxxxxxx 下的 cvtres.exe 替换目录 C:Program FilesMicrosoft Visual Studio 10.0VCin 下的
cvtres.exe
目录 C:Windowswinsxs下可能存在多个以x86_netfx-cvtres_for_vc_and_vb_开头的目录,如图,选择最新一次的即可
over