在VS2008下使用OpenCV的方法 ——附图说明非常详细

来源:互联网 发布:阿里云oss使用 当网盘 编辑:程序博客网 时间:2024/06/05 00:28

在VS2008下使用OpenCV的方法
2009-04-05 12:43
【转】http://hi.baidu.com/guo_1017/blog/item/4126c9d22af047d7a8ec9a81.html

安装所需要的软件

[编辑]

安装VC++ 2008 Express

Visual C++ Express是微软推出的一款免费集成开发环境,如果你没有足够资金购买Visual C++,你可以使用Visual C++ Express。本安装说明撰写时,最新的版本是Visual C++ 2008 Express.

Visual C++ 2008 Express可以从微软网站下载安装(http://www.microsoft.com/express/product/default.aspx)。

[编辑]

安装OpenCV

从 http://www.opencv.org.cn/index.php/Download 下载OpenCV 2.0,并安装,本文档假定安装目录为:D:/Program Files/OpenCV2.0

[编辑]

安装CMake

从 http://www.cmake.org/cmake/resources/software.html 下载 Windows (Win32 Installer) 安装。

[编辑]

编译OpenCV

[编辑]

用CMake导出VC++项目文件

  • 运行cmake-gui,设置路径为OpenCV安装路径(本文档假定安装位置为:D:/Program Files/OpenCV2.0),并创建子目录D:/Program Files/OpenCV2.0/vc2008,用于存放编译结果。
  • 然后点 configure,在弹出的对话框内选择 Visual Studio 9 2008。
  • 如果是VC++2008的Express版本,则不支持OpenMP,所以需要取消ENABLE_OPENMP选项。VC++ 2008(不是Express版本)支持OpenMP,如果你使用VC++2008,强烈建议不要取消这个选项。
点击看大图
Enlarge
点击看大图
点击看大图
Enlarge
点击看大图
点击看大图
Enlarge
点击看大图
[编辑]

编译 OpenCV Debug和Release版本库

完成上一步骤后,将在D:/Program Files/OpenCV2.0/vc2008目录下生成OpenCV.sln的VC Solution File,请用VC++ 2008 Express打开OpenCV.sln,然后执行如下操作:

  • 在Debug下,选择Solution Explorer里的 Solution OpenCV,点右键,运行"Rebuild Solution";如编译无错误,再选择INSTALL项目,运行"Build"。
  • 在Release下,选择Solution Explorer里的 Solution OpenCV,点右键,运行"Rebuild Solution";如编译无错误,再选择INSTALL项目,运行"Build"。

此时,OpenCV的*d.dll文件(for debug)和*.dll文件(for release)将出现在D:/Program Files/OpenCV2.0/vc2008/bin目录中;OpenCV的*d.lib文件(for debug)和*.lib文件(for release)将出现在D:/Program Files/OpenCV2.0/vc2008/lib目录;头文件*.h出现在D:/Program Files/OpenCV2.0/vc2008/include/opencv中。

可以被VC++ 2008 Express调用的OpenCV动态库生成完毕。

点击看大图
Enlarge
点击看大图
点击看大图
Enlarge
点击看大图
点击看大图
Enlarge
点击看大图
点击看大图
Enlarge
点击看大图
点击看大图
Enlarge
点击看大图
 
[编辑]

配置Windows环境变量Path

将D:/Program Files/OpenCV2.0/vc2008/bin加入Windows系统环境变量Path中。加入后可能需要注销当前Windows用户(或重启)后重新登陆才生效。

点击看大图
Enlarge
点击看大图
点击看大图
Enlarge
点击看大图
[编辑]

为VC++ 2008 Express配置OpenCV环境

打开VC++ 2008 Express,菜单 Tools -> Options -> Projects and Solutions -> VC++ Directories

  • Show directories for选择include files,加入目录 D:/Program Files/OpenCV2.0/vc2008/include/opencv
  • Show directories for选择library files,加入目录 D:/Program Files/OpenCV2.0/vc2008/lib

关闭VC++ 2008 Express。

点击看大图
Enlarge
点击看大图
点击看大图
Enlarge
点击看大图
[编辑]

使用OpenCV 2.0编程

  • 打开VC++ 2008 Express,创建一个Win32控制台程序opencvhello;
  • 选择Solution Explorer里的opencvhello项目,点击鼠标右键,选择Properties;
  • 为项目的Debug配置增加依赖的库:cxcore200d.lib cv200d.lib highgui200d.lib
  • 为项目的Release配置增加依赖的库:cxcore200.lib cv200.lib highgui200.lib
  • 编译运行下面的例程(需要将lena.jpg文件放在项目目录下)。
/***********************************************************************
* OpenCV 2.0 测试例程
* 于仕琪 提供
***********************************************************************/


#include "stdafx.h"
#include "highgui.h"

//所有的以新风格命名的函数都在 cv 命名空间中
//如果希望不要每次都输入 cv:: ,则可使用下面语句
//using namespace cv;

int _tmain(int argc, _TCHAR* argv[])
{

const char* imagename = "lena.jpg";

cv::Mat img = cv::imread(imagename); // Matlab风格的 cvLoadImage 函数的另一种调用
if(img.empty())
{
fprintf(stderr, "Can not load image %s/n", imagename);
return -1;
}

if( !img.data ) // 检查是否正确载入图像
return -1;

cv::namedWindow("image", CV_WINDOW_AUTOSIZE); //创建窗口
cv::imshow("image", img); //显示图像

cv::waitKey();

return 0;
}
点击看大图
Enlarge
点击看大图
点击看大图
Enlarge
点击看大图
点击看大图
Enlarge
点击看大图
点击看大图
Enlarge
点击看大图
点击看大图
Enlarge
点击看大图
点击看大图
Enlarge
点击看大图