2014-02-21 70 views
0

的Ubuntu 12.04,QT 4.8,OpenCV的2.4.3,PCL 1.6。通過Qt Creator 2.5.2開發全部。全部位於64位版本。的OpenCV 2.4.7使用Qt衝突4.8.1的QImage

由於我通過「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( 「:圖像/ 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。這可以做到嗎?

回答

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