简体   繁体   English

OpenCV 2.4.7与Qt 4.8.1 QImage冲突

[英]OpenCV 2.4.7 conflict with Qt 4.8.1 QImage

Ubuntu 12.04, Qt 4.8, OpenCv 2.4.3, PCL 1.6. Ubuntu 12.04,Qt 4.8,OpenCv 2.4.3,PCL 1.6 Developing all via Qt Creator 2.5.2. 通过Qt Creator 2.5.2进行全部开发。 All in 64 bits version. 全部为64位版本。

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. 直到现在我都没有遇到这个问题,因为我通过“ cv :: imread(filename)”处理磁盘中的图像文件,然后将其数据传输到QImage-> QPixmap-> QLabel。 NO problem with that. 没问题。

Now, I need to load some images into Qt resource files. 现在,我需要将一些图像加载到Qt资源文件中。 I created the proper .qrc via Qt Creator, and copied and registered image files in it. 我通过Qt Creator创建了正确的.qrc,并在其中复制和注册了图像文件。 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. 直到我决定开始其他项目,并复制粘贴了资源文件和加载该QImage的代码。 In the test project, It works OK. 在测试项目中,它工作正常。 (same computer, same IDE & Environment, but no OpenCV linking). (同一台计算机,相同的IDE和环境,但没有OpenCV链接)。 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);" fn已加载正确的文件名,但“ 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. 这是一个大问题,因为我想在窗口中设置一些图标,并为几个小部件和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. 我必须再次指出,这不是Qt资源文件名的问题。

Is there some workaround or idea about this? 有一些解决方法或想法吗?

PD: I tried QPixmap(":images/red.jpg"); PD:我尝试过QPixmap(“:images / red.jpg”); but program fails execution. 但是程序执行失败。

PD: Some project settings: PD:一些项目设置:

ResFiles.qrc: ResFiles.qrc:

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

QOP.proj file: 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

A workaround I've thought is to load these images with some QFile class and "transfer" it to QImage. 我想到的一种解决方法是使用某些QFile类加载这些图像并将其“传输”到QImage。 Can this be done? 能做到吗?

I've solved this question this way: 我已经通过以下方式解决了这个问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM