dusktreader
dusktreader

Reputation: 4055

Why can't I link against opencv and qt when handling jpeg images?

Loading a QImage object from a jpeg file fails when the application is linked against OpenCV 2.3 or 2.4. The program segfaults when I attempt to create a QImage from a jpeg file

QImage( "some-jpeg.jpg" )

This is only true if the application is linked against opencv's highgui library.

I'm building on Ubuntu 12.04 with OpenCV 2.4 and Qt 4.8.1

Does anyone know why this is happening and how I can work around this? My research project uses OpenCV's image processing code and the frontend depends on Qt.

I've put a bug report in to Qt (https://bugreports.qt-project.org/browse/QTBUG-27032). I tried at OpenCV, but their registration project is borked or something, because it accepted registration info, but then denies attempts to log in...

qt-cv-jpg-test.pro

QT += core gui

TARGET = qt-cv-jpg-test
TEMPLATE = app

INCLUDEPATH += /usr/local/include
LIBS += -L/usr/local/lib -lopencv_highgui -lopencv_core

SOURCES += main.cpp

main.cpp

#include <QImage>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"

int main(int argc, char *argv[])
{
    QString fileName = "some-jpeg.jpg";
    cv::Mat cvImage = cv::imread( fileName.toStdString() );
    QImage qtImage( fileName );
    return 0;
}

The program segfaults, but here is the stack:

0   jpeg_CreateDecompress   /usr/local/lib/libopencv_highgui.so.2.4 0   0x7ffff7ae55fc  
1   ??  /usr/lib/x86_64-linux-gnu/qt4/plugins/imageformats/libqjpeg.so  0   0x7ffff0549ee7  
2   ??  /usr/lib/x86_64-linux-gnu/qt4/plugins/imageformats/libqjpeg.so  0   0x7ffff054a18f  
3   QImageReader::read(QImage*) /usr/lib/x86_64-linux-gnu/libQtGui.so.4 0   0x7ffff6c3f0c8  
4   QImageReader::read()    /usr/lib/x86_64-linux-gnu/libQtGui.so.4 0   0x7ffff6c3f6f7  
5   QImage::load(QString const&, char const*)   /usr/lib/x86_64-linux-gnu/libQtGui.so.4 0   0x7ffff6c348dc  
6   QImage::QImage(QString const&, char const*) /usr/lib/x86_64-linux-gnu/libQtGui.so.4 0   0x7ffff6c34a75  
7   main    main.cpp    9   0x400d10

I don't really know why an opencv method is showing up at the bottom of the stack. I built opencv telling it to use the libjpeg on the file system. I have both libjepg 6.2 and libjpeg8 installed on the system.

Upvotes: 1

Views: 1570

Answers (1)

nightsparc
nightsparc

Reputation: 140

I got the same error - and we found the problem yesterday evening.

I assume you had build OpenCV with the BUILD_JPEG or the OPENCV_BUILD_3RDPARTY_LIBS flag. Furthermore, OpenCV is installed in /usr/local/lib. In this case, OpenCV is built with its own libjpeg, which is statically linked into the opencv_highgui library.

So, when you're creating a QImage and you have linked the opencv_highgui to your project/application, the linker will search for the jpeg_CreateDecompress function (the function is internally used by Qt), which it first will find in the opencv_highgui lib. And there should be some incompatibility between the version which is expected by Qt (in Ubuntu 12.04 its the libjpeg8) and OpenCV's shipped one (libjpeg62).

So, there exist an easy solution ;-) - build OpenCV without it's shipped libjpeg (BUILD_JPEG=OFF) and use the systems shared library.

Upvotes: 4

Related Questions