一.下载OpenCV
二.配置系统环境变量:
我们需要将OpenCV的dll文件所在的目录加入Path环境变量,这样OpenCV才能调用,该路径为:
D:\OpenCV2.3.1\build\x86\vc9\bin
由于有些函数需要TBB,所以需要将tbb所在的目录也加入到环境变量Path中。TBB相关的DLL路径为:
D:\OpenCV2.3.1\build\common\tbb\ia32\vc9
知道这些路径后,将其加入到系统的环境变量Path中,Path路径为:
D:\OpenCV2.3.1\build\x86\vc9\bin;D:\OpenCV2.3.1\build\common\tbb\ia32\vc9
三.配置VS2008环境
下面所有路径均在:VS2008菜单中的“工具”--->“选项”--->“项目和解决方案”--->“VC++目录”选项卡中。
将OpenCV下的build文件夹下的opencv和opencv2文件夹路径给添加进来。
将OpenCV下的build/x86/vc9/lib路径给添加进来。
至此,VS2008中的OpenCV环境都配置完毕了,下面就来看一个测试工程:
opencv_calib3d231d.lib; opencv_contrib231d.lib; opencv_core231d.lib; opencv_features2d231d.lib; opencv_flann231d.lib; opencv_gpu231d.lib; opencv_highgui231d.lib; opencv_imgproc231d.lib; opencv_legacy231d.lib; opencv_ml231d.lib; opencv_objdetect231d.lib; opencv_ts231d.lib; opencv_video231d.lib
opencv_calib3d231.lib; opencv_contrib231.lib; opencv_core231.lib; opencv_features2d231.lib; opencv_flann231.lib; opencv_gpu231.lib; opencv_highgui231.lib; opencv_imgproc231.lib; opencv_legacy231.lib; opencv_ml231.lib; opencv_objdetect231.lib; opencv_ts231.lib; opencv_video231.lib(其实就是少了个d,呵呵)
下面就可以编译下面这个OpenCV的测试程序了:
#include "stdafx.h" #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main(int argc, char* argv[]) { const char* imagename = "lena.jpg"; //从文件中读入图像 Mat img = imread(imagename); //如果读入图像失败 if(img.empty()) { fprintf(stderr, "Can not load image %s\n", imagename); return -1; } //显示图像 imshow("image", img); //此函数等待按键,按键盘任意键就返回 waitKey(); return 0; }
如果都配置正确的话,就会出来一张年轻时代的Lena美女图了,呵呵!
(对了,Lena.jpg图片在OpenCV的官网上,自己下吧,呵呵!)
如果想自己编译程序,就去下个CMake开源程序,然后将代码重新编译一下即可,官网上有。