-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
211 lines (175 loc) · 6.87 KB
/
Copy pathmainwindow.cpp
File metadata and controls
211 lines (175 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setupUI();
setupInputOutputNodes();
// Add initial processing node
addNode();
}
void MainWindow::setupUI() {
centralWidget = new QWidget(this);
mainLayout = new QVBoxLayout(centralWidget);
// Input/Output section
QHBoxLayout *ioLayout = new QHBoxLayout();
// Input
QVBoxLayout *inputLayout = new QVBoxLayout();
inputButton = new QPushButton("Select Input Image", centralWidget);
inputPreview = new QLabel(centralWidget);
inputPreview->setAlignment(Qt::AlignCenter);
inputPreview->setMinimumSize(200, 150);
inputPreview->setStyleSheet("border: 1px solid gray;");
inputLayout->addWidget(inputButton);
inputLayout->addWidget(inputPreview);
ioLayout->addLayout(inputLayout);
// Output
QVBoxLayout *outputLayout = new QVBoxLayout();
outputButton = new QPushButton("Select Output File", centralWidget);
outputPreview = new QLabel(centralWidget);
outputPreview->setAlignment(Qt::AlignCenter);
outputPreview->setMinimumSize(200, 150);
outputPreview->setStyleSheet("border: 1px solid gray;");
outputLayout->addWidget(outputButton);
outputLayout->addWidget(outputPreview);
ioLayout->addLayout(outputLayout);
mainLayout->addLayout(ioLayout);
// Processing nodes
nodesLayout = new QVBoxLayout();
spacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
// Buttons
buttonLayout = new QHBoxLayout();
addButton = new QPushButton("Add Node", centralWidget);
renderButton = new QPushButton("Render", centralWidget);
buttonLayout->addWidget(addButton);
buttonLayout->addWidget(renderButton);
// Add layouts to main layout
mainLayout->addLayout(buttonLayout);
mainLayout->addLayout(nodesLayout);
mainLayout->addItem(spacer);
// Set central widget
setCentralWidget(centralWidget);
// Connections
connect(addButton, &QPushButton::clicked, this, &MainWindow::addNode);
connect(renderButton, &QPushButton::clicked, this, &MainWindow::renderImage);
connect(inputButton, &QPushButton::clicked, this, &MainWindow::browseInputImage);
connect(outputButton, &QPushButton::clicked, this, &MainWindow::selectOutputFile);
}
void MainWindow::setupInputOutputNodes() {
// These are fixed and can't be removed
// The actual processing nodes will be added between them
}
void MainWindow::addNode() {
Node *node = new Node(centralWidget);
nodes.append(node);
nodesLayout->insertWidget(nodesLayout->count() - 1, node); // Insert before spacer
connect(node, &Node::deleteRequested, this, &MainWindow::removeNode);
connect(node, &Node::functionChanged, this, &MainWindow::updatePreview);
}
void MainWindow::removeNode(Node* node) {
nodes.removeOne(node);
node->deleteLater();
updatePreview();
}
void MainWindow::browseInputImage() {
QString filePath = QFileDialog::getOpenFileName(this,
"Select Input Image",
QDir::homePath(),
"Images (*.png *.jpg *.jpeg *.bmp)");
if (!filePath.isEmpty()) {
inputFilePath = filePath;
originalImage = ImageProcessor::loadImage(filePath);
// Update preview
QImage qimg(originalImage.data, originalImage.cols, originalImage.rows,
originalImage.step, QImage::Format_BGR888);
inputPreview->setPixmap(QPixmap::fromImage(qimg).scaled(
inputPreview->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
updatePreview();
}
}
void MainWindow::selectOutputFile() {
QString filePath = QFileDialog::getSaveFileName(this,
"Select Output File",
QDir::homePath(),
"Images (*.png *.jpg *.jpeg *.bmp)");
if (!filePath.isEmpty()) {
outputFilePath = filePath;
}
}
void MainWindow::renderImage() {
if (originalImage.empty()) {
qDebug() << "No input image selected";
return;
}
if (outputFilePath.isEmpty()) {
qDebug() << "No output file selected";
return;
}
processImage();
// Save the result
if (!processedImage.empty()) {
if (ImageProcessor::saveImage(processedImage, outputFilePath)) {
qDebug() << "Image saved successfully to" << outputFilePath;
// Update output preview
QImage qimg(processedImage.data, processedImage.cols, processedImage.rows,
processedImage.step, processedImage.channels() == 1 ?
QImage::Format_Grayscale8 : QImage::Format_BGR888);
outputPreview->setPixmap(QPixmap::fromImage(qimg).scaled(
outputPreview->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
} else {
qDebug() << "Failed to save image";
}
}
}
void MainWindow::processImage() {
if (originalImage.empty()) return;
cv::Mat result = originalImage.clone();
// Apply each node's operation in sequence
for (Node* node : nodes) {
QVariantList params = node->getParameters();
switch(node->currentFunction()) {
case Node::BrightnessContrast:
if (params.size() >= 2) {
result = ImageProcessor::applyBrightnessContrast(
result, params[0].toDouble(), params[1].toInt());
}
break;
case Node::GaussianBlur:
if (params.size() >= 1) {
result = ImageProcessor::applyGaussianBlur(
result, params[0].toInt());
}
break;
case Node::Threshold:
if (params.size() >= 1) {
result = ImageProcessor::applyThreshold(
result, params[0].toDouble());
}
break;
case Node::GaussianNoise:
if (params.size() >= 1) {
result = ImageProcessor::addGaussianNoise(
result, params[0].toDouble());
}
break;
default:
break;
}
}
processedImage = result;
}
void MainWindow::updatePreview() {
if (originalImage.empty()) return;
processImage();
// Update output preview with processed image
if (!processedImage.empty()) {
QImage qimg(processedImage.data, processedImage.cols, processedImage.rows,
processedImage.step, processedImage.channels() == 1 ?
QImage::Format_Grayscale8 : QImage::Format_BGR888);
outputPreview->setPixmap(QPixmap::fromImage(qimg).scaled(
outputPreview->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
}
MainWindow::~MainWindow() {
qDeleteAll(nodes);
}