QT内label控件通过opencv显示图像

网友投稿 841 2022-09-29

QT内label控件通过opencv显示图像

QT内label控件通过opencv显示图像

1.对pro进行配置,使其能够理解opencv。

INCLUDEPATH+=d:\opencv249\include\opencv\ d:\opencv249\include\opencv2\ d:\opencv249\includeLIBS+=d:\opencv249\lib\libopencv_calib3d249.dll.a\ d:\opencv249\lib\libopencv_contrib249.dll.a\ d:\opencv249\lib\libopencv_core249.dll.a\ d:\opencv249\lib\libopencv_features2d249.dll.a\ d:\opencv249\lib\libopencv_flann249.dll.a\ d:\opencv249\lib\libopencv_gpu249.dll.a\ d:\opencv249\lib\libopencv_highgui249.dll.a\ d:\opencv249\lib\libopencv_imgproc249.dll.a\ d:\opencv249\lib\libopencv_legacy249.dll.a\ d:\opencv249\lib\libopencv_ml249.dll.a\ d:\opencv249\lib\libopencv_objdetect249.dll.a\ d:\opencv249\lib\libopencv_video249.dll.a

2.在*.ui内创建两个命令按钮,一个标签。其界面为:

3.配置其.h文件、.cpp文件。

.h文件:

#ifndef MAINWINDOW_H#define MAINWINDOW_H#include #include #include namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); cv::Mat image; QImage img;private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked();private: Ui::MainWindow *ui;};#endif // MAINWINDOW_H

.cpp文件

#include "mainwindow.h"#include "ui_mainwindow.h"#include "mainwindow.h"#include "ui_mainwindow.h"#include #include #include #include #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this);}MainWindow::~MainWindow(){ delete ui;}void MainWindow::on_pushButton_clicked(){ QString filename = QFileDialog::getOpenFileName(this,tr("Open Image"),"",tr("Image File(*.bmp *.jpg *.jpeg *.png)")); QTextCodec *code = QTextCodec::codecForName("gb18030"); std::string name = code->fromUnicode(filename).data(); image = cv::imread(name); if(!image.data) { QMessageBox msgBox; msgBox.setText(tr("image data is null")); msgBox.exec(); } else { cv::cvtColor(image,image,CV_BGR2RGB); img = QImage((const unsigned char*)(image.data),image.cols,image.rows, image.cols*image.channels(), QImage::Format_RGB888); ui->label->clear(); ui->label->setPixmap(QPixmap::fromImage(img)); //ui->processPushButton->setEnabled(true); ui->label->resize(ui->label->pixmap()->size()); }}void MainWindow::on_pushButton_2_clicked(){ cv::flip(image,image,1); img = QImage((const unsigned char*)(image.data),image.cols,image.rows,image.cols*image.channels(), QImage::Format_RGB888); ui->label->setPixmap(QPixmap::fromImage(img));}

补充:

image.cols*image.channels()

可以改为image.step.

参考资料:

1.使用Qt显示openCV矩阵图片

2. Qt中用QLabel显示OpenCV中Mat图像数据出现扭曲现象的解决    http://lovelittlebean.blog.163.com/blog/static/11658218620125208212189/

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:浅析小程序中textarea层级过高穿透问题的解决方案
下一篇:浅析小程序中怎么引入高德地图(高德微信小程序定位)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~