在同学的帮助下,终于成功配置了vs+opencv,将详细过程记录在此,方便以后查阅
安装vs2012
下载vs2012,官方下载地址
- 双击安装文件,设置安装路径
2.选择需要的安装包
3.安装进行中
4.终于安装完成
5.点击启动
提示输入产品秘钥
YKCW6-BPFPF-BT8C9-7DCTH-QXGWC
激活成功
6.出现其它配置页面
这里选择C++为默认开发环境
7.启动
8.注意,有时可能出现兼容性问题,需要下载更新包
点击安装即可
安装opencv
1.下载opencv源码
opencv官网地址:http://opencv.org/,网速有点小慢
点击Download,在弹出的新页面中选择需要下载的opencv版本,这里选择2.4.9
然后,开始下载
大概349MB
2.解压源码
解压路径任意
解压密码
3.为opencv配置环境变量
(1)计算机右键“属性”
(2)高级系统设置
(3)点击环境变量
(4)在名称为PATH的环境变量中添加opencv的bin文件夹路径,具体地:
①32位系统,添加:D:\File Program\opencv\build\x86\vc11\bin
②64位系统,添加:
D:\File Program\opencv\build\x86\vc11\bin (对应于32位系统)
D:\File Program\opencv\build\x64\vc11\bin (对应于64位系统)
目的是为了可以在编译器Win32和X64中来回切换
注1:D:\File Program\opencv为opencv的解压路径
注2:vc11表示编译环境为Microsoft Visual Studio 2012;
vs2012中配置opencv
1.打开vs2012
2.新建win32控制台应用程序
3.在解决方案资源管理器处:源文件->添加->新建项
4.在属性管理器处进行配置
(在属性管理器中进行一次配置,就相当于进行了通用的配置过程,以后新建的工程就不用再额外的进行重新配置了)
(1)视图->属性管理器
(2)弹出了属性管理器
(3)配置Debug Win32
注:这里讨论的是Debug,Release的配置完全类似
在Debug | Win32处的Microsoft.Cpp.Win32.user右键“属性”
弹出Microsoft.Cpp.Win32.user属性页
(4)开始配置Microsoft.Cpp.Win32.user属性页
① 添加包含目录,从而,C++程序可以include opencv
具体地:在弹出的属性页中,找到”VC++目录“,在对应的右侧包含目录中,输入opencv的build->include目录
可以看到,opencv->build->include目录下有两个文件夹,应该将该大目录和两个文件夹目录都加入到include目录下
即将如下三个目录都添加到包含目录中
D:\File Program\opencv\build\include
D:\File Program\opencv\build\include\opencv
D:\File Program\opencv\build\include\opencv2
②添加库目录
仍旧在刚刚的VC++目录右侧,在库目录中添加opencv->build->x86->vc11->lib路径
即:D:\File Program\opencv\build\x86\vc11\lib
注意:这里都是用x64还是x86取决与编译器(而不是操作系统!)
如下图所示,编译器为Win32,那么,就应该选择x86目录下的库
另外:
vc10 = Visual Studio 2010
vc11 = Visual Studio 2012
vc12 = Visual Studio 2013
③添加链接库(即dll)
在“Microsoft.Cpp.Win32.user属性页”的链接器->输入的右侧找到附加依赖项
添加如下dll名称(2.4.9版本-Debug对应的opencv中的动态链接库)
opencv_ml249d.lib
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_objdetect249d.lib
opencv_ts249d.lib
opencv_video249d.lib
opencv_nonfree249d.lib
opencv_ocl249d.lib
opencv_photo249d.lib
opencv_stitching249d.lib
opencv_superres249d.lib
opencv_videostab249d.lib
注:如果配置的是Release | Win32,那么,需要添加的是如下19个不带d的release版
opencv_objdetect249.lib
opencv_ts249.lib
opencv_video249.lib
opencv_nonfree249.lib
opencv_ocl249.lib
opencv_photo249.lib
opencv_stitching249.lib
opencv_superres249.lib
opencv_videostab249.lib
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
另外,对于opencv2.4.8
Debug版
opencv_ml248d.lib
opencv_calib3d248d.lib
opencv_contrib248d.lib
opencv_core248d.lib
opencv_features2d248d.lib
opencv_flann248d.lib
opencv_gpu248d.lib
opencv_highgui248d.lib
opencv_imgproc248d.lib
opencv_legacy248d.lib
opencv_objdetect248d.lib
opencv_ts248d.lib
opencv_video248d.lib
opencv_nonfree248d.lib
opencv_ocl248d.lib
opencv_photo248d.lib
opencv_stitching248d.lib
opencv_superres248d.lib
opencv_videostab248d.lib
Release版
opencv_objdetect248.lib
opencv_ts248.lib
opencv_video248.lib
opencv_nonfree248.lib
opencv_ocl248.lib
opencv_photo248.lib
opencv_stitching248.lib
opencv_superres248.lib
opencv_videostab248.lib
opencv_calib3d248.lib
opencv_contrib248.lib
opencv_core248.lib
opencv_features2d248.lib
opencv_flann248.lib
opencv_gpu248.lib
opencv_highgui248.lib
opencv_imgproc248.lib
opencv_legacy248.lib
opencv_ml248.lib
测试opencv是否配置成功
新建空白win32控制台应用程序
在项目中添加一个cpp,在cpp中输入如下代码
注:一定要保证项目路径下由pic图像!
或者是直接给出图像的绝对路径(这样可以避免路径错误)
#include<iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main()
{
// 读入一张图片(游戏原画)
Mat img=imread("pic.jpg");
// 创建一个名为 "游戏原画"窗口
cvNamedWindow("游戏原画");
// 在窗口中显示游戏原画
imshow("游戏原画",img);
// 等待6000 ms后窗口自动关闭
waitKey(6000);
}
(1)编译
成功,没有编译错误
(2)运行程序,成功显示图像
注1:如果路径下没有要操作的图像,会出现如下错误