Search code examples
opencvosx-snow-leopardqt4.6

How to integrate opencv2.2 with Qt4.7 in OSX 10.6.6?


I'm trying to integrate the opencv2.2 library with Qt4.7 creator on the mac and running into problems. Most of which I've resolved to some degree. I downloaded the opencv2.2 tar.gz library and untar'ed it to a sub-directory under my home folder compiled it using CMake following this guide to use static libraries. (I unchecked the BUILD_SHARED_LIBRARIES and USE_OMIT_FRAME_POINTER and checked the QT options)

My .pro file now currently looks like:

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

I have now simplified my test to just using:

#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();
}

in main.cpp and I still get the same errors. My compiler output:

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'

Sorry for the long edit


Solution

  • You also need to tell qt creator to link with the opencv libraries:

    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
    

    You only need to supply the libraries that you actually need. The order in which you supply the libraries is important (I am not sure if I got it right).

    If lib2 depends on lib1, you need to do -llib2 -llib1.

    If you still get errors, consider posting the error messages that you can find in Qt Creator under the compiler output tab (CMD-4).

    Edit: Adding

    QT += opengl
    

    to your .pro file should fix that problem. See http://doc.qt.nokia.com/4.7/qtopengl.html for more information