1、新建 Qt Application;
2、点击.ui,插入两个按钮 Push Buttons到Form中去,修改其属性,一个名字为OpenImage,一个为Process,右键按钮选择connect signal为其添加响应函数,选择clicked()。对应的cpp文件多出了on_OpenImage_clicked函数
3、qtshowimage.h
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
class qtshowimage : public QMainWindow
{
Q_OBJECT
public:
qtshowimage(QWidget *parent = 0, Qt::WindowFlags flags = 0);
~qtshowimage();
private:
Ui::qtshowimageClass ui;
cv::Mat image;//cv图片
private slots:
void on_OpenImage_clicked();
void on_Process_clicked();
};
#endif // QTSHOWIMAGE_H
4、qtshowimage.cpp
#include "qtshowimage.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <QString>
#include <QLabel>
using namespace std;
using namespace cv;
qtshowimage::qtshowimage(QWidget *parent , Qt::WindowFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
}
qtshowimage::~qtshowimage()
{
}
void qtshowimage::on_OpenImage_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), ".", tr("Image Files (*.png *.jpg *.jpeg *.bmp)"));
image = cv::imread(fileName.toLatin1().data());
cv::namedWindow("Original Image");
cv::imshow("Original Image", image);
}
void qtshowimage::on_Process_clicked()
{
cv::cvtColor(image, image, CV_BGR2RGBA);
QImage img = QImage((const unsigned char*)(image.data), image.cols, image.rows, QImage::Format_RGB32);
QLabel *label = new QLabel(this);
label->move(200, 50);
label->setPixmap(QPixmap::fromImage(img));
label->resize(label->pixmap()->size());
label->show();
}
5、点击Openimage 加载图像
6、点击Process 处理后图像
7、还可参考:https://blog.csdn.net/yang_xian521/article/details/6968012
转载于:https://www.cnblogs.com/rjjhyj/p/9682200.html