PDA

View Full Version : OpenCV conflict with QIMage in Ubuntu



merajc
11th August 2012, 17:42
Hello

I am running a Ubuntu 12.04 installation with the latest Qt SDK. I've compiled OpenCV and imported the libraries into the QtSDK folders.

Qt recognizes the libraries and allows me to build the project if I include the following:


unix: LIBS += -lopencv_core \
-lopencv_highgui \
-lopencv_imgproc \
-lopencv_objdetect \
-lopencv_contrib

The project runs correctly with no errors from the OpenCV until I try to get it to save an image as QImage.

If that wasn't enough, I've also got QML files serving as the user interface, and if I include any "Image" element in them, the program "unexpectedly exits". If I remove the OpenCV code, the program runs correctly with the Image element. If I remove the Image element and re-insert the OpenCV code, the program runs correctly (as long as I do not have QImage anywhere in my code).

I've tried to rebuild OpenCV, and try new Qt projects, but this "conflict" still remains between OpenCV and QImage (and QML Image element).

My colleagues are able to run the project without problems on their Windows 7 machines.

Please advise.

ChrisW67
11th August 2012, 23:02
Thank you for sharing the fact that your program doesn't work. It would help if you shared even the smallest amount of information about how it fails. At the moment all we know is that program fails. We can surmise that your "unexpected exit" problem is your code using an uninitialised or invalid pointer or an uninitialised data structure. Beyond that we could guess all day.

From the knowledge that many programs use both Qt and OpenCV I can be almost certain this is not a collision between QImage and OpenCV. This, for example, works just fine.


#include <QtGui>
#include <QDebug>

#include <opencv/cv.h>
#include <opencv/highgui.h>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);


QImage image("test.jpg");
QLabel label;
label.setPixmap( QPixmap::fromImage(image) );
label.show();

// load an image
IplImage* img = cvLoadImage("test.jpg");
if(!img)
qFatal("Could not load image file\n");
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE | CV_WINDOW_FREERATIO | CV_GUI_NORMAL);
cvMoveWindow("mainWin", 100, 100);
cvShowImage("mainWin", img );

int result = app.exec();

cvDestroyWindow("mainWin");
cvReleaseImage(&img );

return result;
}

merajc
11th August 2012, 23:17
Apologies for not sharing any code, I had wrongly assumed that someone else might have had experienced the problem and thus would know the answer.

The code I have at the moment is confidential, but I will strip it in order to get the bare bones of what's failing and then reply to this thread.

The code that you have given me does indeed work. We use facial detection and recognition that come with OpenCV 2.4.2, and it strangely works with Windows 7.

Thank you for taking your time, I'll return with more information :)

Bullub
16th February 2014, 18:59
Hello, I've just ran into same problem.

Ubuntu 12.04, Qt 4.8, OpenCv 2.4.3, PCL 1.6. Developing all via Qt Creator 2.5.2. All in 64 bits version.

I din't faced this issue until now, due to the fact I was dealing with image files in disk via "cv::imread(filename)" and then transferring its data to QImage -> QPixmap -> QLabel. NO problem with that.

Now, I need to load some images into Qt resource files. I created the proper .qrc via Qt Creator, and copied and registered image files in it. Well, If I try to load images from resources, it doesn't work:

QImage qi = QImage(":images/red.jpg");
if (qi.isNull())
{
QMessageBox::warning(this,"hey","hey");
}

Of course, at beginning I was struggling with resource names for an entire day. Until I decided to start other project, and copypasted the resource file, and the code that loads that QImage. In the test project, It works OK. (same computer, same IDE & Environment, but no OpenCV linking).
Seeing this, I decided to try this piece of code:

QString fn = QFileDialog::getOpenFileName(this,"Load image");
qi = QImage(fn);
if (qi.isNull())
{
QMessageBox::warning(this,"hey","hey");
}

In original code, this doesn't work! fn is loaded with right filename, but "qi = QImage(fn);" fails. In the Test program, this works as expected.

This is a big problem, because I want to set some icons in windows, and some images for several widgets and QSplashScreen. It's not very acceptable to copy image files into generated binaries folders and load them, assuming user will do nothing with them...

I must point again, that this is NOT a issue with Qt resource filename.

Is there some workaround or idea about this?

PD: I tried QPixmap(":images/red.jpg"); but program fails execution.

PD: Some project settings:

ResFiles.qrc:
<RCC>
<qresource prefix="/">
<file>images/red.png</file>
</qresource>
</RCC>

QOP.proj file:
#-------------------------------------------------
#
# Project created by QtCreator 2013-03-09T23:56:16
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = QOP
TEMPLATE = app

SOURCES += main.cpp\
mainwindow.cpp \
[several files more...]

HEADERS += mainwindow.h \
[several files more...]

FORMS += mainwindow.ui \
camwindow.ui \
dialogsetupcam.ui \
dialogtestcam_sr4k.ui \
dialogtestcam_pmdnano.ui \
dialogcommand_pmdnano.ui \
dialogcreatecam.ui

CONFIG += link_pkgconfig
PKGCONFIG += opencv

CONFIG += link_pkgconfig
PKGCONFIG +=

#Do not use this configuration to get PCL, some sublibraries aren't properly accesible, in example Visualizer
#CONFIG += link_pkgconfig
#PKGCONFIG += pcl_io-1.6

#PCL Headers
INCLUDEPATH += /usr/include/pcl-1.6\
/usr/include/flann/\
/usr/include/eigen3/\
/usr/include/openni/
#PCL Libraries
LIBS += -lpcl_common\
-lpcl_features\
-lpcl_filters\
-lpcl_geometry\
-lpcl_io\
-lpcl_kdtree\
-lpcl_keypoints\
-lpcl_octree\
-lpcl_registration\
-lpcl_sample_consensus\
-lpcl_search\
-lpcl_segmentation\
-lpcl_surface\
-lpcl_tracking\
-lpcl_visualization

#VTK Headers
INCLUDEPATH += /usr/include/vtk-5.8
#VTK Libraries
LIBS += -lQVTK\
-lvtkalglib\
-lvtkCharts\
-lvtkCommon\
-lvtkDICOMParser\
-lvtkexoIIc\
-lvtkFiltering\
-lvtkftgl\
-lvtkGenericFiltering\
-lvtkGeovis\
-lvtkGraphics\
-lvtkHybrid\
-lvtkImaging\
-lvtkInfovis\
-lvtkIO\
-lvtkmetaio\
-lvtkParallel\
-lvtkproj4\
-lvtkQtChart\
-lvtkRendering\
-lvtksys\
-lvtkverdict\
-lvtkViews\
-lvtkVolumeRendering\
-lvtkWidgets

RESOURCES += \
ResFiles.qrc

OTHER_FILES += \
images/red.png