Visual Studio 配置 OpenCV

最近要做图像处理方面的工作,所以花一些时间搭了OpenCV。过程略痛苦,故将VS2013配置OpenCV2.4.13的经历写下来,希望能帮到大家。

配置前的准备

Visual Studio 2013 OpenCV 2.4.13

配置过程

Step 1 设置环境变量

首先,依次进入控制面板->系统->高级系统设置,选择环境变量,在path中加入C:\Program Files\opencv\build\x86\vc12\bin

环境变量设置
环境变量设置

这里,为了适应不同需求,我把32位和64位的都加入了path.

Step 2 编写配置文件

下面是已经做好的配置文件,急着用的童鞋可以先拿去用~

如果不急着用,我会在下面慢慢讲述怎么用Visual Studio制作配置文件

<?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>C:\Program Files\opencv\build\include\opencv2;C:\Program Files\opencv\build\include\opencv;C:\Program Files\opencv\build\include;$(IncludePath)</IncludePath>
    <LibraryPath Condition="'$(Platform)'=='Win32'">C:\Program Files\opencv\build\x86\vc12\lib;$(LibraryPath)</LibraryPath>
    <LibraryPath Condition="'$(Platform)'=='X64'">C:\Program Files\opencv\build\x64\vc12\lib;$(LibraryPath)</LibraryPath>
  </PropertyGroup>
  <ItemDefinitionGroup>
    <Link Condition="'$(Configuration)'=='Debug'">
       <AdditionalDependencies>opencv_calib3d2413d.lib;opencv_contrib2413d.lib;opencv_core2413d.lib;opencv_features2d2413d.lib;opencv_flann2413d.lib;opencv_gpu2413d.lib;opencv_highgui2413d.lib;opencv_imgproc2413d.lib;opencv_legacy2413d.lib;opencv_ml2413d.lib;opencv_nonfree2413d.lib;opencv_objdetect2413d.lib;opencv_ocl2413d.lib;opencv_photo2413d.lib;opencv_stitching2413d.lib;opencv_superres2413d.lib;opencv_ts2413d.lib;opencv_video2413d.lib;opencv_videostab2413d.lib;%(AdditionalDependencies)</AdditionalDependencies>    
    </Link>
    <Link Condition="'$(Configuration)'=='Release'">
       <AdditionalDependencies>opencv_calib3d2413.lib;opencv_contrib2413.lib;opencv_core2413.lib;opencv_features2d2413.lib;opencv_flann2413.lib;opencv_gpu2413.lib;opencv_highgui2413.lib;opencv_imgproc2413.lib;opencv_legacy2413.lib;opencv_ml2413.lib;opencv_nonfree2413.lib;opencv_objdetect2413.lib;opencv_ocl2413.lib;opencv_photo2413.lib;opencv_stitching2413.lib;opencv_superres2413.lib;opencv_ts2413.lib;opencv_video2413.lib;opencv_videostab2413.lib;%(AdditionalDependencies)</AdditionalDependencies>    
    </Link>
  </ItemDefinitionGroup>
  <ItemGroup />
</Project>

将文件保存为opencv2413.props备用~

Stepp 3 使用配置文件

打开Visual Studio 2013,新建一个c++项目。在视图->其他窗口中打开属性管理器。

属性管理器
属性管理器

然后选中我们的工程项目,右键->添加现有属性表就可以了~

添加属性
添加属性

Step 4 测试opencv是否配置成功

在项目源文件中新建一个c++文件,输入以下代码:

#include<stdio.h>
#include"highgui.h"

int main(int argc, char** argv){
    IplImage *img = cvLoadImage("E:\\2.jpg"); //cvLoadImage()的参数为图片的完整路径名
    cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
    cvShowImage("Example1", img);
    cvWaitKey(0);
    cvReleaseImage(&img);
    cvDestroyWindow("Example1");
    return 0;
}

如果运行产生图片,即为配置成功。

如何生成配置文件

首先,在属性资源管理器添加新项目属性表

Step 1 添加OpenCV文件

选择 [VC++目录],编辑右边的[包含目录], 把

C:\Program Files\opencv\build\include
C:\Program Files\opencv\build\include\open
C:\Program Files\opencv\build\include\open2

这三个目录添加进去。这样用include添加openCV的头文件的时候才不会出错找不到文件。

包含目录
包含目录

Step 2 添加OpenCV库参考文件

编辑右边的 [库目录],把openCV库函数的路径 C:\Program Files\opencv\build\x64\vc12\lib添加进去(注意到Visual Studio 2013需要使用vc12这个目录,而不是vc11,而且对应是64位的操作系统所以选择x64这个目录)。让Visual Studio在编译程序的时候,能够搜索到这个目录以调用openCV的函数 。

库目录
库目录

Step3 在链接器的输入里面添加lib文件

选择左边的 [链接器],再选择 [输入],修改右边的 [附加依赖项] 把C:\Program Files\opencv\build\x64\vc12\lib目录下的所有以*.lib为扩展名的文件名添加进去,以添加lib文件(步骤8是添加库函数的搜索 目录,这一步是添加具体的lib文件。)在这里以d结尾的文件对应debug模式,而不是以d结尾的文件对应release模式。

链接器
链接器
debug模式
opencv_calib3d2413d.lib
opencv_contrib2413d.lib
opencv_core2413d.lib
opencv_features2d2413d.lib
opencv_flann2413d.lib
opencv_gpu2413d.lib
opencv_highgui2413d.lib
opencv_imgproc2413d.lib
opencv_legacy2413d.lib
opencv_ml2413d.lib
opencv_nonfree2413d.lib
opencv_objdetect2413d.lib
opencv_ocl2413d.lib
opencv_photo2413d.lib
opencv_stitching2413d.lib
opencv_superres2413d.lib
opencv_ts2413d.lib
opencv_video2413d.lib
opencv_videostab2413d.lib
release模式
opencv_calib3d2413.lib
opencv_contrib2413.lib
opencv_core2413.lib
opencv_features2d2413.lib
opencv_flann2413.lib
opencv_gpu2413.lib
opencv_highgui2413.lib
opencv_imgproc2413.lib
opencv_legacy2413.lib
opencv_ml2413.lib
opencv_nonfree2413.lib
opencv_objdetect2413.lib
opencv_ocl2413.lib
opencv_photo2413.lib
opencv_stitching2413.lib
opencv_superres2413.lib
opencv_ts2413.lib
opencv_video2413.lib
opencv_videostab2413.lib

点击确定,我们就做好了一个项目属性表~

PS: 我在前面提供的属性表更加“聪明”,他可以根据不同模式和平台选择不同的文件哦~

推荐阅读更多精彩内容