1、准备
下载OPENCV2.4.7,下载完成后解压至对应目录,我这里是解压至D:\opencv
2、添加环境变量
若是win32下编译就选择x64,若是win64下编译则选择x86
3、新建项目
打开VS2012,新建控制台程序test2,勾选空项目
4、新建项
在新项目中的右键点击源文件->添加->新建项,选择C++文件(.cpp),名称改为main,点击添加
5、属性管理器
在菜单栏里面点<视图>--<属性管理器>,在Microsoft.Cpp.Win32.user右键->属性,弹出属性页
右击“Debug|Win32”点击“添加新项目属性表”(如果想在Release模式下使用OpenCV,右击Release|Win32,同理可得)&lt;img src="https://pic4.zhimg.com/50/e83136838687ba83e81db9bbf4ede131_hd.jpg" data-rawwidth="351" data-rawheight="358" class="content_image" width="351"&gt;
给属性表命名
&lt;img src="https://pic3.zhimg.com/50/7aed98411d80eb2c9eb51c42fd21f4e1_hd.jpg" data-rawwidth="937" data-rawheight="582" class="origin_image zh-lightbox-thumb" width="937" data-original="https://pic3.zhimg.com/7aed98411d80eb2c9eb51c42fd21f4e1_r.jpg"&gt;
6、配置项目属性表
双击创建的属性表
&lt;img src="https://pic1.zhimg.com/50/27db5dbe81b73a35f8579b2b70004b68_hd.jpg" data-rawwidth="346" data-rawheight="318" class="content_image" width="346"&gt;
给属性表命名
&lt;img src="https://pic3.zhimg.com/50/7aed98411d80eb2c9eb51c42fd21f4e1_hd.jpg" data-rawwidth="937" data-rawheight="582" class="origin_image zh-lightbox-thumb" width="937" data-original="https://pic3.zhimg.com/7aed98411d80eb2c9eb51c42fd21f4e1_r.jpg"&gt;
6、配置项目属性表
双击创建的属性表
&lt;img src="https://pic1.zhimg.com/50/27db5dbe81b73a35f8579b2b70004b68_hd.jpg" data-rawwidth="346" data-rawheight="318" class="content_image" width="346"&gt;
7、包含目录
然后在属性页中选择通用属性->CV++目录,然后再包含目录中加入
D:\opencv\build\include、
D:\opencv\build\include\opencv、
D:\opencv\build\include\opencv2
8、附加依赖项目
还是在属性页,选择链接器->输入,然后选择附加依赖项目
,在附加依赖项中加入下列库文件,
其中后缀带d的是debug版本,
没带d的是release版本
opencv_ml247d.lib
opencv_calib3d247d.lib
opencv_contrib247d.lib
opencv_core247d.lib
opencv_features2d247d.lib
opencv_flann247d.lib
opencv_gpu247d.lib
opencv_highgui247d.lib
opencv_imgproc247d.lib
opencv_legacy247d.lib
opencv_objdetect247d.lib
opencv_ts247d.lib
opencv_video247d.lib
opencv_nonfree247d.lib
opencv_ocl247d.lib
opencv_photo247d.lib
opencv_stitching247d.lib
opencv_superres247d.lib
opencv_videostab247d.lib
(release下设置)
opencv_ml247.lib
opencv_calib3d247.lib
opencv_contrib247.lib
opencv_core247.lib
opencv_features2d247.lib
opencv_flann247.lib
opencv_gpu247.lib
opencv_highgui247.lib
opencv_imgproc247.lib
opencv_legacy247.lib
opencv_objdetect247.lib
opencv_ts247.lib
opencv_video247.lib
opencv_nonfree247.lib
opencv_ocl247.lib
opencv_photo247.lib
opencv_stitching247.lib
opencv_superres247.lib
opencv_videostab247.lib
9、引用项目属性表
打开项目目录,你会看到如下文件
&lt;img src="https://pic3.zhimg.com/50/fd97db6c52b4ce078b34489c08b79aa5_hd.jpg" data-rawwidth="800" data-rawheight="324" class="origin_image zh-lightbox-thumb" width="800" data-original="https://pic3.zhimg.com/fd97db6c52b4ce078b34489c08b79aa5_r.jpg"&gt;
打开项目目录,你会看到如下文件
&lt;img src="https://pic3.zhimg.com/50/fd97db6c52b4ce078b34489c08b79aa5_hd.jpg" data-rawwidth="800" data-rawheight="324" class="origin_image zh-lightbox-thumb" width="800" data-original="https://pic3.zhimg.com/fd97db6c52b4ce078b34489c08b79aa5_r.jpg"&gt;
以后新建项目时,打开属性管理器,右击Debug|Win32选择添加现有属性表上述文件即可。
10、demo测试
- #include <opencv2/opencv.hpp>
- using namespace cv;
- int main()
- {
- Mat img = imread("../opencv.jpg");
- if (img.empty())
- {
- fprintf(stderr, "Error: load image failed.");
- return -1;
- }
- namedWindow("image", CV_WINDOW_AUTOSIZE);
- imshow("image", img);
- waitKey();
- return 0;
- }