嗯,这是有道理的,因为IplImage2QImage() 既不是 Qt 也不是 OpenCV 的一部分。
您可能在 Internet 上的某个地方看到此函数被使用并复制/粘贴到您的代码中。
通过谷歌简单搜索,我找到了这个函数的the implementation:
static QImage IplImage2QImage(const IplImage *iplImage)
{
int height = iplImage->height;
int width = iplImage->width;
if (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 3)
{
const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
QImage img(qImageBuffer, width, height, QImage::Format_RGB888);
return img.rgbSwapped();
} else if (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 1){
const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
QImage img(qImageBuffer, width, height, QImage::Format_Indexed8);
QVector<QRgb> colorTable;
for (int i = 0; i < 256; i++){
colorTable.push_back(qRgb(i, i, i));
}
img.setColorTable(colorTable);
return img;
}else{
qWarning() << "Image cannot be converted.";
return QImage();
}
}
希望你会知道如何处理它。
我写了这个最小的例子来展示如何成功使用IplImage2QImage()。它使用cvLoadImage() 从磁盘加载名为 test.jpg 的文件,然后将其显示在 QLabel 上。这很简单,而且很有效!
#include <cv.h>
#include <highgui.h>
#include <iostream>
#include <QtGui>
#include <QImage>
static QImage IplImage2QImage(const IplImage *iplImage)
{
int height = iplImage->height;
int width = iplImage->width;
if (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 3)
{
const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
QImage img(qImageBuffer, width, height, QImage::Format_RGB888);
return img.rgbSwapped();
}
else if (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 1)
{
const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
QImage img(qImageBuffer, width, height, QImage::Format_Indexed8);
QVector<QRgb> colorTable;
for (int i = 0; i < 256; i++)
{
colorTable.push_back(qRgb(i, i, i));
}
img.setColorTable(colorTable);
return img;
}
else
{
std::cout << "Image cannot be converted.";
return QImage();
}
}
int main(int argc, char** argv)
{
QApplication app(argc, argv);
IplImage* img = cvLoadImage("test.jpg", 1);
if (!img)
{
std::cout << "Failed to load test.jpg";
return -1;
}
QImage qt_img = IplImage2QImage(img);
QLabel label;
label.setPixmap(QPixmap::fromImage(qt_img));
label.show();
return app.exec();
}
在我的 Linux 机器上,我使用以下代码编译它:
g++ qimage.cpp -o qimage -I/usr/local/include/opencv -I/usr/local/include -I/opt/qt_47x/include -I/opt/qt_47x/include/QtGui -L/usr/local/lib -L/opt/qt_47x/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lQtCore -lQtGui