将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);

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

被折叠的 条评论
为什么被折叠?



