将IplImage转换为Qpixmap

时间:2011-06-23 01:06:58

标签: c++ qt opencv qimage qpixmap

我们如何将IplImage转换为QPixmapQImage

如果唯一的答案是保存Iplimage,然后将其加载到QPixmap,那么我们该如何做?

4 个答案:

答案 0 :(得分:2)

我刚刚发现了一段有趣的代码here,它提供了从IplImage*转换为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;

答案 1 :(得分:1)

将其保存到文件然后使用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;
}

答案 2 :(得分:1)

为什么不直接使用它:

QImage qt_img = ( QImage ( cv_img->dataIm, cv_img->width, cv_img->height, cv_img->QImage::Format_RGB888 ) ).rgbSwapped();

答案 3 :(得分:0)

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