将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);
转载请注明原文地址: https://ju.6miu.com/read-668446.html