我们如何转换IplImage
为QPixmap
or QImage
?
如果唯一的答案是保存Iplimage
然后将其加载到QPixmap
,那么我们该怎么做呢?
我刚刚在这里找到了一段有趣的代码,它提供了一个从IplImage*
to转换的函数QImage*
。搜索名为 的函数IplImageToQImage()
。
要使用该功能,您可以执行以下操作:
IplImage* cv_img = cvLoadImage("filename.jpg", CV_LOAD_IMAGE_UNCHANGED);
if(!cv_img)
{
std::cout << "ERROR: cvLoadImage failed" << std::endl;
exit(0);
}
uchar* data = NULL;
QImage* qt_img = IplImageToQImage(cv_img, &data, 0.0, 0.0);
if(!qt_img)
{
std::cout << "ERROR: IplImageToQImage failed" << std::endl;
exit(0);
}
qt_img->save("qimage_output.jpg");
delete qt_img;
将其保存到文件中,然后使用 QImage 检索它是一种方法:
// On my system this code can be compiled with:
// g++ qimage_test.cpp -o qimage_test -I/usr/include/qt4 -lQtGui `pkg-config --cflags --libs opencv`
#include <qt4/QtGui/qpainter.h>
#include <highgui.h>
#include <cv.h>
#include <iostream>
int main()
{
IplImage* cv_img = cvLoadImage("coins.jpg", CV_LOAD_IMAGE_UNCHANGED);
if(!cv_img)
{
std::cout << "ERROR: cvLoadImage failed" << std::endl;
return -1;
}
// Process cv_img and then save it on a file on the disk
if (!cvSaveImage("cv_out.jpg", cv_img))
{
std::cout << "ERROR: cvSaveImage failed" << std::endl;
return -1;
}
// Loading OpenCV saved image into QImage
QImage::QImage qt_img("cv_out.jpg");
// Then finally display it, or do something with it.
// Saving it to the disk for testing purposes
qt_img.save("qt_img.jpg");
return 0;
}
为什么不直接使用这个:
QImage qt_img = ( QImage ( cv_img->dataIm, cv_img->width, cv_img->height, cv_img->QImage::Format_RGB888 ) ).rgbSwapped();
IplImage * ImageToIplImage(QPixmap * qPix){
int width = (qPix->toImage()).width();
int height =(qPix->toImage()).height();
// Creates a iplImage with 3 channels
IplImage *img = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
for(int y=0;y<height;y++)
{
for(int x=0;x<width;x++ )
{
QRgb color = qPix->toImage().pixel(x,y);
cvSet2D(img,y,x,cvScalar(qBlue(color),qGreen(color),qRed(color),1));
}
}
return img; }
有效 !!感谢 Fateh benmerzoug