c语言 C/C++联合QT实现图像处理---直接上2027行源码 - width()
文章目录
前言一、程序演示1.主界面2.菜单栏3.图像处理界面
二、功能实现三、使用效果四、源代码展现总结
前言
C/C++联合QT的编译环境在主流环境下相当流行,它方便且强大的库函数,能够胜任许多的界面搭建,包括在嵌入式开发中搭建用户使用界面。本文章的代码展现了如何设计一款图像处理的软件(Cute_Image)。(使用代码时请确保你已经搭建了QT编译环境)
一、程序演示
1.主界面
2.菜单栏
3.图像处理界面
二、功能实现
工具栏从左至右依次是: 1.保存到本地 2.打开文件 3.亮度对比度调整 4.一键复原 5.尺寸大小调整 6.图像旋转 7.镜像调整 8.饱和度调整 9.色温调整 10.模糊处理 11.褪色处理 12.色调处理 13.颗粒度处理
三、使用效果
原图如下 效果1如下 效果2如下 效果3如下 效果4如下 希望大家也可以用我的代码制作出自己的氛围感图片。
四、源代码展现
有点长
在这里插入代码片
#include//包含Qt常用头文件QtWidgets
#include//包含多线程头文件
#define cn QString::fromLocal8Bit
class MyGraphicsScene: public QGraphicsScene
{
public:
MyGraphicsScene(QWidget *parent=0):QGraphicsScene(parent) {}//虽然没有函数内容,但一定要此构造函数,用于设定父窗体
void setShowImage(QImage* imagePtr)
{
showImage=imagePtr;
}
private:
QImage *showImage=nullptr; //定义QImage对象指针showImage,在场景上作为背景被描画显示。初始值是空指针。
//画背景的虚函数,界面需要刷新时自动执行。重写此虚函数用于将showImage作为背景显示
virtual void drawBackground (QPainter *painter, const QRectF &rect)
{
if(showImage==nullptr||(*showImage).isNull()) return; //如果是空指针或者空图像,返回。
painter->setRenderHints(QPainter::SmoothPixmapTransform, true);//消锯齿,设定为false清晰显示每个像素,有利于读取或编辑像素值。
painter->drawImage(0, 0, *showImage);//将showImage作为背景描画在场景上。
}
};
//MyGraphicsView自定义类继承自QGraphicsView,作为图像的显示类。
class MyGraphicsView: public QGraphicsView
{
public:
MyGraphicsView(QWidget *parent = 0): QGraphicsView(parent)//构造函数,指定父窗体,默认为0即没有父窗体
{
this->setDragMode(QGraphicsView::ScrollHandDrag); //设置此模式,当场景大于视图时,可按住鼠标左键移动鼠标来移动场景
//this->setAlignment(Qt::AlignLeft | Qt::AlignTop);//这里是左上角方式显示
}
private:
//鼠标滚轮滚动信号的处理函数(虚函数),鼠标滚轮滚动时自动执行。
virtual void wheelEvent(QWheelEvent *event)
{
QPoint angleDelta = event->angleDelta(); //从事件对象event中获得鼠标滚动角度信息。
qreal scaleFactor = this->transform().m11(); //QGraphicsView视图的观测矩阵里m11表示横向放大倍数,m22表示纵向放大倍数。
//angleDelta的y方向大于0,表示鼠标滚轮向上滚动,需要放大图片,反之缩小。
//scale函数实现的是视图的变焦观测功能。scale(1.1,1.1)表示在现有放大倍数的基础上横纵向继续放大1.1倍。
if (angleDelta.y() > 0 && scaleFactor < 20) this->scale(1.1, 1.1); //如果鼠标滚轮向上滚动,并且放大倍数scaleFactor没有超过20,则再继续放大1.1倍
if (angleDelta.y() < 0 && scaleFactor > 0.02) this->scale(0.9, 0.9); //如果鼠标滚轮向下滚动,并且放大倍数scaleFactor没有低于0.05,则再继续缩小10%
}
};
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//亮度对比度对话框
class MyDialog : public QDialog
{
public:
MyDialog(QImage *rawImage, QWidget *parent = 0); //构造函数
QImage getNewImage()
{
return newImage;
}
private:
MyGraphicsView *view = new MyGraphicsView;//定义视图窗口
MyGraphicsScene *scene = new MyGraphicsScene;//创建场景
QImage *rawImage;//定义原图像指针,用于接收外部的图像指针,亮度对比度调节时用于读取原图像数据。
QImage newImage;//定义新图像,基于backImage数据进行亮度和对比度调节。
QLabel *brightnessLabel = new QLabel("Brightness"); //定义亮度标签
QLabel *contrastLabel = new QLabel("Contrast"); //定义对比度标签
QSlider *brightnessSlider = new QSlider(Qt::Horizontal); //定义亮度滑动条
QSlider *contrastSlider = new QSlider(Qt::Horizontal); //定义对比度滑动条
QLabel *brightnessAdjustLabel = new QLabel("0"); //定义亮度标签
QLabel *contrastAdjustLabel = new QLabel("0"); //定义对比度标签
QPushButton *okButton = new QPushButton("OK"); //定义OK按钮
QPushButton *resetButton = new QPushButton("Reset"); //定义复原按钮,亮度对比度改变设置为0
QPushButton *cancelButton = new QPushButton("Cancel"); //定义取消按钮
QElapsedTimer timer; //计算图像处理时间的计时器
void brightnessContrastValueChanged();//亮度对比度值发生变化时的处理函数
void brightnessContrastValueChangedslider();
uchar *ComputeLookUpTable8Bit(int brightness, int contrast); //计算一定亮度和对比度条件下的8位查询表
void adjustBrightnessContrast(uchar *lookupTable, int startLine, int endLine, int brightness, int contrast);//调整图像亮度和对比度
uchar getAdjustedByte(const int rawByte, int brightness, int contrast);
void okButtonClicked(); //OK按钮点击时的处理函数
void cancelButtonClicked();//Cancel按钮点击时的处理函数
void resetButtonClicked();//Cancel按钮点击时的处理函数
QLineEdit *brightnessLineEdit = new QLineEdit; // 亮度输入框
QLineEdit *contrastLineEdit = new QLineEdit; // 对比度输入框
};
//亮度对比度构造函数
MyDialog::MyDialog(QImage *rawImage, QWidget *parent): QDialog(parent) //构造函数定义,将外部图像作为引用传入
{
this->rawImage = rawImage; //将构造函数传入的rawImage指针赋值给本类的rawImage,虽然两个名称相同,但是不同的指针。
newImage=rawImage->copy(); //将backImage复制一份给newImage
view->setScene(scene); //设置view视窗的场景
scene->setShowImage(&newImage); //将newImage的指针赋给视图下面场景的showImage指针,用于显示描画该图像
scene->setSceneRect(newImage.rect());//设置场景坐标范围为图像尺寸
view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio); //使图形项保持纵横比缩放充满整个视图窗口
this->resize(860, 560); //重设窗口尺寸
this->setWindowTitle("Brightness/Contrast Adjustment");//设置窗口标题
this->setWindowFlags(Qt::Window); //设置本对话框的右上方最小化最大化关闭按钮和Window一样
brightnessAdjustLabel->setFixedWidth(36);
contrastAdjustLabel->setFixedWidth(36);
brightnessSlider->setRange(-255, 255); //设定范围为-255到255
brightnessSlider->setSingleStep(5); //设定递增步长
brightnessSlider->setPageStep(5); //设定PageUp和PageDown步长
contrastSlider->setRange(-255, 255); //设定范围为-255到255
contrastSlider->setSingleStep(5); //设定递增步长
contrastSlider->setPageStep(5); //设定PageUp和PageDown步长
connect(brightnessSlider, &QSlider::valueChanged, this,&brightnessContrastValueChangedslider);//亮度滑动条的值发生改变时的处理函数连接
connect(contrastSlider, &QSlider::valueChanged, this,&brightnessContrastValueChangedslider);//对比度滑动条的值发生改变时的处理函数连接
QVBoxLayout *vLayout = new QVBoxLayout; //定义垂直布局器
vLayout->setContentsMargins(6, 6, 6, 6); //设置布局器边界宽度
this->setLayout(vLayout);//将vLayout设定为本窗口的布局器
QGridLayout *gridLayout = new QGridLayout; //定义网格布局器,将亮度对比度的标签滑动条放入该布局器
gridLayout->addWidget(brightnessLabel, 0, 0);
gridLayout->addWidget(brightnessSlider, 0, 2);
gridLayout->addWidget(brightnessAdjustLabel, 0, 1);
gridLayout->addWidget(contrastLabel, 1, 0);
gridLayout->addWidget(contrastSlider, 1, 2);
gridLayout->addWidget(contrastAdjustLabel, 1, 1);
vLayout->addLayout(gridLayout);//将网格布局器gridLayout添加到vLayout布局器
vLayout->addWidget(view);//添加view到vLayout布局器
QHBoxLayout *hLayout = new QHBoxLayout; //定义水平布局器hLayout
hLayout->addStretch();//添加弹簧
hLayout->addWidget(okButton);//添加OK按钮到hLayout
hLayout->addWidget(resetButton);//添加Cancel按钮到hLayout
hLayout->addWidget(cancelButton);//添加Cancel按钮到hLayout
hLayout->addStretch();//添加弹簧
connect(okButton, &QPushButton::clicked, this, &okButtonClicked);//OK按钮点击和处理函数的连接
connect(resetButton, &QPushButton::clicked, this, &resetButtonClicked);//复原按钮点击和处理函数的连接
connect(cancelButton, &QPushButton::clicked, this, &cancelButtonClicked);//Cancel按钮点击和处理函数的连接
vLayout->addLayout(hLayout);//将hLayout布局器添加到vLayout布局器中
brightnessLineEdit->setValidator(new QIntValidator(-255, 255, this)); // 限制输入为整数,并设置范围
connect(brightnessLineEdit, &QLineEdit::editingFinished, this, &brightnessContrastValueChanged);
contrastLineEdit->setValidator(new QIntValidator(-255, 255, this)); // 限制输入为整数,并设置范围
connect(contrastLineEdit, &QLineEdit::editingFinished, this, &brightnessContrastValueChanged);
brightnessLineEdit->setFixedWidth(50);
contrastLineEdit->setFixedWidth(50);
gridLayout->addWidget(brightnessLineEdit, 0, 2);
gridLayout->addWidget(contrastLineEdit, 1, 2);
}
//亮度对比度声明函数
void MyDialog::brightnessContrastValueChanged()
{
int brightness = brightnessLineEdit->text().toInt(); // 获取亮度值
int contrast = contrastLineEdit->text().toInt(); // 获取对比度值
brightnessAdjustLabel->setText(QString::number(brightness));
contrastAdjustLabel->setText(QString::number(contrast));
int width=rawImage->width(); //图像宽
int height=rawImage->height();//图像高
//int brightness = brightnessSlider->value(); //获得亮度值
//int contrast = contrastSlider->value(); //获得对比度值
//brightnessAdjustLabel->setText(QString::number(brightness));
//contrastAdjustLabel->setText(QString::number(contrast));
timer.start(); //计时器开始计时
//以下循环用新的亮度和对比度调整数据,从rawImage数据计算得出newImage数据。
for (int i=0;i
{
uchar *rawData = rawImage->scanLine(i); //获得原图像第i行的首像素指针
uchar *newData = newImage.scanLine(i); //获得newImage图像第i行的首像素指针
for (int j=0;j
{
newData[0]=getAdjustedByte(rawData[0], brightness, contrast); //从原图blue子像素计算新blue子像素的值
newData[1]=getAdjustedByte(rawData[1], brightness, contrast); //计算新green子像素的值
newData[2]=getAdjustedByte(rawData[2], brightness, contrast);//计算新red子像素的值
rawData+=4;//因为是ARGB格式,所以跳4个字节到下一个像素。
newData+=4;//因为是ARGB格式,所以跳4个字节到下一个像素。
}
}
scene->update();//在场景上更新显示newImage
//在窗口标题栏显示图像尺寸和每次修改图像数据的时间
this->setWindowTitle(QString::number(width)+" x "+QString::number(height)+" - "+QString::number(timer.nsecsElapsed()/1000000.0,'f',1)+"ms");
}
void MyDialog::brightnessContrastValueChangedslider()
{
//int brightness = brightnessLineEdit->text().toInt(); // 获取亮度值
//int contrast = contrastLineEdit->text().toInt(); // 获取对比度值
//brightnessAdjustLabel->setText(QString::number(brightness));
//contrastAdjustLabel->setText(QString::number(contrast));
int width=rawImage->width(); //图像宽
int height=rawImage->height();//图像高
int brightness = brightnessSlider->value(); //获得亮度值
int contrast = contrastSlider->value(); //获得对比度值
brightnessAdjustLabel->setText(QString::number(brightness));
contrastAdjustLabel->setText(QString::number(contrast));
timer.start(); //计时器开始计时
//以下循环用新的亮度和对比度调整数据,从rawImage数据计算得出newImage数据。
for (int i=0;i
{
uchar *rawData = rawImage->scanLine(i); //获得原图像第i行的首像素指针
uchar *newData = newImage.scanLine(i); //获得newImage图像第i行的首像素指针
for (int j=0;j
{
newData[0]=getAdjustedByte(rawData[0], brightness, contrast); //从原图blue子像素计算新blue子像素的值
newData[1]=getAdjustedByte(rawData[1], brightness, contrast); //计算新green子像素的值
newData[2]=getAdjustedByte(rawData[2], brightness, contrast);//计算新red子像素的值
rawData+=4;//因为是ARGB格式,所以跳4个字节到下一个像素。
newData+=4;//因为是ARGB格式,所以跳4个字节到下一个像素。
}
}
scene->update();//在场景上更新显示newImage
//在窗口标题栏显示图像尺寸和每次修改图像数据的时间
this->setWindowTitle(QString::number(width)+" x "+QString::number(height)+" - "+QString::number(timer.nsecsElapsed()/1000000.0,'f',1)+"ms");
}
uchar MyDialog::getAdjustedByte(const int rawByte, int brightness, int contrast)
{
int temp = rawByte+brightness; //brightness adjustment
temp += (temp - 127) * contrast/255; //contrast adjustment
temp=qBound(0,temp,255);//temp的值小于0,取零,大于255,取255,保证temp是uchar类型范围
return temp;
}
void MyDialog::okButtonClicked()
{
this->accept();//OK按钮点击以后发出Accepted返回值
}
void MyDialog::resetButtonClicked()
{
brightnessSlider->setValue(0);//设置亮度滑动条值为0
contrastSlider->setValue(0);//设置对比度滑动条值为0
}
void MyDialog::cancelButtonClicked()
{
this->reject();//OK按钮点击以后发出Rejected返回值
}
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//旋转类对话框
class MyRorateDialog : public QDialog
{
public:
MyRorateDialog(QImage *rawImage, QWidget *parent = 0); //构造函数
QImage getNewImage()
{
return newImage;
}
private:
MyGraphicsView *view = new MyGraphicsView;//定义视图窗口
MyGraphicsScene *scene = new MyGraphicsScene;//创建场景
QImage *rawImage;//定义原图像指针,用于接收外部的图像指针,亮度对比度调节时用于读取原图像数据。
QImage newImage;//定义新图像,基于backImage数据进行亮度和对比度调节。
QLabel *rotationLabel = new QLabel("Rotation"); // 旋转角度标签
QLabel *rotationAdjustLabel = new QLabel("0");
QSlider *rotationSlider = new QSlider(Qt::Horizontal); // 旋转角度滑动条
QPushButton *okButton = new QPushButton("OK"); //定义OK按钮
QPushButton *resetButton = new QPushButton("Reset"); //定义复原按钮,亮度对比度改变设置为0
QPushButton *cancelButton = new QPushButton("Cancel"); //定义取消按钮
QElapsedTimer timer; //计算图像处理时间的计时器
void okButtonClicked(); //OK按钮点击时的处理函数
void cancelButtonClicked();//Cancel按钮点击时的处理函数
void resetButtonClicked();//Cancel按钮点击时的处理函数
void rorationValueChanged();
};
//旋转类构造函数
MyRorateDialog::MyRorateDialog(QImage *rawImage, QWidget *parent): QDialog(parent)
{
this->rawImage = rawImage; //将构造函数传入的rawImage指针赋值给本类的rawImage,虽然两个名称相同,但是不同的指针。
newImage=rawImage->copy(); //将backImage复制一份给newImage
view->setScene(scene); //设置view视窗的场景
scene->setShowImage(&newImage); //将newImage的指针赋给视图下面场景的showImage指针,用于显示描画该图像
scene->setSceneRect(newImage.rect());//设置场景坐标范围为图像尺寸
view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio); //使图形项保持纵横比缩放充满整个视图窗口
this->resize(860, 560); //重设窗口尺寸
this->setWindowTitle("Roration");//设置窗口标题
this->setWindowFlags(Qt::Window); //设置本对话框的右上方最小化最大化关闭按钮和Window一样
rotationLabel->setFixedWidth(36);
rotationSlider->setRange(-225, 255); // 设置旋转滑动条范围
rotationSlider->setSingleStep(5); // 设置步长
rotationSlider->setPageStep(5); // 设置PageUp和PageDown步长
QVBoxLayout *vLayout = new QVBoxLayout; //定义垂直布局器
vLayout->setContentsMargins(6, 6, 6, 6); //设置布局器边界宽度
this->setLayout(vLayout);//将vLayout设定为本窗口的布局器
QGridLayout *gridLayout = new QGridLayout; //定义网格布局器,将亮度对比度的标签滑动条放入该布局器
//gridLayout->addWidget(rotationLabel, 0, 0);
gridLayout->addWidget(rotationAdjustLabel, 0, 1);
gridLayout->addWidget(rotationSlider, 0, 2);
vLayout->addLayout(gridLayout);//将网格布局器gridLayout添加到vLayout布局器
vLayout->addWidget(view);//添加view到vLayout布局器
QHBoxLayout *hLayout = new QHBoxLayout; //定义水平布局器hLayout
hLayout->addStretch();//添加弹簧
hLayout->addWidget(okButton);//添加OK按钮到hLayout
hLayout->addWidget(resetButton);//添加Cancel按钮到hLayout
hLayout->addWidget(cancelButton);//添加Cancel按钮到hLayout
hLayout->addStretch();//添加弹簧
vLayout->addLayout(hLayout);//将hLayout布局器添加到vLayout布局器中
connect(okButton, &QPushButton::clicked, this, &okButtonClicked);//OK按钮点击和处理函数的连接
connect(resetButton, &QPushButton::clicked, this, &resetButtonClicked);//复原按钮点击和处理函数的连接
connect(cancelButton, &QPushButton::clicked, this, &cancelButtonClicked);//Cancel按钮点击和处理函数的连接
connect(rotationSlider, &QSlider::valueChanged, this, &MyRorateDialog::rorationValueChanged);
}
//旋转类声明函数
void MyRorateDialog::rorationValueChanged()
{
int rotationAngle = rotationSlider->value(); // 旋转角度值
rotationAdjustLabel->setText(QString("Rotation: %1").arg(rotationAngle));
timer.start(); // 计时器开始计时
int width = rawImage->width(); // 图像宽
int height = rawImage->height(); // 图像高
qreal radians = qDegreesToRadians(rotationAngle);
QTransform transform;
transform.translate(width / 2.0, height / 2.0); // 平移到图像中心
transform.rotateRadians(radians); // 旋转
transform.translate(-width / 2.0, -height / 2.0); // 平移回原点
newImage = rawImage->transformed(transform, Qt::SmoothTransformation); // 使用QTransform进行旋转
scene->update(); // 在场景上更新显示newImage
this->setWindowTitle(QString::number(width) + " x " + QString::number(height) + " - " +
QString::number(timer.nsecsElapsed() / 1000000.0, 'f', 1) + "ms");
}
void MyRorateDialog::okButtonClicked()
{
this->accept();//OK按钮点击以后发出Accepted返回值
}
void MyRorateDialog::resetButtonClicked()
{
rotationSlider->setValue(0);//设置亮度滑动条值为0
}
void MyRorateDialog::cancelButtonClicked()
{
this->reject();//OK按钮点击以后发出Rejected返回值
}
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//颗粒对话框类
class MyGrainDialog : public QDialog
{
public:
MyGrainDialog(QImage *rawImage, QWidget *parent = 0); //构造函数
QImage getNewImage()
{
return newImage;
}
private:
MyGraphicsView *view = new MyGraphicsView;//定义视图窗口
MyGraphicsScene *scene = new MyGraphicsScene;//创建场景
QImage *rawImage;//定义原图像指针,用于接收外部的图像指针,亮度对比度调节时用于读取原图像数据。
QImage newImage;//定义新图像,基于backImage数据进行亮度和对比度调节。
QLabel *grainLabel = new QLabel("Grain");
QSlider *grainSlider = new QSlider(Qt::Horizontal);
QLabel *grainAdjustLabel = new QLabel("0");
QPushButton *okButton = new QPushButton("OK"); //定义OK按钮
QPushButton *resetButton = new QPushButton("Reset"); //定义复原按钮,亮度对比度改变设置为0
QPushButton *cancelButton = new QPushButton("Cancel"); //定义取消按钮
QElapsedTimer timer; //计算图像处理时间的计时器
void okButtonClicked(); //OK按钮点击时的处理函数
void cancelButtonClicked();//Cancel按钮点击时的处理函数
void resetButtonClicked();//Cancel按钮点击时的处理函数
void applyGrain(int strength);
void grainValueChanged(int value);
};
//颗粒构造函数
MyGrainDialog::MyGrainDialog(QImage *rawImage, QWidget *parent): QDialog(parent)
{
this->rawImage = rawImage; //将构造函数传入的rawImage指针赋值给本类的rawImage,虽然两个名称相同,但是不同的指针。
newImage=rawImage->copy(); //将backImage复制一份给newImage
view->setScene(scene); //设置view视窗的场景
scene->setShowImage(&newImage); //将newImage的指针赋给视图下面场景的showImage指针,用于显示描画该图像
scene->setSceneRect(newImage.rect());//设置场景坐标范围为图像尺寸
view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio); //使图形项保持纵横比缩放充满整个视图窗口
this->resize(860, 560); //重设窗口尺寸
this->setWindowTitle("Grain");//设置窗口标题
this->setWindowFlags(Qt::Window); //设置本对话框的右上方最小化最大化关闭按钮和Window一样
grainLabel->setFixedWidth(36);
grainAdjustLabel->setFixedWidth(36);
grainSlider->setRange(0, 100); // 设定范围为 0 到 100
grainSlider->setSingleStep(1); // 设定递增步长
grainSlider->setPageStep(10); // 设定 PageUp 和 PageDown 步长
QVBoxLayout *vLayout = new QVBoxLayout; //定义垂直布局器
vLayout->setContentsMargins(6, 6, 6, 6); //设置布局器边界宽度
this->setLayout(vLayout);//将vLayout设定为本窗口的布局器
QGridLayout *gridLayout = new QGridLayout; //定义网格布局器,将亮度对比度的标签滑动条放入该布局器
gridLayout->addWidget(grainLabel, 0, 0);
gridLayout->addWidget(grainSlider, 0, 2);
gridLayout->addWidget(grainAdjustLabel, 0, 1);
vLayout->addLayout(gridLayout);//将网格布局器gridLayout添加到vLayout布局器 拼多多帮助力入口
vLayout->addWidget(view);//添加view到vLayout布局器
QHBoxLayout *hLayout = new QHBoxLayout; //定义水平布局器hLayout
hLayout->addStretch();//添加弹簧
hLayout->addWidget(okButton);//添加OK按钮到hLayout
hLayout->addWidget(resetButton);//添加Cancel按钮到hLayout
hLayout->addWidget(cancelButton);//添加Cancel按钮到hLayout
hLayout->addStretch();//添加弹簧
vLayout->addLayout(hLayout);//将hLayout布局器添加到vLayout布局器中
connect(okButton, &QPushButton::clicked, this, &okButtonClicked);//OK按钮点击和处理函数的连接
connect(resetButton, &QPushButton::clicked, this, &resetButtonClicked);//复原按钮点击和处理函数的连接
connect(cancelButton, &QPushButton::clicked, this, &cancelButtonClicked);//Cancel按钮点击和处理函数的连接
connect(grainSlider, &QSlider::valueChanged, this, &MyGrainDialog::grainValueChanged);
connect(grainSlider, &QSlider::valueChanged, this, [this](int value) { applyGrain(value); });
}
//颗粒声明函数
void MyGrainDialog::grainValueChanged(int value)
{
grainAdjustLabel->setText(QString::number(value));
applyGrain(value);
}
void MyGrainDialog::applyGrain(int strength)
{
int width = rawImage->width();
int height = rawImage->height();
// 随机数生成器
std::default_random_engine generator;
std::uniform_int_distributiondistribution(-strength, strength);
for (int i = 0; i < height; i++)
{
uchar *rawData = rawImage->scanLine(i);
uchar *newData = newImage.scanLine(i);
for (int j = 0; j < width; j++)
{
// 对每个通道应用颗粒感
newData[0] = qBound(0, rawData[0] + distribution(generator), 255);
newData[1] = qBound(0, rawData[1] + distribution(generator), 255);
newData[2] = qBound(0, rawData[2] + distribution(generator), 255);
rawData += 4;
newData += 4;
}
}
scene->update();
}
void MyGrainDialog::okButtonClicked()
{
this->accept();//OK按钮点击以后发出Accepted返回值
}
void MyGrainDialog::resetButtonClicked()
{
grainSlider->setValue(0);//设置亮度滑动条值为0
}
void MyGrainDialog::cancelButtonClicked()
{
this->reject();//OK按钮点击以后发出Rejected返回值
}
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//褪色对话框类
class MyFadeDialog : public QDialog
{
public:
MyFadeDialog(QImage *rawImage, QWidget *parent = 0); //构造函数
QImage getNewImage()
{
return newImage;
}
private:
MyGraphicsView *view = new MyGraphicsView;//定义视图窗口
MyGraphicsScene *scene = new MyGraphicsScene;//创建场景
QImage *rawImage;//定义原图像指针,用于接收外部的图像指针,亮度对比度调节时用于读取原图像数据。
QImage newImage;//定义新图像,基于backImage数据进行亮度和对比度调节。
QLabel *fadeLabel = new QLabel("Fade"); // 定义褪色标签
QSlider *fadeSlider = new QSlider(Qt::Horizontal); // 定义褪色滑动条
QLabel *fadeAdjustLabel = new QLabel("0"); // 定义褪色标签
QPushButton *okButton = new QPushButton("OK"); //定义OK按钮
QPushButton *resetButton = new QPushButton("Reset"); //定义复原按钮,亮度对比度改变设置为0
QPushButton *cancelButton = new QPushButton("Cancel"); //定义取消按钮
QElapsedTimer timer; //计算图像处理时间的计时器
void okButtonClicked(); //OK按钮点击时的处理函数
void cancelButtonClicked();//Cancel按钮点击时的处理函数
void resetButtonClicked();//Cancel按钮点击时的处理函数
void fadeColor(QColor &color, int fadeValue);
void fadeValueChanged(); // 褪色值发生变化时的处理函数
};
//褪色构造函数
MyFadeDialog::MyFadeDialog(QImage *rawImage, QWidget *parent): QDialog(parent)
{
this->rawImage = rawImage; //将构造函数传入的rawImage指针赋值给本类的rawImage,虽然两个名称相同,但是不同的指针。
newImage=rawImage->copy(); //将backImage复制一份给newImage
view->setScene(scene); //设置view视窗的场景
scene->setShowImage(&newImage); //将newImage的指针赋给视图下面场景的showImage指针,用于显示描画该图像
scene->setSceneRect(newImage.rect());//设置场景坐标范围为图像尺寸
view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio); //使图形项保持纵横比缩放充满整个视图窗口
this->resize(860, 560); //重设窗口尺寸
this->setWindowTitle("Fade");//设置窗口标题
this->setWindowFlags(Qt::Window); //设置本对话框的右上方最小化最大化关闭按钮和Window一样
fadeLabel->setFixedWidth(36);
fadeAdjustLabel->setFixedWidth(36);
fadeSlider->setRange(0, 100); // 设定范围为 0 到 100
fadeSlider->setSingleStep(1); // 设定递增步长
fadeSlider->setPageStep(5); // 设定 PageUp 和 PageDown 步长
QVBoxLayout *vLayout = new QVBoxLayout; //定义垂直布局器
vLayout->setContentsMargins(6, 6, 6, 6); //设置布局器边界宽度
this->setLayout(vLayout);//将vLayout设定为本窗口的布局器
QGridLayout *gridLayout = new QGridLayout; //定义网格布局器,将亮度对比度的标签滑动条放入该布局器
gridLayout->addWidget(fadeLabel, 0, 0);
gridLayout->addWidget(fadeSlider, 0, 2);
gridLayout->addWidget(fadeAdjustLabel, 0, 1);
vLayout->addLayout(gridLayout);//将网格布局器gridLayout添加到vLayout布局器
vLayout->addWidget(view);//添加view到vLayout布局器
QHBoxLayout *hLayout = new QHBoxLayout; //定义水平布局器hLayout
hLayout->addStretch();//添加弹簧
hLayout->addWidget(okButton);//添加OK按钮到hLayout
hLayout->addWidget(resetButton);//添加Cancel按钮到hLayout
hLayout->addWidget(cancelButton);//添加Cancel按钮到hLayout
hLayout->addStretch();//添加弹簧
vLayout->addLayout(hLayout);//将hLayout布局器添加到vLayout布局器中
connect(okButton, &QPushButton::clicked, this, &okButtonClicked);//OK按钮点击和处理函数的连接
connect(resetButton, &QPushButton::clicked, this, &resetButtonClicked);//复原按钮点击和处理函数的连接
connect(cancelButton, &QPushButton::clicked, this, &cancelButtonClicked);//Cancel按钮点击和处理函数的连接
connect(fadeSlider, &QSlider::valueChanged, this, &fadeValueChanged); // 褪色滑动条的值发生改变时的处理函数连接
}
//褪色声明函数
void MyFadeDialog::fadeValueChanged()
{
int width = rawImage->width();
int height = rawImage->height();
int fadeValue = fadeSlider->value();
fadeAdjustLabel->setText(QString::number(fadeValue));
timer.start();
for (int i = 0; i < height; i++)
{
uchar *rawData = rawImage->scanLine(i);
uchar *newData = newImage.scanLine(i);
for (int j = 0; j < width; j++)
{
QColor color(rawData[2], rawData[1], rawData[0]); // 注意这里颜色顺序是反的,因为 QImage 存储的顺序是 BGRA
fadeColor(color, fadeValue);
newData[0] = color.blue();
newData[1] = color.green();
newData[2] = color.red();
newData[3] = rawData[3];
rawData += 4;
newData += 4;
}
}
scene->update();
this->setWindowTitle(QString::number(width) + " x " + QString::number(height) + " - " +
QString::number(timer.nsecsElapsed() / 1000000.0, 'f', 1) + "ms");
}
void MyFadeDialog::fadeColor(QColor &color, int fadeValue)
{
int h, s, v;
color.getHsv(&h, &s, &v);
// 调整饱和度
s = qMax(0, s - fadeValue);
color.setHsv(h, s, v);
}
void MyFadeDialog::okButtonClicked()
{
this->accept();//OK按钮点击以后发出Accepted返回值
}
void MyFadeDialog::resetButtonClicked()
{
fadeSlider->setValue(0);//设置亮度滑动条值为0
}
void MyFadeDialog::cancelButtonClicked()
{
this->reject();//OK按钮点击以后发出Rejected返回值
}
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//色相对话框
class MyHighlightDialog : public QDialog
{
public:
MyHighlightDialog(QImage *rawImage, QWidget *parent = 0); //构造函数
QImage getNewImage()
{
return newImage;
}
private:
MyGraphicsView *view = new MyGraphicsView;//定义视图窗口
MyGraphicsScene *scene = new MyGraphicsScene;//创建场景
QImage *rawImage;//定义原图像指针,用于接收外部的图像指针,亮度对比度调节时用于读取原图像数据。
QImage newImage;//定义新图像,基于backImage数据进行亮度和对比度调节。
QLabel *highlightLabel = new QLabel("Hue");
QSlider *highlightSlider = new QSlider(Qt::Horizontal);
QLabel *highlightAdjustLabel = new QLabel("0");
QPushButton *okButton = new QPushButton("OK"); //定义OK按钮
QPushButton *resetButton = new QPushButton("Reset"); //定义复原按钮,亮度对比度改变设置为0
QPushButton *cancelButton = new QPushButton("Cancel"); //定义取消按钮
QElapsedTimer timer; //计算图像处理时间的计时器
void okButtonClicked(); //OK按钮点击时的处理函数
void cancelButtonClicked();//Cancel按钮点击时的处理函数
void resetButtonClicked();//Cancel按钮点击时的处理函数
void adjustHue(int hue);
void RGBtoHSV(int r, int g, int b, int &h, int &s, int &v);
void HSVtoRGB(int h, int s, int v, int &r, int &g, int &b);
// 其他函数...
};
//色相构造函数
MyHighlightDialog::MyHighlightDialog(QImage *rawImage, QWidget *parent): QDialog(parent)
{
this->rawImage = rawImage; //将构造函数传入的rawImage指针赋值给本类的rawImage,虽然两个名称相同,但是不同的指针。
newImage=rawImage->copy(); //将backImage复制一份给newImage
view->setScene(scene); //设置view视窗的场景
scene->setShowImage(&newImage); //将newImage的指针赋给视图下面场景的showImage指针,用于显示描画该图像
scene->setSceneRect(newImage.rect());//设置场景坐标范围为图像尺寸
view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio); //使图形项保持纵横比缩放充满整个视图窗口
this->resize(860, 560); //重设窗口尺寸
this->setWindowTitle("Hue");//设置窗口标题
this->setWindowFlags(Qt::Window); //设置本对话框的右上方最小化最大化关闭按钮和Window一样
highlightLabel->setFixedWidth(36);
highlightAdjustLabel->setFixedWidth(36);
highlightSlider->setRange(-255, 255);
highlightSlider->setSingleStep(5);
highlightSlider->setPageStep(5);
QVBoxLayout *vLayout = new QVBoxLayout; //定义垂直布局器
vLayout->setContentsMargins(6, 6, 6, 6); //设置布局器边界宽度
this->setLayout(vLayout);//将vLayout设定为本窗口的布局器
QGridLayout *gridLayout = new QGridLayout; //定义网格布局器,将亮度对比度的标签滑动条放入该布局器
gridLayout->addWidget(highlightLabel, 0, 0);
gridLayout->addWidget(highlightSlider, 0, 2);
gridLayout->addWidget(highlightAdjustLabel, 0, 1);
vLayout->addLayout(gridLayout);//将网格布局器gridLayout添加到vLayout布局器
vLayout->addWidget(view);//添加view到vLayout布局器
QHBoxLayout *hLayout = new QHBoxLayout; //定义水平布局器hLayout
hLayout->addStretch();//添加弹簧
hLayout->addWidget(okButton);//添加OK按钮到hLayout
hLayout->addWidget(resetButton);//添加Cancel按钮到hLayout
hLayout->addWidget(cancelButton);//添加Cancel按钮到hLayout
hLayout->addStretch();//添加弹簧
vLayout->addLayout(hLayout);//将hLayout布局器添加到vLayout布局器中
connect(okButton, &QPushButton::clicked, this, &okButtonClicked);//OK按钮点击和处理函数的连接
connect(resetButton, &QPushButton::clicked, this, &resetButtonClicked);//复原按钮点击和处理函数的连接
connect(cancelButton, &QPushButton::clicked, this, &cancelButtonClicked);//Cancel按钮点击和处理函数的连接
connect(highlightSlider, &QSlider::valueChanged, this, &MyHighlightDialog::adjustHue);
}
//色相声明函数
void MyHighlightDialog::adjustHue(int hue)
{
int width = rawImage->width();
int height = rawImage->height();
for (int i = 0; i < height; i++)
{
uchar *rawData = rawImage->scanLine(i);
uchar *newData = newImage.scanLine(i);
for (int j = 0; j < width; j++)
{
int h, s, v;
RGBtoHSV(rawData[2], rawData[1], rawData[0], h, s, v); // 将RGB转换为HSV
// 调整色相
h += hue;
if (h < 0)
h += 360;
else if (h >= 360)
h -= 360;
int r, g, b;
HSVtoRGB(h, s, v, r, g, b); // 将HSV转换为RGB
newData[0] = b;
newData[1] = g;
newData[2] = r;
rawData += 4;
newData += 4;
}
}
scene->update();
}
void MyHighlightDialog::RGBtoHSV(int r, int g, int b, int &h, int &s, int &v)
{
double red = r / 255.0;
double green = g / 255.0;
double blue = b / 255.0;
double cmax = std::max(std::max(red, green), blue);
double cmin = std::min(std::min(red, green), blue);
double delta = cmax - cmin;
// 计算色相
if (delta == 0)
h = 0;
else if (cmax == red)
h = static_cast(fmod(60 * ((green - blue) / delta + 6), 360));
else if (cmax == green)
h = static_cast(fmod(60 * ((blue - red) / delta + 6), 360));
else if (cmax == blue)
h = static_cast(fmod(60 * ((red - green) / delta + 6), 360));
// 计算饱和度
if (cmax == 0)
s = 0;
else
s = static_cast((delta / cmax) * 100);
// 计算明度
v = static_cast(cmax * 100);
}
void MyHighlightDialog::HSVtoRGB(int h, int s, int v, int &r, int &g, int &b)
{
double hue = h / 60.0;
double saturation = s / 100.0;
double value = v / 100.0;
int hi = static_cast(std::floor(hue)) % 6;
double f = hue - std::floor(hue);
double p = value * (1 - saturation);
double q = value * (1 - f * saturation);
double t = value * (1 - (1 - f) * saturation);
switch (hi)
{
case 0:
r = static_cast(value * 255);
g = static_cast(t * 255);
b = static_cast(p * 255);
break;
case 1:
r = static_cast(q * 255);
g = static_cast(value * 255);
b = static_cast(p * 255);
break;
case 2:
r = static_cast(p * 255);
g = static_cast(value * 255);
b = static_cast(t * 255);
break;
case 3:
r = static_cast(p * 255);
g = static_cast(q * 255);
b = static_cast(value * 255);
break;
case 4:
r = static_cast(t * 255);
g = static_cast(p * 255);
b = static_cast(value * 255);
break;
case 5:
r = static_cast(value * 255);
g = static_cast(p * 255);
b = static_cast(q * 255);
break;
}
}
void MyHighlightDialog::okButtonClicked()
{
this->accept();//OK按钮点击以后发出Accepted返回值
}
void MyHighlightDialog::resetButtonClicked()
{
highlightSlider->setValue(0);//设置亮度滑动条值为0
}
void MyHighlightDialog::cancelButtonClicked()
{
this->reject();//OK按钮点击以后发出Rejected返回值
}
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//饱和度对话框
class MySaturationDialog : public QDialog
{
public:
MySaturationDialog(QImage *rawImage, QWidget *parent = 0); //构造函数
QImage getNewImage()
{
return newImage;
}
MyGraphicsView *view = new MyGraphicsView;//定义视图窗口
MyGraphicsScene *scene = new MyGraphicsScene;//创建场景
QImage *rawImage;//定义原图像指针,用于接收外部的图像指针,亮度对比度调节时用于读取原图像数据。
QImage newImage;//定义新图像,基于backImage数据进行亮度和对比度调节。
QLabel *saturationLabel = new QLabel("Saturation"); // 旋转角度标签
QLabel *saturationAdjustLabel = new QLabel("0"); // 旋转角度标签
QSlider *saturationSlider = new QSlider(Qt::Horizontal);
QPushButton *okButton = new QPushButton("OK"); //定义OK按钮
QPushButton *resetButton = new QPushButton("Reset"); //定义复原按钮,亮度对比度改变设置为0
QPushButton *cancelButton = new QPushButton("Cancel"); //定义取消按钮
QElapsedTimer timer; //计算图像处理时间的计时器
void okButtonClicked(); //OK按钮点击时的处理函数
void cancelButtonClicked();//Cancel按钮点击时的处理函数
void resetButtonClicked();//Cancel按钮点击时的处理函数
void saturationValueChanged();
};
//饱和度构造函数
MySaturationDialog::MySaturationDialog(QImage *rawImage, QWidget *parent): QDialog(parent)
{
this->rawImage = rawImage; //将构造函数传入的rawImage指针赋值给本类的rawImage,虽然两个名称相同,但是不同的指针。
newImage=rawImage->copy(); //将backImage复制一份给newImage
view->setScene(scene); //设置view视窗的场景
scene->setShowImage(&newImage); //将newImage的指针赋给视图下面场景的showImage指针,用于显示描画该图像
scene->setSceneRect(newImage.rect());//设置场景坐标范围为图像尺寸
view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio); //使图形项保持纵横比缩放充满整个视图窗口
this->resize(860, 560); //重设窗口尺寸
this->setWindowTitle("Saturation");//设置窗口标题
this->setWindowFlags(Qt::Window); //设置本对话框的右上方最小化最大化关闭按钮和Window一样
saturationLabel->setFixedWidth(36);
saturationSlider->setRange(-225, 255); // 设置旋转滑动条范围
saturationSlider->setSingleStep(5); // 设置步长
saturationSlider->setPageStep(5); // 设置PageUp和PageDown步长
QVBoxLayout *vLayout = new QVBoxLayout; //定义垂直布局器
vLayout->setContentsMargins(6, 6, 6, 6); //设置布局器边界宽度
this->setLayout(vLayout);//将vLayout设定为本窗口的布局器
QGridLayout *gridLayout = new QGridLayout; //定义网格布局器,将亮度对比度的标签滑动条放入该布局器
gridLayout->addWidget(saturationLabel, 0, 0);
gridLayout->addWidget(saturationAdjustLabel, 0, 1);
gridLayout->addWidget(saturationSlider, 0, 2);
vLayout->addLayout(gridLayout);//将网格布局器gridLayout添加到vLayout布局器
vLayout->addWidget(view);//添加view到vLayout布局器
QHBoxLayout *hLayout = new QHBoxLayout; //定义水平布局器hLayout
hLayout->addStretch();//添加弹簧
hLayout->addWidget(okButton);//添加OK按钮到hLayout
hLayout->addWidget(resetButton);//添加Cancel按钮到hLayout
hLayout->addWidget(cancelButton);//添加Cancel按钮到hLayout
hLayout->addStretch();//添加弹簧
vLayout->addLayout(hLayout);//将hLayout布局器添加到vLayout布局器中
connect(okButton, &QPushButton::clicked, this, &okButtonClicked);//OK按钮点击和处理函数的连接
connect(resetButton, &QPushButton::clicked, this, &resetButtonClicked);//复原按钮点击和处理函数的连接
connect(cancelButton, &QPushButton::clicked, this, &cancelButtonClicked);//Cancel按钮点击和处理函数的连接
connect(saturationSlider, &QSlider::valueChanged, this, &MySaturationDialog::saturationValueChanged);
}
//饱和度声明函数
void MySaturationDialog::saturationValueChanged()
{
int saturation = saturationSlider->value();
saturationAdjustLabel->setText(QString::number(saturation));
timer.start();
int width = rawImage->width();
int height = rawImage->height();
for (int i = 0; i < height; i++)
{
uchar *inputData = rawImage->scanLine(i);
uchar *outputData = newImage.scanLine(i);
for (int j = 0; j < width; j++)
{
// Extract color channels
uchar blue = inputData[0];
uchar green = inputData[1];
uchar red = inputData[2];
// Convert RGB to HSV
int maxc = std::max({red, green, blue});
int minc = std::min({red, green, blue});
int delta = maxc - minc;
int value = maxc;
int saturationFactor = saturation;
// Calculate saturation factor
if (value > 0)
{
saturationFactor += (delta * 128) / value;
}
// Scale the color channels
red = std::clamp((red - 128) * saturationFactor / 128 + 128, 0, 255);
green = std::clamp((green - 128) * saturationFactor / 128 + 128, 0, 255);
blue = std::clamp((blue - 128) * saturationFactor / 128 + 128, 0, 255);
// Store the result
outputData[0] = blue;
outputData[1] = green;
outputData[2] = red;
inputData += 4;
outputData += 4;
}
}
scene->update();
this->setWindowTitle(QString::number(width) + " x " + QString::number(height) + " - " + QString::number(timer.nsecsElapsed() / 1000000.0, 'f', 1) + "ms");
}
void MySaturationDialog::okButtonClicked()
{
this->accept();//OK按钮点击以后发出Accepted返回值
}
void MySaturationDialog::resetButtonClicked()
{
saturationSlider->setValue(0);//设置亮度滑动条值为0
}
void MySaturationDialog::cancelButtonClicked()
{
this->reject();//OK按钮点击以后发出Rejected返回值
}
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//模糊对话框
class MyBlurDialog : public QDialog
{
public:
MyBlurDialog(QImage *rawImage, QWidget *parent = 0); //构造函数
QImage getNewImage()
{
return newImage;
}
MyGraphicsView *view = new MyGraphicsView;//定义视图窗口
MyGraphicsScene *scene = new MyGraphicsScene;//创建场景
QImage *rawImage;//定义原图像指针,用于接收外部的图像指针,亮度对比度调节时用于读取原图像数据。
QImage newImage;//定义新图像,基于backImage数据进行亮度和对比度调节。
QLabel *blurLabel = new QLabel("Blur"); // 定义模糊标签
QSlider *blurSlider = new QSlider(Qt::Horizontal); // 定义模糊滑动条
QLabel *blurAdjustLabel = new QLabel("0"); // 定义模糊标签
QPushButton *okButton = new QPushButton("OK"); //定义OK按钮
QPushButton *resetButton = new QPushButton("Reset"); //定义复原按钮,亮度对比度改变设置为0
QPushButton *cancelButton = new QPushButton("Cancel"); //定义取消按钮
QElapsedTimer timer; //计算图像处理时间的计时器
void okButtonClicked(); //OK按钮点击时的处理函数
void cancelButtonClicked();//Cancel按钮点击时的处理函数
void resetButtonClicked();//Cancel按钮点击时的处理函数
void blurValueChanged();
QImage applyGaussianBlur(const QImage &sourceImage, int radius);
QRgb applyGaussianBlurAtPixel(const QImage &sourceImage, int x, int y, int radius, const QVector&kernel);
};
//模糊构造函数
MyBlurDialog::MyBlurDialog(QImage *rawImage, QWidget *parent): QDialog(parent)
{
this->rawImage = rawImage; //将构造函数传入的rawImage指针赋值给本类的rawImage,虽然两个名称相同,但是不同的指针。
newImage=rawImage->copy(); //将backImage复制一份给newImage
view->setScene(scene); //设置view视窗的场景
scene->setShowImage(&newImage); //将newImage的指针赋给视图下面场景的showImage指针,用于显示描画该图像
scene->setSceneRect(newImage.rect());//设置场景坐标范围为图像尺寸
view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio); //使图形项保持纵横比缩放充满整个视图窗口
this->resize(860, 560); //重设窗口尺寸
this->setWindowTitle("Blur");//设置窗口标题
this->setWindowFlags(Qt::Window); //设置本对话框的右上方最小化最大化关闭按钮和Window一样
blurLabel->setFixedWidth(36);
blurAdjustLabel->setFixedWidth(36);
blurSlider->setRange(0, 100); // 设置旋转滑动条范围
blurSlider->setSingleStep(10); // 设置步长
blurSlider->setPageStep(5); // 设置PageUp和PageDown步长
QVBoxLayout *vLayout = new QVBoxLayout; //定义垂直布局器
vLayout->setContentsMargins(6, 6, 6, 6); //设置布局器边界宽度
this->setLayout(vLayout);//将vLayout设定为本窗口的布局器
QGridLayout *gridLayout = new QGridLayout; //定义网格布局器,将亮度对比度的标签滑动条放入该布局器
gridLayout->addWidget(blurLabel, 0, 0);
gridLayout->addWidget(blurSlider, 0, 2);
gridLayout->addWidget(blurAdjustLabel, 0, 1);
vLayout->addLayout(gridLayout);//将网格布局器gridLayout添加到vLayout布局器
vLayout->addWidget(view);//添加view到vLayout布局器
QHBoxLayout *hLayout = new QHBoxLayout; //定义水平布局器hLayout
hLayout->addStretch();//添加弹簧
hLayout->addWidget(okButton);//添加OK按钮到hLayout
hLayout->addWidget(resetButton);//添加Cancel按钮到hLayout
hLayout->addWidget(cancelButton);//添加Cancel按钮到hLayout
hLayout->addStretch();//添加弹簧
vLayout->addLayout(hLayout);//将hLayout布局器添加到vLayout布局器中
connect(okButton, &QPushButton::clicked, this, &okButtonClicked);//OK按钮点击和处理函数的连接
connect(resetButton, &QPushButton::clicked, this, &resetButtonClicked);//复原按钮点击和处理函数的连接
connect(cancelButton, &QPushButton::clicked, this, &cancelButtonClicked);//Cancel按钮点击和处理函数的连接
connect(blurSlider, &QSlider::valueChanged, this, &MyBlurDialog::blurValueChanged); // 模糊滑动条的值发生改变时的处理函数连接
}
//模糊声明函数
void MyBlurDialog::blurValueChanged()
{
int width = rawImage->width(); // 图像宽
int height = rawImage->height(); // 图像高
int radius = blurSlider->value(); // 模糊半径
timer.start(); // 计时器开始计时
QImage blurredImage = applyGaussianBlur(*rawImage, radius);
newImage = blurredImage;
scene->clear();
scene->addPixmap(QPixmap::fromImage(blurredImage));
this->setWindowTitle(QString::number(width) + " x " + QString::number(height) + " - " + QString::number(timer.nsecsElapsed() / 1000000.0, 'f', 1) + "ms");
};
QImage MyBlurDialog::applyGaussianBlur(const QImage &sourceImage, int radius)
{
if (radius <= 0)
{
return sourceImage.copy();
}
// 创建一个高斯模糊内核
QVectorkernel;
float sigma = static_cast(radius) / 2.0;
float sum = 0.0;
for (int i = -radius; i <= radius; ++i)
{
float value = std::exp(-static_cast(i * i) / (2.0 * sigma * sigma));
kernel.append(value);
sum += value;
}
// 归一化内核
for (float &value : kernel)
{
value /= sum;
}
QImage blurredImage = rawImage->copy();
int width = sourceImage.width();
int height = sourceImage.height();
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
QRgb blurredPixel = applyGaussianBlurAtPixel(sourceImage, x, y, radius, kernel);
resultImage.setPixel(x, y, blurredPixel);
}
}
newImage = blurredImage;
return resultImage;
}
QRgb MyBlurDialog::applyGaussianBlurAtPixel(const QImage &sourceImage, int x, int y, int radius, const QVector&kernel)
{
int width = sourceImage.width();
int height = sourceImage.height();
float r = 0.0, g = 0.0, b = 0.0;
for (int i = -radius; i <= radius; ++i)
{
int xi = qBound(0, x + i, width - 1);
int pixel = sourceImage.pixel(xi, y);
float weight = kernel[i + radius];
r += qRed(pixel) * weight;
g += qGreen(pixel) * weight;
b += qBlue(pixel) * weight;
}
return qRgb(static_cast(r), static_cast(g), static_cast(b));
}
void MyBlurDialog::okButtonClicked()
{
this->accept();//OK按钮点击以后发出Accepted返回值
}
void MyBlurDialog::resetButtonClicked()
{
blurSlider->setValue(0);//设置亮度滑动条值为0
}
void MyBlurDialog::cancelButtonClicked()
{
this->reject();//OK按钮点击以后发出Rejected返回值
}
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//色调对话框
class MySharpenDialog : public QDialog
{
public:
MySharpenDialog(QImage *rawImage, QWidget *parent = 0); //构造函数
QImage getNewImage()
{
return newImage;
}
MyGraphicsView *view = new MyGraphicsView;//定义视图窗口
MyGraphicsScene *scene = new MyGraphicsScene;//创建场景
QImage *rawImage;//定义原图像指针,用于接收外部的图像指针,亮度对比度调节时用于读取原图像数据。
QImage newImage;//定义新图像,基于backImage数据进行亮度和对比度调节。
QLabel *sharpenLabel = new QLabel("sharpen"); // 旋转角度标签
QLabel *sharpenAdjustLabel = new QLabel("0"); // 旋转角度标签
QSlider *sharpenSlider = new QSlider(Qt::Horizontal);
QPushButton *okButton = new QPushButton("OK"); //定义OK按钮
QPushButton *resetButton = new QPushButton("Reset"); //定义复原按钮,亮度对比度改变设置为0
QPushButton *cancelButton = new QPushButton("Cancel"); //定义取消按钮
QElapsedTimer timer; //计算图像处理时间的计时器
void okButtonClicked(); //OK按钮点击时的处理函数
void cancelButtonClicked();//Cancel按钮点击时的处理函数
void resetButtonClicked();//Cancel按钮点击时的处理函数
void sharpenValueChanged();
};
//色调构造函数
MySharpenDialog::MySharpenDialog(QImage *rawImage, QWidget *parent): QDialog(parent)
{
this->rawImage = rawImage; //将构造函数传入的rawImage指针赋值给本类的rawImage,虽然两个名称相同,但是不同的指针。
newImage=rawImage->copy(); //将backImage复制一份给newImage
view->setScene(scene); //设置view视窗的场景
scene->setShowImage(&newImage); //将newImage的指针赋给视图下面场景的showImage指针,用于显示描画该图像
scene->setSceneRect(newImage.rect());//设置场景坐标范围为图像尺寸
view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio); //使图形项保持纵横比缩放充满整个视图窗口
this->resize(860, 560); //重设窗口尺寸
this->setWindowTitle("Sharpen");//设置窗口标题
this->setWindowFlags(Qt::Window); //设置本对话框的右上方最小化最大化关闭按钮和Window一样
sharpenLabel->setFixedWidth(36);
sharpenSlider->setRange(-225, 255); // 设置旋转滑动条范围
sharpenSlider->setSingleStep(5); // 设置步长
sharpenSlider->setPageStep(5); // 设置PageUp和PageDown步长
QVBoxLayout *vLayout = new QVBoxLayout; //定义垂直布局器
vLayout->setContentsMargins(6, 6, 6, 6); //设置布局器边界宽度
this->setLayout(vLayout);//将vLayout设定为本窗口的布局器
QGridLayout *gridLayout = new QGridLayout; //定义网格布局器,将亮度对比度的标签滑动条放入该布局器
gridLayout->addWidget(sharpenLabel, 0, 0);
gridLayout->addWidget(sharpenAdjustLabel, 0, 1);
gridLayout->addWidget(sharpenSlider, 0, 2);
vLayout->addLayout(gridLayout);//将网格布局器gridLayout添加到vLayout布局器
vLayout->addWidget(view);//添加view到vLayout布局器
QHBoxLayout *hLayout = new QHBoxLayout; //定义水平布局器hLayout
hLayout->addStretch();//添加弹簧
hLayout->addWidget(okButton);//添加OK按钮到hLayout
hLayout->addWidget(resetButton);//添加Cancel按钮到hLayout
hLayout->addWidget(cancelButton);//添加Cancel按钮到hLayout
hLayout->addStretch();//添加弹簧
vLayout->addLayout(hLayout);//将hLayout布局器添加到vLayout布局器中
connect(okButton, &QPushButton::clicked, this, &okButtonClicked);//OK按钮点击和处理函数的连接
connect(resetButton, &QPushButton::clicked, this, &resetButtonClicked);//复原按钮点击和处理函数的连接
connect(cancelButton, &QPushButton::clicked, this, &cancelButtonClicked);//Cancel按钮点击和处理函数的连接
connect(sharpenSlider, &QSlider::valueChanged, this, &MySharpenDialog::sharpenValueChanged);
}
//色调声明函数
void MySharpenDialog::sharpenValueChanged()
{
int width = rawImage->width(); // 图像宽
int height = rawImage->height(); // 图像高
int value = sharpenSlider->value();
int temperatureDelta = value; // 温度变化值,范围在-50到50之间
sharpenAdjustLabel->setText(QString::number(temperatureDelta));
timer.start(); // 计时器开始计时
for (int i = 0; i < height; i++)
{
uchar *rawData = rawImage->scanLine(i); // 获取原图像第 i 行的首像素指针
uchar *newData = newImage.scanLine(i); // 获取 newImage 图像第 i 行的首像素指针
for (int j = 0; j < width; j++)
{
int r = rawData[2]; // 红色通道值
int g = rawData[1]; // 绿色通道值
int b = rawData[0]; // 蓝色通道值
// 调整红色和绿色通道值以改变色温
r = qBound(0, r + temperatureDelta, 255);
g = qBound(0, g + temperatureDelta, 255);
newData[0] = b; // 保持蓝色通道值不变
newData[1] = g; // 更新绿色通道值
newData[2] = r; // 更新红色通道值
rawData += 4; // 因为是 ARGB 格式,所以跳 4 个字节到下一个像素
newData += 4; // 因为是 ARGB 格式,所以跳 4 个字节到下一个像素
}
}
scene->update(); // 在场景上更新显示 newImage
this->setWindowTitle(QString::number(width) + " x " + QString::number(height) + " - " + QString::number(timer.nsecsElapsed() / 1000000.0, 'f', 1) + "ms");
};
void MySharpenDialog::okButtonClicked()
{
this->accept();//OK按钮点击以后发出Accepted返回值
}
void MySharpenDialog::resetButtonClicked()
{
sharpenSlider->setValue(0);//设置亮度滑动条值为0
}
void MySharpenDialog::cancelButtonClicked()
{
this->reject();//OK按钮点击以后发出Rejected返回值
}
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//尺寸调整对话框类
class MyResizeDialog : public QDialog
{
public:
MyResizeDialog(QWidget *parent = 0);
QSize getNewSize() const;
private:
QLabel *widthLabel;
QLabel *heightLabel;
QLineEdit *widthLineEdit;
QLineEdit *heightLineEdit;
QPushButton *okButton;
QPushButton *cancelButton;
QSize newSize;
void initUI();
void updateSize();
};
//尺寸调整构造函数
MyResizeDialog::MyResizeDialog(QWidget *parent) : QDialog(parent)
{
initUI();
updateSize();
connect(widthLineEdit, &QLineEdit::textChanged, this, &MyResizeDialog::updateSize);
connect(heightLineEdit, &QLineEdit::textChanged, this, &MyResizeDialog::updateSize);
connect(okButton, &QPushButton::clicked, this, &QDialog::accept);
connect(cancelButton, &QPushButton::clicked, this, &QDialog::reject);
}
//尺寸调整声明函数
void MyResizeDialog::initUI()
{
widthLabel = new QLabel("Width:");
heightLabel = new QLabel("Height:");
widthLineEdit = new QLineEdit;
heightLineEdit = new QLineEdit;
okButton = new QPushButton("OK");
cancelButton = new QPushButton("Cancel");
QFormLayout *layout = new QFormLayout;
layout->addRow(widthLabel, widthLineEdit);
layout->addRow(heightLabel, heightLineEdit);
layout->addRow(okButton, cancelButton);
setLayout(layout);
}
void MyResizeDialog::updateSize()
{
bool okWidth, okHeight;
int width = widthLineEdit->text().toInt(&okWidth);
int height = heightLineEdit->text().toInt(&okHeight);
if (okWidth && okHeight)
{
newSize = QSize(width, height);
}
else
{
newSize = QSize();
}
}
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//自定义MainWindow类,继承自QMainWindow,作为主类,在main函数中创建对象并显示
class MainWindow : public QMainWindow //定义一个MyMainWindow类,继承自QMainWindow窗口类
{
public:
MainWindow(QWidget *parent = 0); //构造函数,参数是父窗体指针,默认是0,即没有父窗口
private:
QMenuBar *menuBar = new QMenuBar; //定义菜单栏对象指针menuBar
QAction *BCAdjustAction;
QAction *grayscaleAction;
QAction *resizeimageAction;
QAction *rotationAction;
QAction *mirrorAction;
QAction *saturateAction;
QAction *saveAction;
QAction *sharpenAction;
QAction *blurAction;
QAction *fadeAction;
QAction *highlightAction;
QAction *grainAction;
QToolBar *hToolBar = new QToolBar; //定义工具栏对象指针toolBar
QToolButton *toolButtonOpen = new QToolButton; //定义一个工具栏按钮指针
QToolButton *toolButtonAdjust = new QToolButton; //定义一个工具栏按钮指针
QToolButton *toolButtonRestore = new QToolButton; //定义一个工具栏按钮指针
QToolButton *toolButtonResizeImage = new QToolButton; //定义一个工具栏按钮指针
QToolButton *toolButtonRotation = new QToolButton; //定义一个工具栏按钮指针
QToolButton *toolButtonMirror = new QToolButton; //定义一个工具栏按钮指针
QToolButton *toolButtonSaturate = new QToolButton; //定义一个工具栏按钮指针
QToolButton *toolButtonSave = new QToolButton; //定义一个工具栏按钮指针
QToolButton *toolButtonSharpen = new QToolButton; //定义一个工具栏按钮指针
QToolButton *toolButtonBlur = new QToolButton; //定义一个工具栏按钮指针
QToolButton *toolButtonFade = new QToolButton; //定义一个工具栏按钮指针
QToolButton *toolButtonHighlight = new QToolButton; //定义一个工具栏按钮指针
QToolButton *toolButtonGrain = new QToolButton; //定义一个工具栏按钮指针
MyGraphicsView *view = new MyGraphicsView; //定义一个MyGraphicsView对象指针
MyGraphicsScene *scene = new MyGraphicsScene;
QImage originalImage;//原图,用于一键复原。
QImage mainImage;//用于图像处理和显示,所有的处理都在此图像上进行。
void openFile(); //点击open菜单项时的处理函数
void grayscale();
void adjustBrightnessContrast();
void restore();
QStatusBar *statusBar; // 增添状态栏指针
void updateImageInfo();
QString mainImageFileName;
void resizeImage();
void rorate();
void mirrorImage();
void saturate();
void save();
void sharpen();
void blur();
void fade();
void highlight();
void grain();
};
//定义构造函数,参数是父窗体指针,先调用基类构造函数QMainWindow(parent)
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent)
{
this->setMinimumSize(320,200);
this->setWindowTitle("Cute Imager"); //设置窗体的标题
this->setWindowIcon(QPixmap(QApplication::applicationDirPath()+"/image.png"));//设置本应用程序目录下的image.png为窗口图标
this->resize(960, 600); //变更窗体尺寸
this->setMenuBar(menuBar); //为menuBar设置为本窗口的菜单栏
QMenu *fileMenu = new QMenu("File"); //文件菜单
QMenu *editMenu = new QMenu("Edit"); //编辑菜单
//为fileMenu和editMenu添加菜单项。
fileMenu->addAction(QPixmap(QApplication::applicationDirPath()+"/open.png"), "Open", this, &openFile); //追加Action,设置本应用程序目录下的open.png为图标
grayscaleAction=editMenu->addAction(QPixmap(QApplication::applicationDirPath()+"/grayscale.png"), "Grayscale", this, &grayscale); //追加Action到edit菜单
grayscaleAction->setEnabled(false);
BCAdjustAction=editMenu->addAction(QPixmap(QApplication::applicationDirPath()+"/BCAdjust.png"), "Brightness/Contrast", this, &adjustBrightnessContrast); //追加Action到edit菜单
BCAdjustAction->setEnabled(false);
resizeimageAction=editMenu->addAction(QPixmap(QApplication::applicationDirPath()+"/resizeimage.png"), "ResizeImage", this, &resizeImage); //追加Action到edit菜单
resizeimageAction->setEnabled(false);
rotationAction=editMenu->addAction(QPixmap(QApplication::applicationDirPath()+"/roration.png"), "Rotation", this, &rorate); //追加Action到edit菜单
rotationAction->setEnabled(false);
mirrorAction=editMenu->addAction(QPixmap(QApplication::applicationDirPath()+"/mirror.png"), "Mirror", this, &mirrorImage); //追加Action到edit菜单
mirrorAction->setEnabled(false);
saturateAction=editMenu->addAction(QPixmap(QApplication::applicationDirPath()+"/saturation.png"), "Saturate", this, &saturate); //追加Action到edit菜单
saturateAction->setEnabled(false);
saveAction=editMenu->addAction(QPixmap(QApplication::applicationDirPath()+"/save.png"), "Save", this, &save); //追加Action到edit菜单
saveAction->setEnabled(false);
sharpenAction=editMenu->addAction(QPixmap(QApplication::applicationDirPath()+"/sharpen.png"), "Sharpen", this, &sharpen); //追加Action到edit菜单
sharpenAction->setEnabled(false);
blurAction=editMenu->addAction(QPixmap(QApplication::applicationDirPath()+"/blur.png"), "Blur", this, &blur); //追加Action到edit菜单
blurAction->setEnabled(false);
fadeAction=editMenu->addAction(QPixmap(QApplication::applicationDirPath()+"/fade.png"), "Fade", this, &fade); //追加Action到edit菜单
fadeAction->setEnabled(false);
highlightAction=editMenu->addAction(QPixmap(QApplication::applicationDirPath()+"/highlight.png"), "Hue", this, &highlight); //追加Action到edit菜单
highlightAction->setEnabled(false);
grainAction=editMenu->addAction(QPixmap(QApplication::applicationDirPath()+"/grain.png"), "Grain", this, &grain); //追加Action到edit菜单
grainAction->setEnabled(false);
menuBar->addMenu(fileMenu); //将fileMenu菜单添加到菜单栏
menuBar->addMenu(editMenu); //将editMenu菜单添加到菜单栏
this->addToolBar(Qt::TopToolBarArea, hToolBar); //添加toolBar工具栏到本窗口上部
toolButtonOpen->setIcon(QPixmap(QApplication::applicationDirPath()+"/open.png")); //设置本应用程序目录下的open.png为按钮的图标
toolButtonOpen->setToolTip("Open"); //设置提示信息为"Open"
connect(toolButtonOpen, &QToolButton::clicked, this, &openFile);
toolButtonAdjust->setEnabled(false); //定义一个工具栏按钮指针
toolButtonAdjust->setIcon(QPixmap(QApplication::applicationDirPath()+"/BCAdjust.png")); //设置本应用程序目录下的BCAdjust.png为按钮的图标
toolButtonAdjust->setToolTip("Brightness/Contrast"); //设置提示信息为"Brightness/Contrast"
connect(toolButtonAdjust, &QToolButton::clicked, this, &adjustBrightnessContrast);
toolButtonRestore->setEnabled(false); //定义一个工具栏按钮指针
toolButtonRestore->setIcon(QPixmap(QApplication::applicationDirPath()+"/restore.png")); //设置本应用程序目录下的restore.png为按钮的图标
toolButtonRestore->setToolTip(cn("restore")); //设置提示信息为"一键复原"
connect(toolButtonRestore, &QToolButton::clicked, this, &restore);
toolButtonResizeImage->setEnabled(false); //定义一个工具栏按钮指针
toolButtonResizeImage->setIcon(QPixmap(QApplication::applicationDirPath()+"/resizeimage.png")); //设置本应用程序目录下的restore.png为按钮的图标
toolButtonResizeImage->setToolTip(cn("ResizeImage")); //设置提示信息为"一键复原"
connect(toolButtonResizeImage, &QToolButton::clicked, this, &resizeImage);
toolButtonRotation->setEnabled(false); //定义一个工具栏按钮指针
toolButtonRotation->setIcon(QPixmap(QApplication::applicationDirPath()+"/roration.png")); //设置本应用程序目录下的restore.png为按钮的图标
toolButtonRotation->setToolTip(cn("Rorate")); //设置提示信息为"一键复原"
connect(toolButtonRotation, &QToolButton::clicked, this, &rorate);
toolButtonMirror->setEnabled(false); //定义一个工具栏按钮指针
toolButtonMirror->setIcon(QPixmap(QApplication::applicationDirPath()+"/mirror.png")); //设置本应用程序目录下的restore.png为按钮的图标
toolButtonMirror->setToolTip(cn("Mirror")); //设置提示信息为"一键复原"
connect(toolButtonMirror, &QToolButton::clicked, this, &mirrorImage);
toolButtonSaturate->setEnabled(false); //定义一个工具栏按钮指针
toolButtonSaturate->setIcon(QPixmap(QApplication::applicationDirPath()+"/saturation.png")); //设置本应用程序目录下的restore.png为按钮的图标
toolButtonSaturate->setToolTip(cn("Saturate")); //设置提示信息为"一键复原"
connect(toolButtonSaturate, &QToolButton::clicked, this, &saturate);
toolButtonSave->setEnabled(false); //定义一个工具栏按钮指针
toolButtonSave->setIcon(QPixmap(QApplication::applicationDirPath()+"/save.png")); //设置本应用程序目录下的restore.png为按钮的图标
toolButtonSave->setToolTip(cn("Save")); //设置提示信息为"一键复原"
connect(toolButtonSave, &QToolButton::clicked, this, &save);
toolButtonSharpen->setEnabled(false); //定义一个工具栏按钮指针
toolButtonSharpen->setIcon(QPixmap(QApplication::applicationDirPath()+"/sharpen.png")); //设置本应用程序目录下的restore.png为按钮的图标
toolButtonSharpen->setToolTip(cn("Sharpen")); //设置提示信息为"一键复原"
connect(toolButtonSharpen, &QToolButton::clicked, this, &sharpen);
toolButtonBlur->setEnabled(false); //定义一个工具栏按钮指针
toolButtonBlur->setIcon(QPixmap(QApplication::applicationDirPath()+"/blur.png")); //设置本应用程序目录下的restore.png为按钮的图标
toolButtonBlur->setToolTip(cn("Blur")); //设置提示信息为"一键复原"
connect(toolButtonBlur, &QToolButton::clicked, this, &blur);
toolButtonFade->setEnabled(false); //定义一个工具栏按钮指针
toolButtonFade->setIcon(QPixmap(QApplication::applicationDirPath()+"/fade.png")); //设置本应用程序目录下的restore.png为按钮的图标
toolButtonFade->setToolTip(cn("Fade")); //设置提示信息为"一键复原"
connect(toolButtonFade, &QToolButton::clicked, this, &fade);
toolButtonHighlight->setEnabled(false); //定义一个工具栏按钮指针
toolButtonHighlight->setIcon(QPixmap(QApplication::applicationDirPath()+"/highlight.png")); //设置本应用程序目录下的restore.png为按钮的图标
toolButtonHighlight->setToolTip(cn("Hue")); //设置提示信息为"一键复原"
connect(toolButtonHighlight, &QToolButton::clicked, this, &highlight);
toolButtonGrain->setEnabled(false); //定义一个工具栏按钮指针
toolButtonGrain->setIcon(QPixmap(QApplication::applicationDirPath()+"/grain.png")); //设置本应用程序目录下的restore.png为按钮的图标
toolButtonGrain->setToolTip(cn("Grain")); //设置提示信息为"一键复原"
connect(toolButtonGrain, &QToolButton::clicked, this, &grain);
hToolBar->addWidget(toolButtonSave); //将toolButtonAdjust工具栏按钮添加到toolBar
hToolBar->setIconSize(QSize(24, 24)); //设置工具栏图标的尺寸,单位为像素
hToolBar->addWidget(toolButtonOpen); //将toolButtonOpen工具栏按钮添加到toolBar
hToolBar->addWidget(toolButtonAdjust); //将toolButtonAdjust工具栏按钮添加到toolBar
hToolBar->addWidget(toolButtonRestore); //将toolButtonAdjust工具栏按钮添加到toolBar
hToolBar->addWidget(toolButtonResizeImage); //将toolButtonAdjust工具栏按钮添加到toolBar
hToolBar->addWidget(toolButtonRotation); //将toolButtonAdjust工具栏按钮添加到toolBar
hToolBar->addWidget(toolButtonMirror); //将toolButtonAdjust工具栏按钮添加到toolBar
hToolBar->addWidget(toolButtonSaturate); //将toolButtonAdjust工具栏按钮添加到toolBar
hToolBar->addWidget(toolButtonSharpen); //将toolButtonAdjust工具栏按钮添加到toolBar
hToolBar->addWidget(toolButtonBlur); //将toolButtonAdjust工具栏按钮添加到toolBar
hToolBar->addWidget(toolButtonFade); //将toolButtonAdjust工具栏按钮添加到toolBar
hToolBar->addWidget(toolButtonHighlight); //将toolButtonAdjust工具栏按钮添加到toolBar
hToolBar->addWidget(toolButtonGrain); //将toolButtonAdjust工具栏按钮添加到toolBar
view->setScene(scene); //设置view视窗的场景
this->setCentralWidget(view); //将view视窗作为主类窗口的中心窗口,用于显示图像。
statusBar = new QStatusBar(this); // 创建状态栏
setStatusBar(statusBar); // 设置状态栏
connect(scene, &MyGraphicsScene::sceneRectChanged, this, &MainWindow::updateImageInfo);
updateImageInfo();
}
QSize MyResizeDialog::getNewSize() const
{
return newSize;
}
//调整尺寸
void MainWindow::resizeImage()
{
MyResizeDialog resizeDialog(this);
if (resizeDialog.exec() == QDialog::Accepted)
{
QSize newSize = resizeDialog.getNewSize();
mainImage = mainImage.scaled(newSize); // 使用 QImage 的 scaled 函数调整图像尺寸
scene->setShowImage(&mainImage);
scene->setSceneRect(mainImage.rect());
scene->update();
}
}
//旋转
void MainWindow::rorate()
{
if (mainImage.isNull()) return;//如果rawImage是空图像,返回
MyRorateDialog myRorateDialog(&mainImage, this);//定义亮度对比度对话框,将mainImage的指针传递给对话框作为原图像。
if (myRorateDialog.exec() == QDialog::Accepted) //显示对话框,如果是点击了OK按钮
{
mainImage = myRorateDialog.getNewImage().copy(); //将对话框场景的newImage复制一份给本窗口场景的mainImage
scene->setShowImage(&mainImage);//将mainImage的指针赋给场景的图形指针showImage,用于描画显示。
scene->setSceneRect(mainImage.rect()); //设置场景坐标范围为图像尺寸,这样视图view可以看到图像全部并能正确缩放。
scene->update();//更新场景,将showImage,也就是mainImage描画出来。
}
}
//色相
void MainWindow::highlight()
{
if (mainImage.isNull()) return;//如果rawImage是空图像,返回
MyHighlightDialog myHighlightDialog(&mainImage, this);//定义亮度对比度对话框,将mainImage的指针传递给对话框作为原图像。
if (myHighlightDialog.exec() == QDialog::Accepted) //显示对话框,如果是点击了OK按钮
{
mainImage = myHighlightDialog.getNewImage().copy(); //将对话框场景的newImage复制一份给本窗口场景的mainImage
scene->setShowImage(&mainImage);//将mainImage的指针赋给场景的图形指针showImage,用于描画显示。
scene->setSceneRect(mainImage.rect()); //设置场景坐标范围为图像尺寸,这样视图view可以看到图像全部并能正确缩放。
scene->update();//更新场景,将showImage,也就是mainImage描画出来。
}
}
//褪色
void MainWindow::fade()
{
if (mainImage.isNull()) return;//如果rawImage是空图像,返回
MyFadeDialog myFadeDialog(&mainImage, this);//定义亮度对比度对话框,将mainImage的指针传递给对话框作为原图像。
if (myFadeDialog.exec() == QDialog::Accepted) //显示对话框,如果是点击了OK按钮
{
mainImage = myFadeDialog.getNewImage().copy(); //将对话框场景的newImage复制一份给本窗口场景的mainImage
scene->setShowImage(&mainImage);//将mainImage的指针赋给场景的图形指针showImage,用于描画显示。
scene->setSceneRect(mainImage.rect()); //设置场景坐标范围为图像尺寸,这样视图view可以看到图像全部并能正确缩放。
scene->update();//更新场景,将showImage,也就是mainImage描画出来。
}
}
//色调
void MainWindow::sharpen()
{
if (mainImage.isNull()) return;//如果rawImage是空图像,返回
MySharpenDialog mySharpenDialog(&mainImage, this);//定义亮度对比度对话框,将mainImage的指针传递给对话框作为原图像。
if (mySharpenDialog.exec() == QDialog::Accepted) //显示对话框,如果是点击了OK按钮
{
mainImage = mySharpenDialog.getNewImage().copy(); //将对话框场景的newImage复制一份给本窗口场景的mainImage
scene->setShowImage(&mainImage);//将mainImage的指针赋给场景的图形指针showImage,用于描画显示。
scene->setSceneRect(mainImage.rect()); //设置场景坐标范围为图像尺寸,这样视图view可以看到图像全部并能正确缩放。
scene->update();//更新场景,将showImage,也就是mainImage描画出来。
}
}
//饱和度
void MainWindow::saturate()
{
if (mainImage.isNull()) return;//如果rawImage是空图像,返回
MySaturationDialog mySaturationDialog(&mainImage, this);//定义亮度对比度对话框,将mainImage的指针传递给对话框作为原图像。
if (mySaturationDialog.exec() == QDialog::Accepted) //显示对话框,如果是点击了OK按钮
{
mainImage = mySaturationDialog.getNewImage().copy(); //将对话框场景的newImage复制一份给本窗口场景的mainImage
scene->setShowImage(&mainImage);//将mainImage的指针赋给场景的图形指针showImage,用于描画显示。
scene->setSceneRect(mainImage.rect()); //设置场景坐标范围为图像尺寸,这样视图view可以看到图像全部并能正确缩放。
scene->update();//更新场景,将showImage,也就是mainImage描画出来。
}
}
//模糊
void MainWindow::blur()
{
if (mainImage.isNull()) return;//如果rawImage是空图像,返回
MyBlurDialog myBlurDialog(&mainImage, this);//定义亮度对比度对话框,将mainImage的指针传递给对话框作为原图像。
if (myBlurDialog.exec() == QDialog::Accepted) //显示对话框,如果是点击了OK按钮
{
mainImage = myBlurDialog.getNewImage().copy(); //将对话框场景的newImage复制一份给本窗口场景的mainImage
scene->setShowImage(&mainImage);//将mainImage的指针赋给场景的图形指针showImage,用于描画显示。
scene->setSceneRect(mainImage.rect()); //设置场景坐标范围为图像尺寸,这样视图view可以看到图像全部并能正确缩放。
scene->update();//更新场景,将showImage,也就是mainImage描画出来。
}
}
//颗粒
void MainWindow::grain()
{
if (mainImage.isNull()) return;//如果rawImage是空图像,返回
MyGrainDialog myGrainDialog(&mainImage, this);//定义亮度对比度对话框,将mainImage的指针传递给对话框作为原图像。
if (myGrainDialog.exec() == QDialog::Accepted) //显示对话框,如果是点击了OK按钮
{
mainImage = myGrainDialog.getNewImage().copy(); //将对话框场景的newImage复制一份给本窗口场景的mainImage
scene->setShowImage(&mainImage);//将mainImage的指针赋给场景的图形指针showImage,用于描画显示。
scene->setSceneRect(mainImage.rect()); //设置场景坐标范围为图像尺寸,这样视图view可以看到图像全部并能正确缩放。
scene->update();//更新场景,将showImage,也就是mainImage描画出来。
}
}
//显示状态
void MainWindow::updateImageInfo()//显示状态栏信息函数
{
QString info;
if (!mainImage.isNull())
{
QFileInfo fileInfo(mainImageFileName); // 使用QFileInfo获取文件信息
QString fileName = fileInfo.fileName();
int width = mainImage.width();
int height = mainImage.height();
// 根据需要构建显示信息的字符串
info = QString("File: %1 | Size: %2 x %3").arg(fileName).arg(width).arg(height);
}
statusBar->showMessage(info); // 在状态栏显示信息
}
//打开文件
void MainWindow::openFile()//打开文件函数
{
QString fileName = QFileDialog::getOpenFileName(this, "Open", "", "Images(*.jpg *.bmp *.png *.ico *.gif *.tif);;All Files(*.*)");
if (fileName == "") return;//如果没有打开文件,返回。
mainImage = QImage(fileName);
mainImageFileName = fileName; // 保存文件名
originalImage = mainImage.copy();
if (mainImage.isNull()) //如果创建rawImage失败,rawImage是空对象
{
scene->setShowImage(&mainImage); //将视图下面场景的showImage也设置为空图像指针
scene->setSceneRect(0, 0, 0, 0); //设置场景坐标范围为0
toolButtonAdjust->setEnabled(false); //对比度亮度调整按钮失效
BCAdjustAction->setEnabled(false); //对比度亮度调整菜单项失效
grayscaleAction->setEnabled(false); //灰度菜单项失效
toolButtonRestore->setEnabled(false); //一键复原按钮失效
toolButtonResizeImage->setEnabled(false); //一键复原按钮失效
toolButtonRotation->setEnabled(false); //一键复原按钮失效
toolButtonMirror->setEnabled(false); //一键复原按钮失效
toolButtonSaturate->setEnabled(false);
toolButtonSave->setEnabled(false);
toolButtonSharpen->setEnabled(false);
toolButtonBlur->setEnabled(false);
toolButtonFade->setEnabled(false);
toolButtonHighlight->setEnabled(false);
toolButtonGrain->setEnabled(false);
QMessageBox::warning(this, "Media file open", "Invalid image format!");
return; //返回
}
//如果不是RGB32格式,转换成该格式。本程序只对ARGB32格式的图像数据进行处理,如果需要处理其它格式的图像,请修改此处。
scene->setShowImage(&mainImage); //将mainImage的指针给场景的showImage,用于图像显示。每次图像处理以后得到新mainImage,必须执行此语句,并执行scene->update()用于界面刷新。
scene->setSceneRect(mainImage.rect()); //设置场景坐标范围为图像尺寸,这样视图view可以看到图像全部。
view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio); //使场景适当缩放保持纵横比充满整个视图窗口
toolButtonAdjust->setEnabled(true); //设置亮度对比度调整按钮激活状态
toolButtonRestore->setEnabled(true); //设置一键复原按钮激活状态
toolButtonResizeImage->setEnabled(true); //设置一键复原按钮激活状态
toolButtonRotation->setEnabled(true); //设置一键复原按钮激活状态
toolButtonMirror->setEnabled(true); //设置一键复原按钮激活状态
toolButtonSaturate->setEnabled(true); //设置一键复原按钮激活状态
toolButtonSave->setEnabled(true); //设置一键复原按钮激活状态
toolButtonSharpen->setEnabled(true); //设置一键复原按钮激活状态
toolButtonBlur->setEnabled(true); //设置一键复原按钮激活状态
toolButtonFade->setEnabled(true); //设置一键复原按钮激活状态
toolButtonHighlight->setEnabled(true); //设置一键复原按钮激活状态
toolButtonGrain->setEnabled(true); //设置一键复原按钮激活状态
BCAdjustAction->setEnabled(true); //设置亮度对比度调整菜单激活状态
grayscaleAction->setEnabled(true); //设置灰度菜单项激活状态
resizeimageAction->setEnabled(true); //设置灰度菜单项激活状态
rotationAction->setEnabled(true); //设置灰度菜单项激活状态
mirrorAction->setEnabled(true); //设置灰度菜单项激活状态
saturateAction->setEnabled(true); //设置灰度菜单项激活状态
saveAction->setEnabled(true); //设置灰度菜单项激活状态
sharpenAction->setEnabled(true); //设置灰度菜单项激活状态
blurAction->setEnabled(true); //设置灰度菜单项激活状态
fadeAction->setEnabled(true); //设置灰度菜单项激活状态
highlightAction->setEnabled(true); //设置灰度菜单项激活状态
grainAction->setEnabled(true); //设置灰度菜单项激活状态
updateImageInfo();
}
//灰度
void MainWindow::grayscale()//计算灰度函数
{
int width=mainImage.width(); //图像宽
int height=mainImage.height();//图像高
uchar r,g,b,grayScale; //r代表red,g代表green,b代表blue,范围0~255
for (int i=0;i
{
uchar *data=mainImage.scanLine(i); //获取图像第i行首像素的指针
for (int j=0;j
{
b=data[0]; g=data[1]; r=data[2]; //获得三个子像素值
grayScale=(r*30+g*59+b*11)/100; //计算得到灰度值
data[0]=data[1]=data[2]=grayScale; //让blue,green,red子像素的值都等于灰度值
data+=4;//因为是ARGB格式,需要跳过4个字节的位置,到下一个像素。
}
}
scene->setShowImage(&mainImage);//将mainImage的指针赋给场景的图形指针showImage,用于描画显示。
scene->setSceneRect(mainImage.rect()); //设置场景坐标范围为图像尺寸,这样视图view可以看到图像全部并能正确缩放。
scene->update();//更新场景,将showImage,也就是mainImage描画出来。
}
//保存
void MainWindow::save()
{
QString filePath = QFileDialog::getSaveFileName(this, "Save Image", "", "Images (*.png *.jpg *.bmp)");
if (filePath.isEmpty()) return; // 如果用户取消保存操作,直接返回
if (mainImage.save(filePath))
{
QMessageBox::information(this, "Save Image", "Image saved successfully!");
}
else
{
QMessageBox::warning(this, "Save Image", "Failed to save image!");
}
}
//镜像
void MainWindow::mirrorImage()
{
if (!mainImage.isNull()) {
mainImage = mainImage.mirrored(true, false);
scene->setShowImage(&mainImage);
scene->setSceneRect(mainImage.rect());
scene->update();
}
}
//亮度对比度
void MainWindow::adjustBrightnessContrast()
{
if (mainImage.isNull()) return;//如果rawImage是空图像,返回
MyDialog myDialog(&mainImage, this);//定义亮度对比度对话框,将mainImage的指针传递给对话框作为原图像。
if (myDialog.exec() == QDialog::Accepted) //显示对话框,如果是点击了OK按钮
{
mainImage = myDialog.getNewImage().copy(); //将对话框场景的newImage复制一份给本窗口场景的mainImage
scene->setShowImage(&mainImage);//将mainImage的指针赋给场景的图形指针showImage,用于描画显示。
scene->setSceneRect(mainImage.rect()); //设置场景坐标范围为图像尺寸,这样视图view可以看到图像全部并能正确缩放。
scene->update();//更新场景,将showImage,也就是mainImage描画出来。
}
}
//复原
void MainWindow::restore()
{
mainImage=originalImage.copy();
scene->setShowImage(&mainImage);//将mainImage的指针赋给场景的图形指针showImage,用于描画显示。
scene->setSceneRect(mainImage.rect()); //设置场景坐标范围为图像尺寸,这样视图view可以看到图像全部并能正确缩放。
scene->update();//更新场景,将showImage,也就是mainImage描画出来。
}
int main(int argc, char *argv[])
{
QApplication::setStyle(QStyleFactory::create("fusion")); //设置所有控件样式为fusion风格
QApplication app(argc, argv); //创建一个程序
//app.setFont(QFont("Arial, Microsoft Yahei",12));
MainWindow mainWindow; //创建一个主窗口MyCuteImager对象
mainWindow.show(); //显示主窗口对象。
return app.exec(); //开启程序消息循环
}
总结
代码敲写不易,感谢支持。好好学习,天天向上。
好文链接