These days,I am working on developing an application for “High Voltage Electron Microscope”.Basically it takes images from “High Voltage Electron Microscope” and do tomographic reconstruction and 3D reconstruction…etc I use QT4 for user interface and and OpenCV for image processing functionalities and OpenGL as core frameworks.
I came up with big obstacle since it utilise many frameworks and have to handle conversion in between many image-data-structures.Eg : QImage in QT4 , IplImage in OpenCV and some custom image data structure for internal algorithms.
I came up with following conversion between QImage to IplImage.Hope it will helpful to someone whose spending hours on google.
IplImage* QImage2IplImage(QImage *qimg)
{IplImage *imgHeader = cvCreateImageHeader( cvSize(qimg->width(), qimg->height()), IPL_DEPTH_8U, 4);
imgHeader->imageData = (char*) qimg->bits();uchar* newdata = (uchar*) malloc(sizeof(uchar) * qimg->byteCount());
memcpy(newdata, qimg->bits(), qimg->byteCount());
imgHeader->imageData = (char*) newdata;
//cvClo
return imgHeader;
}
QImage* IplImage2QImage(IplImage *iplImg)
{
int h = iplImg->height;
int w = iplImg->width;
int channels = iplImg->nChannels;
QImage *qimg = new QImage(w, h, QImage::Format_ARGB32);
char *data = iplImg->imageData;for (int y = 0; y < h; y++, data += iplImg->widthStep)
{
for (int x = 0; x < w; x++)
{
char r, g, b, a = 0;
if (channels == 1)
{
r = data[x * channels];
g = data[x * channels];
b = data[x * channels];
}
else if (channels == 3 || channels == 4)
{
r = data[x * channels + 2];
g = data[x * channels + 1];
b = data[x * channels];
}if (channels == 4)
{
a = data[x * channels + 3];
qimg->setPixel(x, y, qRgba(r, g, b, a));
}
else
{
qimg->setPixel(x, y, qRgb(r, g, b));
}
}
}
return qimg;}
Ohhh .. “Hope it will helpful to someone whose spending hours on google.” .. indeed.
Thank you
By: ST on May 12, 2010
at 5:16 pm
Yes it did… Thank you!
By: Donny on June 30, 2010
at 8:04 pm
It was very helpful, thanks a lot
By: Jonathan on July 23, 2010
at 1:43 pm
I did not not get the correct output. Just help me to get the output as color image.
Thanks
By: Ashok on August 4, 2010
at 11:07 am
hi Ashok, could you tell me what conversion didnt work ?
Is it QImage to IplImage or IplImage to QImage ?
I have only shown here general methods to get an idea how to do the conversion.You might have to extend these for handle various color-channels and bitrates.
By: umanga on August 4, 2010
at 11:55 am
Hi
I am trying to convert QImage to IplImage with depth 8 and channels 3. But i am getting gray image only and also the image is not clear. Is there anyway to get color image. If possible help me with code.
Thanks
By: Ashok on August 5, 2010
at 3:11 am
Im googling how to solve the speed of IplImage to QImage transform because my case is very slow. Can your method achieve 30 fps? The following is mine.
frame = cvQueryFrame(capture);
cvCvtColor(frame,frame,CV_BGR2RGB);
m_i = QImage((unsigned char *)frame->imageDataOrigin,frame->width,frame->height,QImage::Format_RGB888);
By: sz_proxy on October 26, 2010
at 9:58 am
hi,
i think your method is much faster because you directly using a memory buffer.I assign pixel data for each pixel inside two loops,so performance overhead there!
By: umanga on October 26, 2010
at 10:05 am
Thank you, very helpful!
By: Daniel on December 7, 2010
at 10:53 pm
Thank you for this.
By the way, did you meant vice-versa instead of wise-versa?
By: André on January 4, 2011
at 3:58 am
Thank you. It’s very useful.
May I copy your code into my own program?
By: Xelix on March 22, 2011
at 8:25 pm
yeap sure.feel free
By: umanga on March 23, 2011
at 1:48 am
Hey just, FYI, you have a typo in your code for converting from a QImage to an IPLImage: when you initialize imgHeader you are setting the dimensions to be (qimg->width(), qimg->width()). Second parameter should be height.
By: unknown on March 30, 2011
at 7:27 am
Thanks!
By: unknown on May 31, 2011
at 1:43 pm
Excellent job, that has helped me a lot! The only modification I’ve had to make on your code (I only used the Ipl->QImage portion) consisted in inverting the “R” and “B” coordinates.
By: Fabio Ruini on June 22, 2011
at 3:00 pm
glad it helped you.
By: umanga on June 23, 2011
at 2:12 am
Hey , thanks for the code,, this i s really helpful ..
but i have a small problem with this code. Look in my application i am converting a QImage to IplImage and then performing the Face Detection using LibFace , the problem is that some low quality images creates application crash . Do you have any idea.
By: Harshit on June 29, 2011
at 10:36 am
hay harshi ,glad it helped you.Sorry I havent worked with LibFace.But we use this conversion and OpenCV-edge-detection on low quality microscopic images.It works just fine.
By: umanga on June 30, 2011
at 12:46 am
Thanks very much, really useful!
By: Mohammad on September 1, 2011
at 1:09 am
Thanks a lot Umanga! I’m using this for my Final Year Project
By: Praveena Sarathchandra on November 23, 2011
at 3:26 pm
hay Praneena.glad it helped!
By: umanga on November 24, 2011
at 1:43 am
Hi,
I used your IplImage2QImage function to convert the following IplImage:
IplImage *marker_inside=cvCreateImage(cvSize(50,50),IPL_DEPTH_8U,1);
and display it in a QLabel.
QLabel *imgDisplayLabel = new QLabel(“LOADING…”);
imgDisplayLabel->setPixmap(QPixmap::fromImage(IplImage2QImage(marker_inside)));
imgDisplayLabel->setGeometry(QRect(312, 454, 21, 20));
But I’m getting the “error: C2664: ‘QPixmap::fromImage’ : cannot convert parameter 1 from ‘QImage *’ to ‘const QImage &’
Reason: cannot convert from ‘QImage *’ to ‘const QImage’
No constructor could take the source type, or constructor overload resolution was ambiguous”
How to avoid this?
thanks
By: Praveena Sarathchandra on November 24, 2011
at 4:31 am
check fromImage() method signature.It takes a reference of QImage.You are passing a pointer (QImage*). de-reference it before passing – like QPixmap::fromImage(*(IplImage2QImage(marker_inside)));
By: umanga on November 24, 2011
at 5:25 am
Thanks a lot for the instant reply. mainwindow.obj:-1: error: LNK2005: “class QImage * __cdecl IplImage2QImage(struct _IplImage *)” (?IplImage2QImage@@YAPAVQImage@@PAU_IplImage@@@Z) already defined in main.obj I have added “#ifndef” safety to all headers.
By: Praveena Sarathchandra on November 24, 2011
at 6:42 am
make sure you havent defined IplImage2QImage() twice in your source code.It seems you have defined it in both mainwindow.cpp and main.cpp
By: umanga on November 24, 2011
at 9:00 am
Thanks! it helped..
By: Kushal on February 7, 2012
at 6:39 am