官方文档并注释:
These instructions were tested with Visual C 9.0 (as part of Visual Studio 2008 Professional). These instructions should work without modification with Visual Studio 2005 and 2003. Modifications may be necessary for Visual Studio Express editions. These instructions do not apply to Visual Studio 2010 which uses different settings (see VisualC ).
Install OpenCV
Download the OpenCV 2.1.0 Windows installer from SourceForge - "OpenCV-2.1.0-win32-vs2008.exe".
Install it to a folder (without any spaces in it), say "C:OpenCV2.1". This article will refer to this path as $openCVDir
During installation, enable the option "Add OpenCV to the system PATH for all users".
由于有些函数需要TBB,所以需要将tbb所在的目录也加入到环境变量Path中如C:TBBbinia32vc9 加入后可能需要注销当前Windows用户(或重启)后重新登陆才生效
Configure Visual Studio
Open VC Directories configuration: Tools > Options > Projects and Solutions > VC Directories
Choose "Show directories for: Include files"
Add "$openCVDirincludeopencv"
Choose "Show directories for: Library files"
Add "$openCVDirlib"
如果使用了DirectShow, Add "../DirectShow/Lib"(看具体情况)
Choose "Show directories for: Source files"
Add "$openCVDirsrccv"
Add "$openCVDirsrccvaux"
Add "$openCVDirsrccxcore"
Add "$openCVDirsrchighgui"
Add "$openCVDirsrcml"
Configure your Project
After you've created a project you'll need to add the OpenCV dependencies.(debug 和release 都添加)
Open Project Properties: Project > %projectName% Properties...
Open Linker Input properties: Configuration Properties > Linker > Input
Open the "..." window to edit "Additional Dependencies" and on each line put:
"cv210.lib"
"cxcore210.lib"
"highgui210.lib"
“ml210.lib”
“cvaux210.lib”
And any other lib file necessary for your project
注意,请打开了新编辑窗口(即点击了“...”按钮)“附加依赖项”,并一条一条分别加入,一条一行(一个回车),(注意行末不要加";")否则会出现类似以下错误:1>LINK : fatal error LNK1104: 无法打开文件“…….lib”
Your project should now build. If you get any errors try restarting Visual Studio and then doing a clean Rebuild.
如果使用了DirectShow or DirectX, 在vs的解决方案资源管理器中右键相应的项目,选择属性,在属性窗口的 配置属性 -> C/C -> 常规 中的 附加包含目录,点击其右边的小按钮,在弹出的附加包含目录中,确保条目 $(WindowsSdkDir)include 和 $(FrameworkSDKDir)include 在任何dx目录如..DirectShowInclude的前面。如果要在全局配置添加头文件目录,也要遵循这个原则
用下面的程序测试:
// OpenCV_Helloworld.cpp : Defines the entry point for the console application.
// Created for build/install tutorial, Microsoft Visual C 2008 Express and OpenCV //2.1.0
代码语言:cpp复制#include "stdafx.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
int _tmain(int argc, _TCHAR *argv[])
{
IplImage *img = cvLoadImage("e.jpg");
cvNamedWindow("Image:", 1);
cvShowImage("Image:", img);
cvWaitKey();
cvDestroyWindow("Image:");
cvReleaseImage(&img);
return 0;
}
//双击exe的话,e.jpg图片放在.exe(先bulid)同一文件夹内;按F5直接运行程序的话,
//e.jpg要放在opencvopencv里
参考:OPencvChina官网