QImage Mat2QImage(const Mat& mat){// 8-bits unsigned, NO. OF CHANNELS=1if(mat.type()==CV_8UC1){
cout<<"1"<<endl;// Set the color table (used to translate colour indexes to qRgb values)QVector<QRgb> colorTable;for (int i=0; i<256; i++)colorTable.push_back(qRgb(i,i,i));
// Copy input Matconst uchar *qImageBuffer = (const uchar*)mat.data;// Create QImage with same dimensions as input MatQImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);img.setColorTable(colorTable);
return img;}
// 8-bits unsigned, NO. OF CHANNELS=3if(mat.type()==CV_8UC3){
cout<<"3"<<endl;// Copy input Matconst uchar *qImageBuffer = (const uchar*)mat.data;// Create QImage with same dimensions as input MatQImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);return img.rgbSwapped();}
else{
qDebug() << "ERROR: Mat could not be converted to QImage.";return QImage();}
}
Mat转换为QImage
最新推荐文章于 2024-06-24 22:45:03 发布