OpenCV 2.4.7与Qt 4.8.1 QImage发生冲突

时间:2014-02-21 12:00:37

标签: c++ qt opencv qimage

Ubuntu 12.04,Qt 4.8,OpenCv 2.4.3,PCL 1.6。通过Qt Creator 2.5.2开发全部。全部采用64位版本。

直到现在我还没有遇到这个问题,因为我通过“cv :: imread(filename)”处理磁盘中的图像文件,然后将其数据传输到QImage - > QPixmap - > QLabel。没问题。

现在,我需要将一些图像加载到Qt资源文件中。我通过Qt Creator创建了正确的.qrc,并在其中复制并注册了图像文件。好吧,如果我尝试从资源加载图像,它不起作用:

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

当然,一开始我在资源名称上苦苦挣扎了一整天。直到我决定启动其他项目,并复制资源文件和加载QImage的代码。在测试项目中,它工作正常。 (同一台计算机,相同的IDE和环境,但没有OpenCV链接)。 看到这个,我决定尝试这段代码:

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

在原始代码中,这不起作用! fn加载了正确的文件名,但是“qi = QImage(fn);”失败。在测试程序中,这可以按预期工作。

这是一个大问题,因为我想在Windows中设置一些图标,并为几个小部件和QSplashScreen设置一些图像。将图像文件复制到生成的二进制文件夹并加载它们是不太可接受的,假设用户不对它们做任何事情......

我必须再次指出,这不是Qt资源文件名的问题。

是否有一些解决方法或想法?

PD:我试过QPixmap(“:images / red.jpg”);但程序执行失败。

PD:一些项目设置:

ResFiles.qrc:

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

QOP.proj文件:

#-------------------------------------------------
#
# 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

我认为一种解决方法是使用一些QFile类加载这些图像并将其“转移”到QImage。可以这样做吗?

1 个答案:

答案 0 :(得分:0)

我用这种方式解决了这个问题:

//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!!