-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectFiles.cpp
157 lines (128 loc) · 3.82 KB
/
selectFiles.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
#include "mainwindow.h"
#include "QFileDialog"
#include <iostream>
void MainWindow::combineImages() {
combinedPyr = ImagePyramid(
leftPyr, rightPyr,
imageMask(
leftPyr.getWidth(), leftPyr.getHeight(),
ui->startSlider->value(),
ui->endSlider->value()));
displayImages();
}
/**
* @brief MainWindow::handleLeftFileButton
* Shows a file dialog to select an image attempts to load it as the left image.
*/
void MainWindow::handleLeftFileButton() {
QString path = QFileDialog::getOpenFileName(
this, tr("Select Left Image"), "", imageFileFilter
);
// Only attempt loading if a file was selected
if (path != emptyMsg) {
ui->leftFileText->setText(path);
submitLeftImage();
}
}
/**
* @brief MainWindow::handleRightFileButton
* Shows a file dialog to select an image attempts to load it as the right image.
*/
void MainWindow::handleRightFileButton() {
QString path = QFileDialog::getOpenFileName(
this, tr("Select Right Image"), "", imageFileFilter
);
// Only attempt loading if a file was selected
if (path != emptyMsg) {
ui->rightFileText->setText(path);
submitRightImage();
}
}
/**
* @brief MainWindow::handleLeftFileSubmit
* Attempts to read an image from the path in the text line and
* use it for merging with the right image.
*/
void MainWindow::submitLeftImage() {
// Read string from text line
QString path = ui->leftFileText->text();
Mat leftImage;
// Attempt loading an image, display error message if error
switch (loadImage(leftImage, path)) {
case 0: // no error
setLeftErrorMessage(emptyMsg);
break;
case 1: // empty path
setLeftErrorMessage(emptyPathMsg);
break;
case 2: // Could not read file
setLeftErrorMessage(imageInvalidMsg);
default: // unknown error, probably invalid image
setLeftErrorMessage(imageInvalidMsg);
break;
}
// set the image
leftPyr.setImage(leftImage);
// set right image size
rightPyr.setSize(leftPyr.getSize());
combineImages();
displayImages();
}
void MainWindow::submitRightImage() {
QString path = ui->rightFileText->text();
Mat rightImage;
// Attempt loading an image, display error message if error
switch (loadImage(rightImage, path)) {
case 0: // no error
setRightErrorMessage(emptyMsg);
break;
case 1: // empty path
setRightErrorMessage(emptyPathMsg);
break;
case 2: // Could not read file because it ended up empty
setRightErrorMessage(imageInvalidMsg);
default: // Invalid image
setRightErrorMessage(imageInvalidMsg);
break;
}
// set the image without changing the size used
rightPyr.setImage(rightImage, false);
combineImages();
displayImages();
}
/**
* @brief MainWindow::setLeftErrorMessage
* @param msg the message to set
*/
void MainWindow::setLeftErrorMessage(QString msg) {
ui->leftErrorMessage->setText(msg);
}
/**
* @brief MainWindow::setRightErrorMessage
* @param msg the message to set
*/
void MainWindow::setRightErrorMessage(QString msg) {
ui->rightErrorMessage->setText(msg);
}
int MainWindow::loadImage(Mat &dst, QString path) {
if (path == emptyMsg) {
return 1;
}
// read in the images from resource
QFile file(path);
// Load image
Mat image;
if (file.open(QIODevice::ReadOnly)){
qint64 sz = file.size();
std::vector<uchar> buf(sz);
file.read((char*)buf.data(), sz);
image = imdecode(buf, IMREAD_COLOR);
}
// Check if image was read correctly (not empty)
if (image.empty()) {
return 2;
}
//
image.convertTo(dst, CV_8U);
return 0;
}