ui界面
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_ImageProcess.h"
#include <opencv.hpp>
using namespace cv;
class ImageProcess : public QMainWindow
{
Q_OBJECT
public:
ImageProcess(QWidget *parent = nullptr);
~ImageProcess();
private:
Ui::ImageProcessClass ui;
Mat image;
QLabel* label;
QLabel* label_2;
//添加槽函数
private slots:
void on_OpenFig_clicked();
//按格式on_控件名_clicked命名函数,QT会默认完成函数和按钮动作的链接,如果不这样命名的话就去设置信号槽函数
void on_Progress_clicked();
};
#include "ImageProcess.h"
#include <qfiledialog.h>
#include <QScrollArea>
#include <QColorDialog>
#include <QToolBar>
#include <qmenu.h>
#include <qaction.h>
#include <qdebug.h>
#include <qobject.h>
#include <QFontDialog>
#include <qfont.h>
using namespace std;
ImageProcess::ImageProcess(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//设置主窗口的大小
resize(1400, 480 * 2);
// 将工具栏放入窗口
QToolBar* toolBar = new QToolBar();
// 设置默认值
addToolBar(Qt::TopToolBarArea, toolBar);
// 只允许上下停靠
toolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
toolBar->setFloatable(false);
toolBar->setMovable(true);
//在工具栏中添加工具
toolBar->addAction(ui.actionopen);
toolBar->addAction(ui.actionsave);
toolBar->addAction(ui.actionawRect);
toolBar->addAction(ui.actionzoom);
//添加颜色对话框
connect(ui.actioncolor, &QAction::triggered, this, [=]() {
QColor color = QColorDialog::getColor(Qt::white, this);
qDebug() << "color info" << color << color.name();
});
//添加字体对话框
connect(ui.actionfont, &QAction::triggered, this, [=]() {
bool ok;
QFont font = QFontDialog::getFont(&ok, QFont("Times", 12), this);
if (ok) {
// font is set to the font the user selected
}
else {
// the user canceled the dialog; font is set to the initial
// value, in this case Times, 12.
}
});
}
void ImageProcess::on_OpenFig_clicked()
{
QString filename;
filename = QFileDialog::getOpenFileName(this,
tr("Select Image File"),
"",
tr(" *.png *.bmp *.jpg *.tif *.GIF "));
if (filename.isEmpty())
{
return;
}
else
{
string str = filename.toStdString(); // 将filename转变为string类型;
image = imread(str);
//image=imread(fileName.toLatin1().data);
cvtColor(image, image, COLOR_BGR2RGB);
cv::resize(image, image, Size(300*2, 200*2));
QImage img = QImage((const unsigned char*)(image.data), image.cols, image.rows, QImage::Format_RGB888);
label = new QLabel();
label->setPixmap(QPixmap::fromImage(img));
label->resize(QSize(img.width(), img.height()));
ui.scrollArea->setWidget(label);
}
}
void ImageProcess::on_Progress_clicked()
{
flip(image, image, 4);//反转函数 0 上下反转;整数,水平发转;负数,水平垂直均反转
QImage img1 = QImage((const unsigned char*)(image.data), image.cols, image.rows, QImage::Format_RGB888);
label_2 = new QLabel();
label_2->setPixmap(QPixmap::fromImage(img1));
label_2->resize(QSize(img1.width(), img1.height()));
ui.scrollArea_2->setWidget(label_2);
}
ImageProcess::~ImageProcess()
{}
打开和处理图像的按钮的槽函数通过手动连接参考下文