本博客为本人学生时代的技术博客,工作后10年已经不再更新,需要关注可以访问geekpm.com,点击访问!

IplImage QImage Mat 格式互转

//QImage->IplImage *
static IplImage *ConvertToIplImage(const QImage &img)
{
    int nChannel=0;
    if(img.format()==QImage::Format_RGB888)nChannel=3;
    if(img.format()==QImage::Format_ARGB32)nChannel=4;
    if( nChannel==0 )return false;
 
    IplImage *iplImg=cvCreateImageHeader( cvSize(img.width(), img.height()),8,nChannel );
    iplImg->imageData=(char*)img.bits();
 
    if(nChannel==3)
        cvConvertImage(iplImg,iplImg,CV_CVTIMG_SWAP_RB);
 
    return iplImg;
}
 
//Mat->QImage
static QImage ConvertToQImage(cv::Mat &mat)
{
    QImage img;
    int nChannel=mat.channels();
    if(nChannel==3)
    {
        cv::cvtColor(mat,mat,CV_BGR2RGB);
        img = QImage((const unsigned char*)mat.data,mat.cols,mat.rows,QImage::Format_RGB888);
    }
    else if(nChannel==4||nChannel==1)
    {
        img = QImage((const unsigned char*)mat.data,mat.cols,mat.rows,QImage::Format_ARGB32);
    }
 
    return img;
}
 
//IplImage *->QImage 
static QImage ConvertToQImage(IplImage *iplImg)
{
    QImage img;
    int nChannel=iplImg->nChannels;
    if(nChannel==3)
    {
        cvConvertImage(iplImg,iplImg,CV_CVTIMG_SWAP_RB);
        img = QImage((const unsigned char*)iplImg->imageData,iplImg->width,iplImg->height,QImage::Format_RGB888);
    }
    else if(nChannel==4||nChannel==1)
    {
        img = QImage((const unsigned char*)iplImg->imageData,iplImg->width,iplImg->height,QImage::Format_ARGB32);
    }
 
    return img;
}
posted @ 2013-03-29 14:07  emouse  阅读(4104)  评论(0编辑  收藏  举报