Search code examples
qtopencvresourcesimread

Qt Creator : Read an image from resources


I try to read image from resource.

Qt : 5.9.1 and OpenCV : 3.2

My code is :

// Set initial screen
QImage image;
cv::Mat src = cv::imread(":/images/initialScreen.png");
image = ImageFormat::Mat2QImage(src);
videoFrameLabel->setPixmap(QPixmap::fromImage(image));

And my files :

enter image description here

My .pro file :

QT       += core gui
QT += widgets
QT += gui
CONFIG += c++14
QT += multimedia multimediawidgets
...

SOURCES += \
        main.cpp \
        mainwindow.cpp \
    imageformat.cpp

HEADERS += \
        mainwindow.h \
    imageformat.h \
    blob.h

FORMS += \
        mainwindow.ui

INCLUDEPATH += C:\opencv\build\include
LIBS += C:\opencv-build\bin\libopencv_video320.dll
...
LIBS += C:\opencv-build\bin\libopencv_imgcodecs320.dll


DISTFILES +=

RESOURCES += \
    rsc.qrc

It does not work, where do i make mistake? Thanks !


Solution

  • Only Qt classes that use QFile can read from resources stored using the Qt resource system, from the docs:

    The resource system is based on tight cooperation between qmake, rcc (Qt's resource compiler), and QFile.

    Therefore, You have to read the data from the resource file using QFile, then supply the read data to cv::imdecode to get a cv::Mat.