如何在OSX 10.6.6中将opencv2.2与Qt4.7集成?

时间:2011-03-12 18:58:10

标签: opencv osx-snow-leopard qt4.6

我正在尝试将opencv2.2库与Qt4.7创建者集成在mac上并遇到问题。其中大部分我已经在某种程度上解决了。 我下载了opencv2.2 tar.gz库,并将其解压缩到我的主文件夹下的子目录下,使用CMake在this guide之后使用静态库编译它。 (我取消选中了BUILD_SHARED_LIBRARIES和USE_OMIT_FRAME_POINTER并检查了QT选项)

我的.pro文件现在看起来像:

TARGET = openCVTst1
TEMPLATE = app
SOURCES += main.cpp\
        mainwindow.cpp

INCLUDEPATH += /opt/local/include /usr/local/include

LIBS += -L/opt/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_features2d \
        -lopencv_flann -lopencv_gpu -lopencv_imgproc -lopencv_ml \
        -lopencv_objdetect -lopencv_video -lopencv_highgui -lopencv_core

HEADERS  += mainwindow.h
FORMS    += mainwindow.ui

我现在已将我的测试简化为使用:

#include <QtGui/QApplication>
#include "mainwindow.h"
#include <opencv/cv.h>
#include <opencv/highgui.h>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//MainWindow w;
//w.show();
cvNamedWindow( "My Window", 1 );
IplImage *img = cvCreateImage( cvSize( 640, 480 ), IPL_DEPTH_8U, 1 );
CvFont font;
double hScale = 1.0;
double vScale = 1.0;
int lineWidth = 1;
cvInitFont( &font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC,
hScale, vScale, 0, lineWidth );
cvPutText( img, "Hello World!", cvPoint( 200, 400 ), &font,
cvScalar( 255, 255, 0 ) );
cvShowImage( "My Window", img );
cvWaitKey();
return a.exec();
}
在main.cpp中

我仍然得到相同的错误。我的编译器输出:

Running build steps for project openCVTst1...
Configuration unchanged, skipping qmake step.
Starting: "/usr/bin/make" -w
make: Entering directory `/Users/Inprimus/devProjects/openCVTst1-build-desktop'
/usr/bin/qmake -spec /usr/local/Qt4.7/mkspecs/macx-g++ -o Makefile ../openCVTst1/openCVTst1.pro
make: Leaving directory `/Users/Inprimus/devProjects/openCVTst1-build-desktop'
make: Entering directory `/Users/Inprimus/devProjects/openCVTst1-build-desktop'
g++ -headerpad_max_install_names -o openCVTst1.app/Contents/MacOS/openCVTst1 main.o mainwindow.o moc_mainwindow.o   -F/Library/Frameworks -L/Library/Frameworks -L/opt/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_video -lopencv_highgui -lopencv_core -framework QtGui -L/tmp/qt-stuff-6474/source/qt-everywhere-opensource-src-4.7.0/lib -F/tmp/qt-stuff-6474/source/qt-everywhere-opensource-src-4.7.0/lib -framework QtCore 
ld: warning: directory '/tmp/qt-stuff-6474/source/qt-everywhere-opensource-src-4.7.0/lib' following -L not found
ld: warning: directory '/tmp/qt-stuff-6474/source/qt-everywhere-opensource-src-4.7.0/lib' following -F not found
Undefined symbols:
  "QGLFormat::~QGLFormat()", referenced from:
      ViewPort::ViewPort(CvWindow*, int, int)in libopencv_highgui.a(window_QT.o)
      ViewPort::ViewPort(CvWindow*, int, int)in libopencv_highgui.a(window_QT.o)
  "_glPushMatrix", referenced from:
      ViewPort::setGL(int, int)in libopencv_highgui.a(window_QT.o)
      ViewPort::setGL(int, int)in libopencv_highgui.a(window_QT.o)
  "_gzclose", referenced from:
      _cvReleaseFileStorage in libopencv_core.a(persistence.o)
      _cvOpenFileStorage in libopencv_core.a(persistence.o)
      _cvOpenFileStorage in libopencv_core.a(persistence.o)
  "_glMatrixMode", referenced from:
      ViewPort::unsetGL()     in libopencv_highgui.a(window_QT.o)
      ViewPort::setGL(int, int)in libopencv_highgui.a(window_QT.o)
      ViewPort::setGL(int, int)in libopencv_highgui.a(window_QT.o)
      ViewPort::paintEvent(QPaintEvent*)       in libopencv_highgui.a(window_QT.o)
  "_glShadeModel", referenced from:
      ViewPort::initGL()     in libopencv_highgui.a(window_QT.o)
      ViewPort::ViewPort(CvWindow*, int, int)in libopencv_highgui.a(window_QT.o)
  "_gzrewind", referenced from:
      icvRewind(CvFileStorage*)      in libopencv_core.a(persistence.o)
  "_glEnable", referenced from:
      ViewPort::initGL()     in libopencv_highgui.a(window_QT.o)
      ViewPort::initGL()     in libopencv_highgui.a(window_QT.o)
      ViewPort::initGL()     in libopencv_highgui.a(window_QT.o)
      ViewPort::ViewPort(CvWindow*, int, int)in libopencv_highgui.a(window_QT.o)
      ViewPort::ViewPort(CvWindow*, int, int)in libopencv_highgui.a(window_QT.o)
      ViewPort::ViewPort(CvWindow*, int, int)in libopencv_highgui.a(window_QT.o)
  "QGLWidget::QGLWidget(QGLFormat const&, QWidget*, QGLWidget const*, QFlags<Qt::WindowType>)", referenced from:
      ViewPort::ViewPort(CvWindow*, int, int)in libopencv_highgui.a(window_QT.o)
  "QGLFormat::QGLFormat(QFlags<QGL::FormatOption>, int)", referenced from:
      ViewPort::ViewPort(CvWindow*, int, int)in libopencv_highgui.a(window_QT.o)
  "_glHint", referenced from:
      ViewPort::initGL()     in libopencv_highgui.a(window_QT.o)
      ViewPort::ViewPort(CvWindow*, int, int)in libopencv_highgui.a(window_QT.o)
  "_gzgets", referenced from:
      icvGets(CvFileStorage*, char*, int)in libopencv_core.a(persistence.o)
  "_glPopMatrix", referenced from:
      ViewPort::unsetGL()     in libopencv_highgui.a(window_QT.o)
      ViewPort::unsetGL()     in libopencv_highgui.a(window_QT.o)
      ViewPort::paintEvent(QPaintEvent*)       in libopencv_highgui.a(window_QT.o)
      ViewPort::paintEvent(QPaintEvent*)       in libopencv_highgui.a(window_QT.o)
  "_glFrustum", referenced from:
      ViewPort::icvgluPerspective(double, double, double, double)in libopencv_highgui.a(window_QT.o)
      ViewPort::setGL(int, int)in libopencv_highgui.a(window_QT.o)
  "_gzeof", referenced from:
      icvEof(CvFileStorage*)      in libopencv_core.a(persistence.o)
  "_glLoadIdentity", referenced from:
      ViewPort::setGL(int, int)in libopencv_highgui.a(window_QT.o)
      ViewPort::setGL(int, int)in libopencv_highgui.a(window_QT.o)
  "QGLWidget::grabFrameBuffer(bool)", referenced from:
      ViewPort::saveView()     in libopencv_highgui.a(window_QT.o)
  "_gzopen", referenced from:
      _cvOpenFileStorage in libopencv_core.a(persistence.o)
  "_gzputs", referenced from:
      icvPuts(CvFileStorage*, char const*)in libopencv_core.a(persistence.o)
make: Leaving directory `/Users/Inprimus/devProjects/openCVTst1-build-desktop'
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [openCVTst1.app/Contents/MacOS/openCVTst1] Error 1
The process "/usr/bin/make" exited with code %2.
Error while building project openCVTst1 (target: Desktop)
When executing build step 'Make'

很抱歉长时间编辑

2 个答案:

答案 0 :(得分:2)

您还需要告诉qt creator链接opencv库:

LIBS += -L/opt/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_video -lopencv_highgui -lopencv_core

您只需提供实际需要的库。您提供库的顺序很重要(我不确定我是否正确)。

如果lib2取决于lib1,则需要执行-llib2 -llib1

如果仍然出现错误,请考虑在Qt Creator标签(CMD-4)下发布compiler output中可以找到的错误消息。

修改 添加

QT += opengl

到你的.pro文件应该解决这个问题。有关详细信息,请参阅http://doc.qt.nokia.com/4.7/qtopengl.html

答案 1 :(得分:1)

您是否关注this guide - 抱歉不是mac用户