QImage和IplImage转换总结

来源:互联网 发布:疯狂的程序员 pdf 编辑:程序博客网 时间:2024/05/22 06:49

在arm中做图像处理,因为不支持GTK,一般都会用到QT来实现显示功能,所以不可避免的要涉及到QImage和IplImage两种图像格式之间的转换,下面总结一下转换的方法。

(下面格式转换的代码都是网友写的,我这里只是归纳修改了一下)

IplImage 转换为 QImage

方法1 

QImage *IplImageToQImage(const IplImage *img){    QImage *qmg;    cvCvtColor(img,img,CV_BGR2RGB);    qmg = new QImage((unsigned char*)img->imageData,img->width,img->height,img->widthStep,QImage::Format_RGB888);    return qmg;}

这里要注意的是,IplImage每个像素点存储的格式为BGR,而QImage每个像素点存储的格式为RGB,所以在转换之前需要先使用opencv提供的cvCvtColor函数将BGR图像变为RGB图像。还有就是QImage最后一个参数QImage::Format_RGB888,这是指色彩空间R,G,B三个通道分别用8位来表示,如果是灰度图,则参数为QImage::Format_Indexed8。

例如:

QImage *IplImageToQImage(const IplImage *img){    QImage *qmg;    IplImage *img_gray = cvCreateImage(cvGetSize(img),8,1);    cvCvtColor(img,img_gray,CV_BGR2GRAY);    qmg = new QImage((unsigned char*)img->imageData,img->width,img->height,img->widthStep,QImage::Format_Indexed8);    return qmg;}

方法2

QImage *IplImageToQImage(*img){    QImage *qmg = new QImage (img->width,img-height,img->widthStep,QImage::Format_RGB32);    for (int i=0; i<height; ++i)    {        for (int j=0; j<width; ++j)        {           int r,g,b;           if( 3 == img->nChannels )           {                b = (int)CV_IMAGE_ELEM( img, uchar, i, j*3+0 );        g = (int)CV_IMAGE_ELEM( img, uchar, i, j*3+1 );        r = (int)CV_IMAGE_ELEM( img, uchar, i, j*3+2 );            }    else if( 1 == img->nChannels )    {        b = (int)CV_IMAGE_ELEM( img, uchar, i, j*3+0 );g = b;        r = b;             }               qmg->setPixel( j, i, qRgb(r,g,b) );         }    }    return qmg;}

这里要注意的是qmg->setPixel( j, i, qRgb(r,g,b) );括号里面是(j,i);而不是(i,j)。我还没弄明白为什么这里要调换一下i,j的顺序,希望知道的朋友指点一下。

QImage转换为IplImage

方法1

IplImage *QImageToIplImage(const QImage * qImage){    int width = qImage->width();    int height = qImage->height();    CvSize Size;    Size.height = height;    Size.width = width;    IplImage *IplImageBuffer = 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(IplImageBuffer, y, x, CV_RGB(qRed(rgb), qGreen(rgb), qBlue(rgb)));        }    }    return IplImageBuffer;}

方法2

IplImage *QImageToIplImage(const QImage * qImage){    int width = qImage->width();    int height = qImage->height();    CvSize Size;    Size.height = height;    Size.width = width;    IplImage *IplImageBuffer = 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);            CV_IMAGE_ELEM( IplImageBuffer, uchar, y, x*3+0 ) = qBlue(rgb);    CV_IMAGE_ELEM( IplImageBuffer, uchar, y, x*3+1 ) = qGreen(rgb);    CV_IMAGE_ELEM( IplImageBuffer, uchar, y, x*3+2 ) = qRed(rgb);}      }     return IplImageBuffer;}

这两种方法其实都是一样的,只是表达形式有点不同,还有就是一个是生成RGB格式图像,一个是生成BGR格式的图像。









0 0
原创粉丝点击