VS2015+QT5.8+opencv4.1 读取图片并显示

1. 在UI界面上放2个pushButton和2个label

2. 在QtGuiApplication1.h文件中添加槽相关代码:

VS2015+QT5.8+opencv4.1 读取图片并显示

QtGuiApplication1.cpp中添加槽函数。

VS2015+QT5.8+opencv4.1 读取图片并显示

3.添加信号和自定义槽

1)点击编辑信号与槽按钮进入编辑模式

VS2015+QT5.8+opencv4.1 读取图片并显示

2)在信号/槽编辑器中点击+进行添加信号和槽,在槽中先选择close()

VS2015+QT5.8+opencv4.1 读取图片并显示

3)然后回到ui界面,拖动槽到合适的位置,双击close(),如下图所示出现配置连接

VS2015+QT5.8+opencv4.1 读取图片并显示

点击编辑按钮,在槽中添加自定义的槽函数,点击OK

VS2015+QT5.8+opencv4.1 读取图片并显示

 

4)在UI界面中的文件下点击保存

4. 点击XXX.ui,右键选择编译,会在ui_seedingImageProcess.h中生成相应的代码

修改界面,需要重新编译。

 

Qt中显示图像:

Mat BGR 转化为RGB再转化为QImage格式,再用QPixmap对象获得图像,然后在Qlabel中显示。

Mat srcImage, srcImage1, rgbImage;

QImage dstImage;

QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), ".", tr("Image Files(*.jpg *.png *.jpeg *.bmp)"));

string str = fileName.toStdString();

if (fileName.isEmpty())

{

       QMessageBox::information(this, "警告", "没有选择文件");

       return;

}

srcImage = imread(str);  //读取图片

cvtColor(srcImage, rgbImage, CV_BGR2RGB); //图像格式转换

dstImage = QImage((const unsigned char*)(rgbImage.data), rgbImage.cols, rgbImage.rows, QImage::Format_RGB888);

ui. label->setPixmap(QPixmap::fromImage(dstImage));//显示