说明:操作系统ubuntu 12.04 desktop LTS
1.前期准备
采用OpenCV2.3.1。官方下载地址
http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.3.1/OpenCV-2.3.1a.tar.bz2/download
一定先装依赖的包要不然写openCV程序的时候虽然编译能通过,可能运行时出错。
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libswscale-dev libjasper-dev
sudo apt-get install libtiff4-dev
安装cmake
sudo apt-get install cmake
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的automake。OpenCV使用cmake进行makefile的创建。
安装qtcreator
sudo apt-get install qtcreator
2.安装
将OpenCV包解压到用户目录下,得到opencv文件夹
cd ~/opencv
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
(注意“..”前有空格)
make
等待大概十分钟make完成
sudo make install
至此opencv库安装到了/usr/local/lib目录下,重启电脑,或者执行sudo ldconfig
(关于ldconfig:ubuntu把在开机时所有库文件装入到缓存,这样避免每次使用链接库都需装载,提高系统的效率,ldconfig命令就是重新装载,可通过ldconfig -v查看已经在缓存里的库。)
3.QT4+OpenCV组合配置示例
下面就可以使用QT和OpenCV进行开发了。
打开QTcreator新建项目,添加头文件,源文件及其代码(如下,示例项目在附件里也有)
//opencv_example.h
#ifndefOPENCV_EXAMPLE_H
#define OPENCV_EXAMPLE_H
#include <QtGui/QMainWindow>
#include <QtGui>
#include "cv.h"
#include "highgui.h"
class OpenCV_Example : public QMainWindow
{
Q_OBJECT public: OpenCV_Example();
QPushButton *b1;
IplImage *img;
public slots: void slot_imgshow();
};
#endif // OPENCV_EXAMPLE_H
///opencv_example.cpp
#include "opencv_example.h"
OpenCV_Example::OpenCV_Example()
{
setGeometry(100,100,100,100);
b1 = new QPushButton("Show Image",this);
connect(b1,SIGNAL(clicked()), this, SLOT(slot_imgshow()));
}
void OpenCV_Example::slot_imgshow()
{
QMessageBox::information(this,"title","Hello this is popup");
img = cvLoadImage("/home/guo/OpenCV_Example/OpenCV_Example/21.jpg",1);
//要打开的图片 //create a new window
cvNamedWindow("Window", 1); //display the image in the window
cvShowImage("Window", img); //wait for key to close the window
cvWaitKey(0);
cvDestroyWindow( "Window" ); //destroy the window
}
在main中编写:
#include #include <QtGui/QApplication>
#include "opencv_example.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
OpenCV_Example w;
w.show();
return a.exec();
}
在.pro文件中添加编译/链接选项
INCLUDEPATH+= /usr/local/include
INCLUDEPATH+=/usr/local/include/opencv
LIBS+=/usr/local/lib/libopencv_core.so
LIBS+= /usr/local/lib/libopencv_highgui.so
LIBS+= /usr/local/lib/libopencv_imgproc.so
然后构建运行。