若改变图像适应label,则最后一行代码的src改为:
src
.
scaled
(
ui
->
label
->
size
())
若改变label适应图像,则在最后添加一行代码:
ui
->
label
->
resize
(
ui
->
label
->
pixmap
()->
size
());
【方法一】QPixmap显示
QString fileName = QFileDialog::getOpenFileName(this, tr("打开图像"), ".", tr("*.jpg *.bmp *.png"));
QPixmap src(fileName);//QPixmap src; src.load(fileName);
ui->label->setPixmap(src);//若改变图像适应label,则 ui->label->setPixmap(src.scaled(ui->label->size()));【方法二】QImage转换为 QPixmap显示
QString fileName = QFileDialog::getOpenFileName(this, tr("打开图像"), ".", tr("*.jpg *.bmp *.png"));
QImage src(fileName);//QImage src; src.load(fileName);
ui->label->setPixmap(QPixmap::fromImage(src));
【方法三】Mat转换QImage再转换QPixmap显示
说明:注意imread读取路径名的格式;Mat类转换为QImage类的格式;
QString fileName = QFileDialog::getOpenFileName(this, tr("打开图像"), ".", tr("*.jpg *.bmp *.png"));
Mat openImage = imread(fileName.toLatin1().data());//imread读取路径名
Mat openImage1;
cvtColor(openImage1, openImage1, CV_BGR2RGB);
QImage src = QImage((const unsigned char*)(openImage1.data), openImage1.cols, openImage1.rows, QImage::Format_RGB888);//Mat类转换为QImage类
ui->label->setPixmap(QPixmap::fromImage(src.scaled(ui->label->size())));
【头文件】
#include <QMainWindow>
#include<QFileDialog>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;