qt中QImageQImage和Mat类型的转换

本文介绍了如何在Qt环境中将QImage类型的数据转换为OpenCV的Mat类型,以及反过来将Mat转换为QImage,这对于在Qt界面中显示OpenCV处理的图像至关重要。详细阐述了转换过程并提供了实际操作代码示例。
将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);


     

     

     

     



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值