Win7+OpenCV 2.4.13+Visual Studio 2015一劳永逸的配置

来自https://www.hsli.top

本文提供一种一劳永逸配置OpenCV的方法,基于Win7+OpenCV 2.4.13+Visual Studio 2015

本文所涉及的所有D:\OpenCV2413\为笔者的OpenCV安装目录,请修改为你自己的安装目录

首先从官网下载OpenCV2.4.13安装包,解压安装到自定义目录

环境变量

修改环境变量,系统属性->高级系统设置->高级->环境变量中系统变量Path添加一条指向OpenCV目录内的D:\OpenCV2413\opencv\build\x86\vc12\bin文件夹

设置属性

新建一个Visual Studio控制台工程,导航到视图->其他窗口->属性管理器,右键点击Debug | Win32 添加新项目属性表,命名为OpenCV_2.4.13_Debug_x86.props保存到自定目录,本文以G:\Visual Studio\OpenCV为例,并修改以下项目:

包含目录

通用属性->VC++目录->包含目录中,添加

1
2
3
D:\OpenCV2413\opencv\build\include
D:\OpenCV2413\opencv\build\include\opencv2
D:\OpenCV2413\opencv\build\include\opencv

注意请导航到你自己的OpenCV安装目录下

库目录

通用属性->VC++目录->库目录中,添加

1
D:\OpenCV2413\opencv\build\x86\vc12\lib

如下图

这里写图片描述

添加附加依赖项

在属性页打开通用属性->链接器->输入->附加依赖项

D:\OpenCV2413\opencv\build\x86\vc12\lib目录中新建一个list.bat批处理文件,输入以下内容:

1
2
3
4
cd %
dir *d.lib >> debug.list
dir *3.lib >> release.list
pause

保存关闭,运行,在目录下即可得到debug.listrelease.list文件,用记事本打开如下:

debug.list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 驱动器 D 中的卷是 Software
卷的序列号是 E48E-92B4

D:\OpenCV2413\opencv\build\x86\vc12\lib 的目录

2016/05/12 19:44 210,502 opencv_calib3d2413d.lib
2016/05/12 19:45 373,270 opencv_contrib2413d.lib
2016/05/12 19:43 473,664 opencv_core2413d.lib
2016/05/12 19:44 332,938 opencv_features2d2413d.lib
2016/05/12 19:43 107,910 opencv_flann2413d.lib
2016/05/12 19:44 457,990 opencv_gpu2413d.lib
2016/05/12 19:43 142,812 opencv_highgui2413d.lib
2016/05/12 19:43 193,196 opencv_imgproc2413d.lib
2016/05/12 19:44 476,570 opencv_legacy2413d.lib
2016/05/12 19:43 240,664 opencv_ml2413d.lib
2016/05/12 19:44 307,260 opencv_nonfree2413d.lib
2016/05/12 19:44 185,948 opencv_objdetect2413d.lib
2016/05/12 19:44 557,966 opencv_ocl2413d.lib
2016/05/12 19:43 91,576 opencv_photo2413d.lib
2016/05/12 19:45 568,938 opencv_stitching2413d.lib
2016/05/12 19:44 371,396 opencv_superres2413d.lib
2016/05/12 19:44 9,614,610 opencv_ts2413d.lib
2016/05/12 19:43 115,816 opencv_video2413d.lib
2016/05/12 19:44 381,590 opencv_videostab2413d.lib
19 个文件 15,204,616 字节
0 个目录 26,290,794,496 可用字节

将其中的库文件名复制出来,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

粘贴到附加依赖项
这里写图片描述

确定保存,至此,Debug的属性配置完毕,不要忘记右键该属性表保存

Release属性表

接下来配置Release属性表

创建Release属性表

在Debug属性表保存的G:\Visual Studio\OpenCV目录中,将OpenCV_2.4.13_Debug_x86.props属性表复制一份,命名为OpenCV_2.4.13_Release_x86.props

修改Release属性

在Visual Studio工程中的属性管理器窗口右键点击Release | Win32添加现有属性表,导航到G:\Visual Studio\OpenCV目录添加Release属性表OpenCV_2.4.13_Release_x86.props,并双击打开修改通用属性->链接器->输入->附加依赖项

打开上文中生成的release.list,复制出release库的文件名,如下:

release库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

粘贴到附加依赖项中,确定保存,右键单击该属性表保存属性表。

添加msvc120d

在VS2015中编译OpenCV 2.4.13程序会报错MSVCR120D.dll和MSVC120P.dll缺失

点击这里下载链接下载msvc120d两个文件,放在D:\OpenCV2413\opencv\build\x86\vc12\bin目录中,再次编译即可解决问题。

注意切换到你自己的OpenCV安装目录下

测试

在工程中输入以下代码,测试运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;

int main()
{
VideoCapture cap(0);

if (!cap.isOpened())
{
cout << "Camera Error!" << endl;
return -1;
}

Mat frame;
namedWindow("Image", CV_WINDOW_AUTOSIZE);

while (1)
{
cap >> frame;
imshow("Image", frame);

if (waitKey(2) == 27)
{
destroyAllWindows();
break;
}
}

return 0;
}

该程序会打开摄像头显示摄像头图像,至此完全配置完毕

新项目使用OpenCV

新项目要使用OpenCV时,只需要在Debug | Win32中添加保存好的Debug属性表即可,要生成Release版本的话在Release | Win32中添加保存好的Release属性表,非常方便。

如果文章有用,请随意打赏