opencv配置详解(含windows和linux两种平台)

含windows下QT4、VC6.0、VS2010和linux下QT4的配置
一、VS2010+opencv2.42配置
备注:本人是在sourceforge上下载的已经编译好的版本,所以解压后配置一下即可,过程比较简单。
1)配置include路径
打开MicrosoftVisual Studio 2010,切换到解决方案视图,右键解决方案,属性,切换到VC++目录,在Include Directories(包含目录)中,加入:
E:\Program Files\opencv\build\include
E:\Program Files\opencv\build\include\opencv
2)配置lib路径
在LibraryDirectories(库目录)中加入:E:\Program Files\opencv\build\x86\vc10\lib
3)设置环境变量
需要将OpenCV的dll文件所在的目录添加到环境变量Path里。
32位系统& VC2010,dll目录为:E:\Program Files\opencv\build\x86\vc10\bin
由于有些函数需要TBB,所以需要将tbb所在的目录也加入到环境变量Path中。TBB相关的DLL路径为:
 E:\Program Files\opencv\build\common\tbb\ia32\vc10
设置完环境后重启电脑,使得环境变量生效
到处已经可以测试一个简单的程序
#include "stdafx.h"
//#include //#include "cv.h"
//#include //#include "cxcore.h"
//#include   //#include "highgui.h"

#include //#include
#include //#include
#include   //#include
//#pragma comment(lib,"cv.lib")
//#pragma comment(lib,"cxcore.lib")
//#pragma comment( lib, "highgui.lib" )
#pragma comment( lib, "opencv_core242d.lib ") 
#pragma comment( lib, "opencv_highgui242d.lib" )

using namespace cv;

int _tmain(int argc, _TCHAR* argv[])
{
    namedWindow( "show_image", WINDOW_AUTOSIZE );//显示窗口
    const char* imagename = "meinv.jpg";//默认源代码文件所在一个目录
 Mat src = imread(imagename);
 //如果读入图像失败  
    if(src.empty()) 
    { 
        fprintf(stderr, "Can not load image %s\n", imagename); 
   getchar();
        return -1; 
    } 
    //显示图像  
    imshow("image", src); 
    //此函数等待按键,按键盘任意键就返回  
    waitKey(); 
    return 0;
}

4)链接库
如上面的测试程序,在程序中加入诸如:
#pragma comment( lib, "opencv_core242d.lib ")
#pragma comment( lib, "opencv_highgui242d.lib" )
#pragma comment( lib, "opencv_calib3d242d.lib" )
#pragma comment( lib, "opencv_contrib242d.lib" )
#pragma comment( lib, "opencv_features2d242d.lib" )
#pragma comment( lib, "opencv_imgproc242d.lib" )
或者:
打开VS2010,建立一个Win32空的控制台程序helloopencv
设置项目属性,在SolutionExplorer的helloopencv上右键选择Properties,在弹出的helloopencvProperty Pages窗口中,在链接器(Link)下的输入(Input),为项目的Debug配置增加附加依赖项(AdditionalDependecies):

opencv_calib3d242d.lib
opencv_contrib242d.lib
opencv_core242d.lib
opencv_features2d242d.lib
opencv_flann242d.lib
opencv_gpu242d.lib
opencv_haartraining_engined.lib
opencv_highgui242d.lib
opencv_imgproc242d.lib
opencv_legacy242d.lib
opencv_ml242d.lib
opencv_objdetect242d.lib
opencv_ts242d.lib
opencv_video242d.lib
(可根据实际需要删减)注意,请打开了新编辑窗口(即点击了“...”按钮)“附加依赖项”,并一条一条分别加入,一条一行(一个回车),(注意行末不要加";")否则会出现类似以下错误:1>LINK: fatal error LNK1104: 无法打开文件“…….lib”
 
同理,为项目的Release选项增加依赖项,操作过程类似,只是在helloopencv最左侧的Configuration中,下拉选项选择“Release”即可。Release中需要增加的依赖项为:
 
opencv_calib3d242.lib; opencv_contrib242.lib; opencv_core242.lib;opencv_features2d242.lib; opencv_flann242.lib; opencv_gpu242.lib;opencv_highgui242.lib; opencv_imgproc242.lib; opencv_legacy242.lib; opencv_ml242.lib;opencv_objdetect242.lib; opencv_ts242.lib; opencv_video242.lib (可根据实际需要删减)*

本篇文章来源于 Linux公社网站(www.linuxidc.com) 
原文链接:http://www.linuxidc.com/Linux/2012-08/68052.htm
二、VC6+opencv1.0配置
1.安装openc
双击opencv1.0.exe,勾选add .. to the systemPATH,检查C:\Program Files\OpenCV\bin是否已经被加入到环境变量PATH,没有则加入。
2.全局设置
菜单Tools->Options->Directories:先设置lib路径,选择Library files,在下方填入路径:
C:\Program Files\OpenCV\lib
然后选择include files,在下方填入路径:
C:\Program Files\OpenCV\cxcore\include
C:\Program Files\OpenCV\cv\include
C:\Program Files\OpenCV\cvaux\include
C:\Program Files\OpenCV\ml\include
C:\Program Files\OpenCV\otherlibs\highgui
C:\Program Files\OpenCV\otherlibs\cvcam\include
然后选择source files,在下方填入路径:
C:\Program Files\OpenCV\cv\src
C:\Program Files\OpenCV\cxcore\src
C:\Program Files\OpenCV\cvaux\src
C:\Program Files\OpenCV\otherlibs\highgui
C:\Program Files\OpenCV\otherlibs\cvcam\src\windows
最后点击“ok”,完成设置。
3.项目设置
每创建一个将要使用OpenCV的VC Project,都需要给它指定需要的lib。菜单:Project->Settings,然后将Setting for选为All Configurations,然后选择右边的link标签,在Object/library modules附加上
cxcore.lib cv.lib ml.lib cvaux.lib highgui.lib cvcam.lib
测试程序C语言版:
/***********************************************************************
 * OpenCV example
 * By Shiqi Yu 2006
 ***********************************************************************/
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"cxcore.lib")
#pragma comment( lib, "highgui.lib" )

#include "stdio.h"
#include "cv.h"
#include "highgui.h"
 
int main( int argc, char** argv )
{
    IplImage* pImg; //声明IplImage指针
 
    //载入图像
    //if( argc == 2 && (pImg = cvLoadImage( argv[1], 1)) != 0 )//[[此处的argc==2是否需要改成argc==1?我改了之后才能运行成功。求大牛解惑]] //  wmzzzz : 在"属性"|"debug"|里的command arguments 里加入参数(一个路径:要打开的文件路径) 这时 argc==2 就合理了...可以试试多加几个
    pImg=cvLoadImage("meinv.jpg", 1);
 if(pImg!=NULL)
 {
        cvNamedWindow( "Image", 1 );//创建窗口
        cvShowImage( "Image", pImg );//显示图像
 
        cvWaitKey(0); //等待按键
 
        cvDestroyWindow( "Image" );//销毁窗口
        cvReleaseImage( &pImg ); //释放图像
        return 0;
    }
   else
    printf("can't find image\n");
    return -1;
}
三、linux下QT+opencv2.3.1
备注:linux下,先从官网上下载opencv的源代码,然后自己编译成库,过程比较繁琐。
开发环境linuxmint12_lxde(3.0.0-12-generic)+QTsdk1.2.1(QT creator2.4.1,基于QT4.7.4)+OpenCV2.3.1
QTSdk直接去http://qt-project.org/下载即可
opencv安装方法:推荐几个网址:
http://www.opencv.org.cn/index.php/Debian
http://www.matrixq.net/2011/10/10320.html
http://www.samontab.com/web/category/image-processing/

1用QtCreator新建一个Qt Console Application的过程此处就省略掉。建立完后在main函数中输入以下代码:
   #include
#include
#include
#include

using namespace cv;

int main(int argc, char *argv[])
{
//    QCoreApplication a(argc, argv);
   
    namedWindow( "src", WINDOW_AUTOSIZE );
    Mat src1 = imread( "lena.jpg" );
    while(1)
    {
        imshow( "src", src1 );
        char c =  waitKey(30);
        if( 27==c )
            return 0;
    }

//    return a.exec();
}

2. 将opencv的头文件和库文件与Qt关联起来,所以在双击QtCreator工程下到***.pro文件,在其后面添加下列代码:
   INCLUDEPATH += /usr/local/include \
                /usr/local/include/opencv \
                /usr/local/include/opencv2

LIBS += /usr/local/lib/libopencv_highgui.so \
        /usr/local/lib/libopencv_core.so    \
        /usr/local/lib/libopencv_imgproc.so
3. 把图片放入调试输出目录,即***-build-desktop-Qt_4_8_2_in_PATH__Qt-4_8_2__Release文件夹中
四 windows下Qt 4.7.4+opencv1.0
(opencv2.42未调试通过)
1.修改空工程,修改pro文件,在末尾加上(注意路径不要含有空格)
INCLUDEPATH +=   D:\OpenCV\cv\include \
  D:\OpenCV\cvaux\include \
  D:\OpenCV\cxcore\include \
  D:\OpenCV\otherlibs\highgui
LIBS +=   D:\OpenCV\lib\cv.lib \
  D:\OpenCV\lib\cvaux.lib \
  D:\OpenCV\lib\cxcore.lib \
  D:\OpenCV\lib\highgui.lib \
  D:\OpenCV\lib\cvcam.lib
 
 2.添加文件main.cpp,代码如下:
#include
#include "cv.h"
#include "highgui.h"

int main( int argc, char** argv )
{
    IplImage* pImg; //声明IplImage指针
    //载入图像
    //if( argc == 2 && (pImg = cvLoadImage( argv[1], 1)) != 0 )//[[此处的argc==2是否需要改成argc==1?我改了之后才能运行成功。求大牛解惑]] //  wmzzzz : 在"属性"|"debug"|里的command arguments 里加入参数(一个路径:要打开的文件路径) 这时 argc==2 就合理了...可以试试多加几个
    pImg=cvLoadImage("meinv.jpg", 1);
    if(pImg!=NULL)
    {
        printf("Load image success\n");
        cvNamedWindow( "Image", 1 );//创建窗口
        cvShowImage( "Image", pImg );//显示图像

        cvWaitKey(0); //等待按键

        cvDestroyWindow( "Image" );//销毁窗口
        cvReleaseImage( &pImg ); //释放图像
        return 0;
    }
   else
       printf("can't find image\n");

    return -1;
}
3.将图片meinv.jpg放入 testOpencv-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK____目录下,编译运行即可
  

 

你可能感兴趣的:(opencv配置详解(含windows和linux两种平台))