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