如何在Eclipse下配置opencv

如题所述

1、假设已经安装了MinGW,安装目录:C:/MinGW,将C:/MinGW/bin添加到系统环境变量中。如果闲下载安装MinGW麻烦,可以直接下载一个Dev-CPP或许Code::Blocks开发环境,这两个IDE中都是自带MinGW的。
2、下载eclipse-cpp-helios-SR2-win32.zip
3、安装opencv,假设安装目录为:C:/OpenCV
4、解压eclipse-cpp-helios-SR2-win32.zip,启动eclipse.exe
新建C++项目->可执行程序->Hello World C++ Project
5、添加头文件和库文件
右键项目选择“属性”->C/C++ Build->Settings。
Tool Settings 标签页,GCC C++ Compiler->Includes中添加OpenCV的头文件目录,MinGW C++ Linker->Libraries中添加OpenCV的库文件目录以及相应的库文件名称(注意:这里的库文件不加后缀名)

6、配置完成以后,可以使用下面代码进行测试:

////////////////////////////////////////////////////////////////////////
//
// hello-world.cpp
//
// 该程序从文件中读入一幅图像,将之反色,然后显示出来.
//
////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

int main(int argc, char *argv[])
{
IplImage* img = 0;
int height,width,step,channels;
uchar *data;
int i,j,k;

if(argc<2){
printf("Usage: main <image-file-name>/n/7");
exit(0);
}

// load an image
img=cvLoadImage(argv[1]);
if(!img){
printf("Could not load image file: %s/n",argv[1]);
exit(0);
}

// get the image data
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
data = (uchar *)img->imageData;
printf("Processing a %d*%d image with %d channels/n",height,width,channels);

// create a window
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
cvMoveWindow("mainWin", 100, 100);

// invert the image
// 相当于 cvNot(image);
for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)
data[i*step+j*channels+k]=255-data[i*step+j*channels+k];

// show the image
cvShowImage("mainWin", img );

// wait for a key
cvWaitKey(0);

// release the image
//cvReleaseImage(&img );
return 0;
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答