简介
计算机视觉任务越来越多的依赖著名的开源计算机视觉库OpenCV。OpenCV 2.0 包含了一系列精心设计数据结构和经过优化的视觉算法,大家可以短时间内开发一个不错的视觉应用。OpenCV支持多种不同的硬件和软件平台,这也是它被广泛应用的一个重要原因。使用C/C++开发OpenCV程序,需要告知编译器、连接器相应的头文件目录、库文件以库文件的目录,在开发不同的项目的时候,每次都分别指定这三项内容操作起来有些麻烦,本文针对OpenCV+VS2013介绍一种通过添加属性表和合并调试环境变量的方式来简化配置过程。
本文测试过的具体情况有:
OpenCV Version | Configuration | Platform |
---|---|---|
2.4.10 | Debug, Release | x64, Win32 |
2.4.11 | Debug, Release | x64, Win32 |
2.4.12 | Debug, Release | x64, Win32 |
3.0.0 | Debug, Release | x64, Win32 |
准备OpenCV
在OpenCV的官方网站下载相应的版本(这里测试过2.4.10,2.4.11,2.4.12,3.0.0四个版本)。下载完成后,双击相应的exe文件,它会自解压到你指定的目录下。在解压后的目录下找到build目录,将此目录下的所有内容拷贝到某个固定的目录下,以后的开发都会引用这个目录,所以最好慎重选择存放位置。例如我将OpenCV2.4.10的build目录的所有内容存放在K:\OpenCV\2.4.10
这个目录下,(后面的配置过程都将以此目录为例),
将x64和x86两个目录下的vc12、vc11和vc10分别改成vc12.0、vc11.0和vc10.0
属性表
以2.4.10为例。新建一个文本文件,将其重命名为”OpenCV_2.4.10.props”(不要带引号)。用记事本编辑,拷贝如下内容:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup>
<IncludePath>K:\OpenCV\2.4.10\include\opencv;K:\OpenCV\2.4.10\include;$(IncludePath)</IncludePath>
<LibraryPath>K:\OpenCV\2.4.10\$(PlatformShortName)\vc$(VisualStudioVersion)\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup>
<Link Condition="'$(Configuration)'=='Debug'">
<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>
</Link>
<Link Condition="'$(Configuration)'=='Release'">
<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>
</Link>
</ItemDefinitionGroup>
</Project>
保存OpenCV_2.4.10.props之后,将文件关闭。
工程配置
新建一个Win32 控制台工程(Win32 Console Application)。在属性管理器中添加刚才保存的属性表。所有不同配置可以使用同一个属性表,因为属性表对所有情况做了处理。
注意:如果属性表和工程在同一个分区下,那么工程文件会使用相对路径。这样一来,拷贝工程到不同的目录后,工程就不能被正确加载了。所以,建议将属性表放到某个特定的分区,工程目录最好跟属性表不要在同一个分区。
执行编译后程序
编译成功后,可能会提示某某dll找不到。将下面的内容拷贝到工程的“属性–> 调试 –> 环境”。
PATH=K:\OpenCV\2.4.10\$(PlatformShortName)\vc$(VisualStudioVersion)\bin;$(PATH) $(LocalDebuggerEnvironment)
附加:其他几个版本的属性表
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup>
<IncludePath>K:\OpenCV\2.4.11\include\opencv;K:\OpenCV\2.4.11\include;$(IncludePath)</IncludePath>
<LibraryPath>K:\OpenCV\2.4.11\$(PlatformShortName)\vc$(VisualStudioVersion)\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup>
<Link Condition="'$(Configuration)'=='Debug'">
<AdditionalDependencies>opencv_calib3d2411d.lib;opencv_contrib2411d.lib;opencv_core2411d.lib;opencv_features2d2411d.lib;opencv_flann2411d.lib;opencv_gpu2411d.lib;opencv_highgui2411d.lib;opencv_imgproc2411d.lib;opencv_legacy2411d.lib;opencv_ml2411d.lib;opencv_nonfree2411d.lib;opencv_objdetect2411d.lib;opencv_ocl2411d.lib;opencv_photo2411d.lib;opencv_stitching2411d.lib;opencv_superres2411d.lib;opencv_ts2411d.lib;opencv_video2411d.lib;opencv_videostab2411d.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Link Condition="'$(Configuration)'=='Release'">
<AdditionalDependencies>opencv_calib3d2411.lib;opencv_contrib2411.lib;opencv_core2411.lib;opencv_features2d2411.lib;opencv_flann2411.lib;opencv_gpu2411.lib;opencv_highgui2411.lib;opencv_imgproc2411.lib;opencv_legacy2411.lib;opencv_ml2411.lib;opencv_nonfree2411.lib;opencv_objdetect2411.lib;opencv_ocl2411.lib;opencv_photo2411.lib;opencv_stitching2411.lib;opencv_superres2411.lib;opencv_ts2411.lib;opencv_video2411.lib;opencv_videostab2411.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
</Project>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup>
<IncludePath>K:\OpenCV\2.4.12\include\opencv;K:\OpenCV\2.4.12\include;$(IncludePath)</IncludePath>
<LibraryPath>K:\OpenCV\2.4.12\$(PlatformShortName)\vc$(VisualStudioVersion)\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup>
<Link Condition="'$(Configuration)'=='Debug'">
<AdditionalDependencies>opencv_calib3d2412d.lib;opencv_contrib2412d.lib;opencv_core2412d.lib;opencv_features2d2412d.lib;opencv_flann2412d.lib;opencv_gpu2412d.lib;opencv_highgui2412d.lib;opencv_imgproc2412d.lib;opencv_legacy2412d.lib;opencv_ml2412d.lib;opencv_nonfree2412d.lib;opencv_objdetect2412d.lib;opencv_ocl2412d.lib;opencv_photo2412d.lib;opencv_stitching2412d.lib;opencv_superres2412d.lib;opencv_ts2412d.lib;opencv_video2412d.lib;opencv_videostab2412d.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Link Condition="'$(Configuration)'=='Release'">
<AdditionalDependencies>opencv_calib3d2412.lib;opencv_contrib2412.lib;opencv_core2412.lib;opencv_features2d2412.lib;opencv_flann2412.lib;opencv_gpu2412.lib;opencv_highgui2412.lib;opencv_imgproc2412.lib;opencv_legacy2412.lib;opencv_ml2412.lib;opencv_nonfree2412.lib;opencv_objdetect2412.lib;opencv_ocl2412.lib;opencv_photo2412.lib;opencv_stitching2412.lib;opencv_superres2412.lib;opencv_ts2412.lib;opencv_video2412.lib;opencv_videostab2412.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
</Project>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup>
<IncludePath>K:\OpenCV\3.0.0\include\opencv;K:\OpenCV\3.0.0\include;$(IncludePath)</IncludePath>
<LibraryPath>K:\OpenCV\3.0.0\$(PlatformShortName)\vc$(VisualStudioVersion)\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup>
<Link Condition="'$(Configuration)'=='Debug'">
<AdditionalDependencies>opencv_ts300d.lib;opencv_world300d.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Link Condition="'$(Configuration)'=='Release'">
<AdditionalDependencies>opencv_ts300.lib;opencv_world300.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
</Project>
测试程序工程及代码
注意:必须修改相应项目中的属性表路径,否则不能用!
注意:属性表要换成你自己的,就是路径要跟你自己的硬盘上相应的路径对应好!否则不能用!
http://pan.baidu.com/s/1i43Pflr