3

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?

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
hamza
  • 2,714
  • 3
  • 19
  • 14

4 Answers4

3

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;
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • i have already found & tested it but somehow it didnt worked for me , the application compiled perfectly but it crashed after the execution , i have located the problem & its on that function since i am working with a Qt console application i have noticed an error message on the consol window , it was something like :OpenCV Error: Bad argument in unknown function, file ..\..\..\..\ocv\opencv\modules\core\src\matrix.cpp, line 641 – hamza Jun 23 '11 at 05:23
  • Before posting it here, I copied that function and tested it and it worked fine. – karlphillip Jun 23 '11 at 16:30
1

Why not just using this:

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

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;
}
karlphillip
  • 92,053
  • 36
  • 243
  • 426
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; }

it worked !! thanks to Fateh benmerzoug

hamza
  • 2,714
  • 3
  • 19
  • 14
  • The problem with this approach is that you are assuming that your images will always have 3 channels. What happens if they have 4 channels? or 1 channel? If you noticed on the website I referenced earlier, there's also a function for this conversion named `QImageToIplImage`. You should check it out. – karlphillip Jun 23 '11 at 16:33
  • 4
    It is converting QPixmap to IplImage not the other way!1 – Sherif May 24 '12 at 02:25
  • @karlphillip: if we have a QPixmap, how can we know the number of channel of this QPixmap? – aviit Nov 20 '15 at 07:59
  • 1
    That is outside the scope of this question, so go ahead and ask a new question if you want to. However, it seems that [QPixmap doc](http://doc.qt.io/qt-5/qpixmap.html) doesn't have a method for that purpose. – karlphillip Nov 20 '15 at 15:46