0. 记录Qt开发中的一些问题
本篇博文主要是记录opencv中的Mat数据转成QImage数据。
1. 函数代码
//QLabel中显示处理结果
void MainWindow::matToLabelShow(Mat mat)
{
QLabel* label = ui->LB_ShowLabel;
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));
delete scale_image;
}