-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
206 lines (158 loc) · 6.27 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include <QFile>
#include <iostream>
using namespace cv;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("Merging 2 images using Laplacian Pyramids");
const QString leftImagePath = ":/images/apple.jpg";
const QString rightImagePath = ":/images/orange.jpg";
// set the text edits to the path
ui->leftFileText->setText(leftImagePath);
ui->rightFileText->setText(rightImagePath);
// Read the images
Mat leftImage, rightImage;
loadImage(leftImage, leftImagePath);
loadImage(rightImage, rightImagePath);
// Set the images
leftPyr.setImage(leftImage);
rightPyr.setImage(rightImage);
// Layers
leftPyr.setLayers(initialLayers);
rightPyr.setLayers(initialLayers);
// Combine them
combineImages();
// Display them
displayImages();
// Connect slider changing values to recombining the images
connect(ui->startSlider, SIGNAL(valueChanged(int)), this, SLOT(combineImages()));
connect(ui->endSlider, SIGNAL(valueChanged(int)), this, SLOT(combineImages()));
// Connect file selection UI to functionality
connect(ui->leftFileButton, SIGNAL(clicked()), this, SLOT(handleLeftFileButton()));
connect(ui->rightFileButton, SIGNAL(clicked()), this, SLOT(handleRightFileButton()));
connect(ui->leftFileSubmit, SIGNAL(clicked()), this, SLOT(submitLeftImage()));
connect(ui->rightFileSubmit, SIGNAL(clicked()), this, SLOT(submitRightImage()));
connect(ui->leftFileText, SIGNAL(returnPressed()), this, SLOT(submitLeftImage()));
connect(ui->rightFileText, SIGNAL(returnPressed()), this, SLOT(submitRightImage()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::displayImages() {
Size imSize = leftPyr.getSize();
// calculate display size of images
int width = imSize.width;
int height = imSize.height;
// if too small, make the image the largest possibls while
// still fitting in the dimensions
if (imSize.width < minDisplayDim.width &&
imSize.width < minDisplayDim.width) {
// If too small, make the image fit inside but barely
if (imSize.width < minDisplayDim.width) {
width = minDisplayDim.width;
height = imSize.height * minDisplayDim.width / imSize.width;
}
if (imSize.height > minDisplayDim.height) {
int w2 = imSize.width * minDisplayDim.height / imSize.height;
if (width > w2) {
width = w2;
height = minDisplayDim.height;
}
}
}
else {
// If too large, make the image fit inside
if (imSize.width > maxDisplayDim.width) {
width = maxDisplayDim.width;
height = imSize.height * maxDisplayDim.width / imSize.width;
}
if (imSize.height > maxDisplayDim.height) {
int w2 = imSize.width * maxDisplayDim.height / imSize.height;
if (width > w2) {
width = w2;
height = maxDisplayDim.height;
}
}
}
Size displaySize = Size(width, height);
// resize the UI components to fit the images if possible
resizeUI(displaySize);
displayImage(ui->leftImageLabel, leftPyr.getResizedImage(displaySize));
displayImage(ui->rightImageLabel, rightPyr.getResizedImage(displaySize));
displayImage(ui->reconstructionLabel, combinedPyr.getResizedImage(displaySize));
// status bar
ui->statusbar->showMessage(
"Layers Used: " + QString::number(leftPyr.getLayers())
+ "\t Image size: " + QString::number(leftPyr.getWidth())
+ " x " + QString::number(leftPyr.getHeight())
);
}
/**
* Displays an opencv image in a QLabel.
*/
void MainWindow::displayImage(QLabel *label, Mat img) {
Mat image;
cv::cvtColor(img, image, CV_BGR2RGB);
label->setPixmap(QPixmap::fromImage((QImage(image.data, image.cols, image.rows, image.step, QImage::Format_RGB888))));
}
/*
* Creates a horizontal gradient mask of type CV_32FC1
*/
Mat MainWindow::imageMask(
int width, int height,
int startPercent, int endPercent
) {
Mat mask;
int start = width * startPercent / 100;
int end = width * endPercent / 100;
if (start > end) {
// If start > end, find mask with start and end swapped, then invert
mask = imageMask(width, height, endPercent, startPercent);
subtract(1, mask, mask);
}
else {
// create array
mask.create(height, width, CV_32FC1);
// fill left and right
rectangle(mask, Point(0, 0), Point(start, height-1), 1.0, FILLED);
rectangle(mask, Point(end, 0), Point(width-1, height-1), 0.0, FILLED);
// generate gradient between start and end cols
for (int col = start; col < end; col++) {
// linear gradient between start and end
float value = (float)(end - col) / (end - start);
mask.col(col).setTo(value);
}
}
return mask;
}
void MainWindow::resizeUI(Size imageDimensions) {
int width = max(imageDimensions.width, minDisplayDim.width);
int height = imageDimensions.height;
int windowWidth = 4*displayGap + 3*width;
int windowHeight = 3*displayGap + height + panelHeight;
// resize window
this->resize(windowWidth, windowHeight);
this->setMinimumWidth(windowWidth);
this->setMinimumHeight(windowHeight);
this->setMaximumWidth(windowWidth);
this->setMaximumHeight(windowHeight);
// Image Display Labels
ui->leftImageLabel->resize(width, height);
ui->reconstructionLabel->resize(width, height);
ui->rightImageLabel->resize(width, height);
ui->leftImageLabel->move(displayGap, displayGap);
ui->reconstructionLabel->move(2*displayGap + width, displayGap);
ui->rightImageLabel->move(3*displayGap + 2*width, displayGap);
// Resize slider panel
ui->sliders->resize(width, panelHeight);
ui->sliders->move(2*displayGap + width, 2 + displayGap + height);
// resize other panels
ui->leftFrame->resize(width, panelHeight);
ui->rightFrame->resize(width, panelHeight);
ui->leftFrame->move(displayGap, 2 * displayGap + height);
ui->rightFrame->move(3*displayGap + 2*width, 2 * displayGap + height);
}