OpenCv学习———OpenCv永久配置(Win8.1+VS2013+OpenCv2.4.10)
避免每次新建工程时对OpenCv进行配置一次的情况,提出OpenCv永久配置,介绍一种通过添加现有属性表的方式来同时配置Win32和x64,debug和release,并不需要单独配置环境变量。在这里非常感谢采石工的分享,以及协助远程的调试。
1 新建配置属性表
以2.4.10为例。新建一个文本文件,将其重命名为”OpenCV2.0.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 <OpenCVDir>D:\opencv\build\</OpenCVDir> 6 </PropertyGroup> 7 <PropertyGroup> 8 <_PropertySheetDisplayName>opencv</_PropertySheetDisplayName> 9 <LocalDebuggerEnvironment Condition="'$(Platform)'=='Win32'">PATH=$(OpenCVDir)\x86\vc12\bin;$(PATH)</LocalDebuggerEnvironment> 10 <LocalDebuggerEnvironment Condition="'$(Platform)'=='X64'">PATH=$(OpenCVDir)\x64\vc12\bin;$(PATH)</LocalDebuggerEnvironment> 11 <IncludePath>$(OpenCVDir)\include;$(OpenCVDir)\include\opencv;$(OpenCVDir)\include\opencv2;$(IncludePath)</IncludePath> 12 <LibraryPath Condition="'$(Platform)'=='Win32'">$(OpenCVDir)\x86\vc12\lib;$(LibraryPath)</LibraryPath> 13 <LibraryPath Condition="'$(Platform)'=='X64'">$(OpenCVDir)\x64\vc12\lib;$(LibraryPath)</LibraryPath> 14 </PropertyGroup> 15 <ItemDefinitionGroup> 16 <Link Condition="'$(Configuration)'=='Debug'"> 17 <AdditionalDependencies>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_ml2410d.lib;opencv_nonfree2410d.lib;opencv_objdetect2410d.lib;opencv_ocl2410d.lib;opencv_photo2410d.lib;opencv_stitching2410d.lib;opencv_superres2410d.lib;opencv_ts2410d.lib;opencv_video2410d.lib;opencv_videostab2410d.lib;%(AdditionalDependencies)</AdditionalDependencies> 18 </Link> 19 <Link Condition="'$(Configuration)'=='Release'"> 20 <AdditionalDependencies>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;opencv_nonfree2410.lib;opencv_objdetect2410.lib;opencv_ocl2410.lib;opencv_photo2410.lib;opencv_stitching2410.lib;opencv_superres2410.lib;opencv_ts2410.lib;opencv_video2410.lib;opencv_videostab2410.lib;%(AdditionalDependencies)</AdditionalDependencies> 21 </Link> 22 </ItemDefinitionGroup> 23 <ItemGroup /> 24 </Project>
保存OpenCV2.0.props之后,将文件关闭。下面给出OpenCv2.4.12配置属性详解,只要修改相应的版本信息就可以就运行。
2 工程配置
新建一个Win32 控制台工程(Win32 Console Application)。在属性管理器中添加刚才保存的属性表。所有不同配置可以使用同一个属性表。
注意:如果属性表和工程在同一个分区下,那么工程文件会使用相对路径。这样一来,拷贝工程到不同的目录后,工程就不能被正确加载了。所以,建议将属性表放到某个特定的分区,工程目录最好跟属性表不要在同一个分区。控制台用main函数,win32是WinMain函数,一般建控制台工程,视情况而定。
3 添加配置属性表
在属性管理器下,右击工程名,添加现有现有属性表。如果你找不到属性管理器,你可以视图->其他窗口->属性管理器
4 代码测试
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <cv.h> #include <highgui.h> int main() { IplImage* img = cvLoadImage("E:\\测试图片\\2016112309\\test.jpg",1); if (!img) { printf("error"); } cvNamedWindow("OpenCV", CV_WINDOW_AUTOSIZE); cvShowImage("OpenCV", img); cvWaitKey(0); cvReleaseImage(&img); cvDestroyWindow("OpenCV"); return 0; }