开发者

Convert IplImage to Qpixmap

How开发者_开发知识库 do we convert IplImage to a QPixmap or QImage?

If the only answer is save the Iplimage then load it to the QPixmap, then how do we do that?


I just found an interesting piece of code here, which provides a function to convert from IplImage* to QImage*. Search for a function namedIplImageToQImage().

To use that function you could do:

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;


Saving it to a file and then using QImage to retrieve it is a way to do it:

// 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;
}


Why not just using this:

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; }

it worked !! thanks to Fateh benmerzoug

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜