您的位置:首页 > 其它

IplImage与QImage之间相互转换的问题

2013-10-14 14:14 288 查看
做Qt这么久了,一直想把IplImage与QImage之间相互转换的问题贴出来,一直没有时间,今天抽空把代码贴出来,思路很简单,只需要把数据根据两种不同格式进行复制就行了。

IplImage转QImage:

QImage* IplImageToQImage(const IplImage *pIplImage)
{
QImage *qImage;

int w = pIplImage->width;
int h = pIplImage->height;

qImage = new QImage(w, h, QImage::Format_RGB32);

int x, y;
for(x = 0; x < pIplImage->width; ++x)
{
for(y = 0; y < pIplImage->height; ++y)
{
CvScalar color = cvGet2D(pIplImage, y, x);

int r = color.val[2];
int g = color.val[1];
int b = color.val[0];

qImage->setPixel(x, y, qRgb(r,g,b));
}
}

return qImage;
}


QImage转IplImage:

IplImage* QImageToIplImage(const QImage *qImage)
{
int width = qImage->width();
int height = qImage->height();
CvSize size;//(width,height);
size.width = width;
size.height = height;
IplImage *iplImage = cvCreateImage(size, IPL_DEPTH_8U, 3);
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
QRgb rgb = qImage->pixel(x, y);
cvSet2D(iplImage, y, x, CV_RGB(qRed(rgb), qGreen(rgb), qBlue(rgb)));
}
}
return iplImage;
}


完了百度了一下,发现老早已经有人把完整的代码贴出来了(QImage转IplImage),第一种IplImage转QImage貌似更为简单,但代码有问题,编译无法通过,但是方法很好。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: