qt中QImageQImage和Mat类型的转换

来源:互联网 发布:砍价网站源码 编辑:程序博客网 时间:2024/05/21 10:44
将opencv中的Mat类型转换为qt中的QImage
QImage Mat2QImage(const Mat &mat)
{
    //8-bitsunsigned,NO.OFCHANNELS=1
    if(mat.type()==CV_8UC1)
    {
       //cout<<"1"<<endl;
        //Setthecolortable(usedtotranslatecolourindexestoqRgbvalues)
        QVector<QRgb>colorTable;
        for(int i=0;i<256;i++)
            colorTable.push_back(qRgb(i,i,i));
        //CopyinputMat
        const uchar*qImageBuffer=(const uchar*)mat.data;
        //CreateQImagewithsamedimensionsasinputMat
        QImage img(qImageBuffer,mat.cols,mat.rows,mat.step,QImage::Format_Indexed8);
        img.setColorTable(colorTable);
        return img;
    }
    //8-bitsunsigned,NO.OFCHANNELS=3
    if(mat.type()==CV_8UC3)
    {
       //cout<<"3"<<endl;
        //CopyinputMat
        const uchar*qImageBuffer=(const uchar*)mat.data;
        //CreateQImagewithsamedimensionsasinputMat
        QImage img(qImageBuffer,mat.cols,mat.rows,mat.step,QImage::Format_RGB888);
        return  img.rgbSwapped();
    }
    else
    {
        qDebug()<<"ERROR:MatcouldnotbeconvertedtoQImage.";
        return QImage();
    }

}

调用方式为

 Mat fileSrc = imread( path);
 QImage imagesrc = Mat2QImage(fileSrc);


     

     

     

     

                                             
1 1
原创粉丝点击