[Solved with workaround] OpenCV 2.4.7 conflict with Qt 4.8.1 QImage



  • I have started long time ago, a project with Qt4. 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've just discovered a conflict between QImage and OpenCV.

    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@

    A workaround I've thought is to load these images with some QFile class and "transfer" it to QImage. Can this be done?


  • Lifetime Qt Champion

    Hi,

    Aren't you just missing a / ?

    @QFile redImageFile(":/images/red.png");@

    or in your case:

    @QImage redImage(":/images/red.png");@



  • No. Any of both cases won't work for me.

    Note the QFileDialog::getOpenFileName code portion... In there, filename or filepath misspelling is not possible, and QImage continues failing.


  • Lifetime Qt Champion

    Is the QImage null ? What format is it ?

    Also can you show the QLabel related code ?



  • I finally solved this question with this workaround:

    @//This WON'T WORK due to problems caused by presence of OpenCV library; it interferes with normal QImage loading from QResources
    /*
    QImage logoImage(":/images/Logo.png");
    */
    //Read file rawly into RAM (workaround to OpenCV/QImage issue)
    bool isImageInRAM = false;
    QFile file(":/images/Logo.png"); //Apparently there's no difference between ":/images/QOPLogo.png" and ":images/Logo.png"
    QByteArray rawImageFileInRAM;
    if (file.open(QIODevice::ReadOnly))
    {
    rawImageFileInRAM = file.readAll();
    file.close();
    isImageInRAM = true;
    }
    else
    checkFailed(0,QObject::tr("Error loading image from resource: %1").arg(QObject::tr("Logo"))); //checkFailed is a custom method that shows message errors

    //This WON'T WORK due to problems caused by presence of OpenCV library; it interferes with normal QImage loading from memory (yes, too U_U)
    /*
    QImage logoImage = QImage::fromData(rawImageFileInRAM);
    /
    std::vector<uchar> buffInRAM(rawImageFileInRAM.data(),rawImageFileInRAM.data()+rawImageFileInRAM.size());
    cv::Mat imgMatFromRAM = cv::imdecode(cv::Mat(buffInRAM),CV_LOAD_IMAGE_COLOR);
    cv::Mat cvRGBLogoImage(imgMatFromRAM.rows,imgMatFromRAM.cols,CV_8UC3);
    cv::cvtColor(imgMatFromRAM,cvRGBLogoImage,CV_BGR2RGB); //Convert BGR->RGB
    QImage logoImage((const unsigned char
    )cvRGBLogoImage.data,cvRGBLogoImage.cols,cvRGBLogoImage.rows,cvRGBLogoImage.step,QImage::Format_RGB888);

    //At this point logoImage already has image from resources ¡Yeaahhhh!!@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.