0. 写在前面
用于记录一下,在Qt上显示Mat的数据,记录代码如下;
//QLabel中显示处理结果
void MainWindow::matToLabelShow(QLabel *label, Mat &mat)
{
cv::Mat Rgb;
QImage Img;
if (mat.channels() == 3)//RGB Img
{
cv::cvtColor(mat, Rgb,COLOR_BGR2RGB);
Img = QImage((const uchar*)(Rgb.data), Rgb.cols, Rgb.rows, Rgb.cols * Rgb.channels(), QImage::Format_RGB888);
}
else//Gray Img
{
Img = QImage((const uchar*)(mat.data), mat.cols, mat.rows, mat.cols * mat.channels(), QImage::Format_Indexed8);
}
//自适应显示
int ori_width = Img.size().width();
int ori_height = Img.size().height();
int m_width = label->size().width();
int m_height = label->size().height();
int pro;
if(ori_width/m_width >= ori_height / m_height)
{
pro = ori_width / m_width;
}
else
{
pro = ori_height / m_height;
}
int scale_width = ori_width / pro;
int scale_height = ori_height / pro;
QImage *scale_image = new QImage();
*scale_image = Img.scaled(scale_width,scale_height,Qt::KeepAspectRatio);
//label->setScaledContents(true);//充满整个屏幕
label->setAlignment(Qt::AlignCenter);
label->setPixmap(QPixmap::fromImage(*scale_image));
}