QImage gray = Mat2QImage(img);//显示模板图
QPixmap imge = QPixmap::fromImage(gray);
QPixmap ima = imge.scaled(ui.label1->width(),ui.label1->height(),Qt::KeepAspectRatio);
ui.label1->setPixmap(ima);
此程序实现了mat图像到qt之间的转化过程,其中用到了Mat2QImage,它的算法也需要在程序中体现,代码如下:
QImage en:: Mat2QImage(cv::Mat mtx)
{
switch (mtx.type())
{
case CV_8UC1:
{
QImage img((const unsigned char *)(mtx.data), mtx.cols, mtx.rows, mtx.cols, QImage::Format_Grayscale8);
return img;
}
break;
case CV_8UC3:
{
QImage img((const unsigned char *)(mtx.data), mtx.cols, mtx.rows, mtx.cols * 3, QImage::Format_RGB888);
return img.rgbSwapped();
}
break;
case CV_8UC4:
{
QImage img((const unsigned char *)(mtx.data), mtx.cols, mtx.rows, mtx.cols * 4, QImage::Format_ARGB32);
return img;
}
break;
default:
{
QImage img;
return img;
}
break;
}
}