PDA

View Full Version : qimage to iplimage



jack1989
25th March 2010, 14:27
hi i am quite new to Qt, and i am stuck in a problem regarding converting a Qimage to Iplimage in my program,
can anyone help me with this

JohannesMunk
25th March 2010, 15:16
Never used OpenCV / IplImage before but google revealed this:

http://www.c-plusplus.de/forum/viewtopic-var-t-is-209429-and-postdays-is-0-and-postorder-is-asc-and-start-is-10.html



IplImage *img= ...

QImage *image= new QImage(img->width, img->height, QImage::Format_RGB32);

for (int y=0;y<img->height;y++)
{
unsigned int *dst= (unsigned int*)image->scanLine(y);
unsigned char *src= (unsigned char*)img->imageData + y*img->width*3;
for (int x=0;x<img->width;x++,src+=3)
{
*dst++= src[0] | (src[1]<<8) | (src[2]<<16) | (0xff<<24);
}
}

You just have to turn it around and make sure the destination format is right.. And you may need to adapt the pixel stride (src+=3 ) to whatever your source is.. look for uses of scanLine.

Johannes

jack1989
1st April 2010, 15:32
hi here is a code snippet of function i am usin to convert QImage to IplImage, but it gives the error : Invalid parameter passed to C runtime function.
IplImage* ImageViewer::convertToIplImage(QImage *qImage)
{
cvNamedWindow("win1",CV_WINDOW_AUTOSIZE);
IplImage* cvImage;
cvImage = cvCreateImageHeader(cvSize(qImage->width(), qImage->height()), IPL_DEPTH_8U, 4);
cvImage->imageData = (char*)qImage->bits();
IplImage* colorImage = cvCreateImage( cvGetSize(cvImage), 8, 3 );
cvConvertImage( cvImage, colorImage, 0 );
cvShowImage("win1",colorImage);
cvWaitKey(0);

// if(!cvSaveImage("cvImage.bmp", cvImage)) printf("Could not save: cvImage.bmp in qtToCv");

return colorImage;
//return x;

}

can anyone tell me whats going wrong