-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
394 lines (342 loc) · 13.8 KB
/
Copy pathmainwindow.cpp
File metadata and controls
394 lines (342 loc) · 13.8 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "mainwindow.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QScrollBar>
#include <iostream>
#include <utility>
#include "ImageUtils.h"
#include "MathUtils.h"
using namespace cv;
using namespace imgutils;
using namespace mathutils;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setupUi(this);
scene = new Scene3D();
frameGL->layout()->addWidget(scene);
scene->setPointSize(10);
progressBar->hide();
rbDAISY->hide();
initModule_nonfree();
}
void MainWindow::addlog(const QString &s)
{
log->insertPlainText(s + "\n");
qApp->processEvents();
}
void MainWindow::addlog(const QString &name, const Mat &m)
{
QString s;
for (int row = 0; row < m.rows; row++) {
for (int col = 0; col < m.cols; col++) {
s += QString::number(m.at<double>(row, col));
if (col < m.cols - 1)
s += " ";
}
if (row < m.rows - 1)
s += "; ";
}
log->insertPlainText(name + ": [" + s + "]\n");
qApp->processEvents();
}
void MainWindow::addlog(const QString &name, const Vec4d &v)
{
QString s;
for (int row = 0; row < v.rows; row++) {
s += QString::number(v[row]);
if (row < v.rows - 1)
s += "; ";
}
log->insertPlainText(name + ": [" + s + "]\n");
qApp->processEvents();
}
void dumpCameraPositionsToObj(const char *fileName, std::vector<cv::Mat> Ts)
{
QFile F(fileName);
F.open(QIODevice::WriteOnly);
QTextStream T(&F);
T << "# All coordinates are with respect to first camera\r\n";
for (int i = 0; i < Ts.size(); i++) {
double *m = (double*)Ts[i].data;
T << QString("v %1 %2 %3\r\n")
.arg(m[0])
.arg(m[1])
.arg(m[2])
.replace(".", ",");
}
F.close();
}
// TODO: consider moving to QJSONDocument api
void dumpResultsToJSON(const char *fileName, QStringList inputFileNames, std::vector<cv::Mat> Rs, std::vector<cv::Mat> Ts)
{
QFile F(fileName);
F.open(QIODevice::WriteOnly);
QTextStream T(&F);
QStringList L;
L << "{";
for (int i = 0; i < Rs.size(); i++) {
double *m = (double*)Rs[i].data;
Vec4d Q = convertRotationMatrixToQuaternion(Rs[i]);
L << QString(" \"%1\": {").arg(inputFileNames[i]);
L << QString(" \"position\": {");
L << QString(" \"x\": %1").arg(Ts[i].at<double>(0, 0));
L << QString(" \"y\": %1").arg(Ts[i].at<double>(1, 0));
L << QString(" \"z\": %1").arg(Ts[i].at<double>(2, 0));
L << QString(" },");
L << QString(" \"OZdirection\": {");
L << QString(" \"x\": %1").arg(Q[1]);
L << QString(" \"y\": %1").arg(Q[2]);
L << QString(" \"z\": %1").arg(Q[3]);
L << QString(" \"w\": %1").arg(Q[0]);
L << QString(" },");
L << QString(" \"OZrotation\": {");
L << QString(" \"R_1\": %1 %2 %3").arg(m[0]).arg(m[1]).arg(m[2]);
L << QString(" \"R_2\": %1 %2 %3").arg(m[3]).arg(m[4]).arg(m[5]);
L << QString(" \"R_3\": %1 %2 %3").arg(m[6]).arg(m[7]).arg(m[8]);
L << QString(" }");
L << QString(" }");
}
L << "}";
T << L.join("\r\n");
F.close();
}
void MainWindow::on_actionOpen_triggered()
{
imFiles = QFileDialog::getOpenFileNames(this, "Raw files");
cmbImage->clear();
cmbImage->addItems(imFiles);
process();
return;
}
void MainWindow::process()
{
if (imFiles.size() < 2)
return;
scene->setCloud(PointCloud());
progressBar->setValue(0);
progressBar->show();
qApp->processEvents();
btnStart->setEnabled(false);
const double scaling = 0.25;
if (!log->toPlainText().isEmpty())
addlog("\n====================================\n");
bool isDebug = chkDebug->isChecked();
int detector = _SURF;//rbSURF->isChecked() ? _SURF : _DAISY;
int refimg = cmbImage->currentIndex();
Mat image0 = imread(imFiles[refimg].toLatin1().data(), CV_LOAD_IMAGE_GRAYSCALE ); // reference image
scene->addPoint(CloudPoint(0, 0, 0, QColor(Qt::gray)));
float startAngle0;
float startAngleCur;
std::vector<Mat> Rs, Ts;
Rs.push_back(Mat::eye(3, 3, CV_64FC1));
Ts.push_back(Mat::zeros(3, 1, CV_64FC1));
int curimg = 0;
addlog(QString("Processing %1 images.").arg(imFiles.size()));
addlog("");
addlog(QString("Reference image: ") + imFiles[refimg]);
addlog("");
cv::Mat cumR = cv::Mat::eye(3, 3, CV_64FC1);
cv::Mat cumT = cv::Mat::zeros(3, 1, CV_64FC1);
Qt::GlobalColor color = Qt::gray;
for (int refimg = 0; refimg < imFiles.size() - 1; refimg++) {
progressBar->setValue(100 / (imFiles.size() - 1) * curimg);
curimg++;
qApp->processEvents();
int ind = refimg + 1;
if (refimg == ind)
continue;
addlog(QString("Image %1: %2").arg(refimg).arg(imFiles[refimg]));
addlog(QString("Image %1: %2").arg(curimg).arg(imFiles[ind]));
image0 = imread(imFiles[refimg].toLatin1().data(), CV_LOAD_IMAGE_GRAYSCALE ); // reference image
Mat imageCur = imread(imFiles[ind].toLatin1().data(), CV_LOAD_IMAGE_GRAYSCALE );
//cv::resize(imageCur, imageCur, Size(0, 0), scaling, scaling);
std::vector<Point2f> pts0, ptsCur;
startAngle0 = -90;
startAngleCur = -90;
QString name1 = imFiles[refimg].mid(imFiles[refimg].lastIndexOf("/") + 1, imFiles[refimg].lastIndexOf(".") - imFiles[refimg].lastIndexOf("/") - 1);
QString name2 = imFiles[ind].mid(imFiles[ind].lastIndexOf("/") + 1, imFiles[ind].lastIndexOf(".") - imFiles[ind].lastIndexOf("/") - 1);
// TODO: check that this is legal or just use source unstitched spherical (eqiangular) images
// get optimal image hemisphere division for images according to matches
if (chkCustomHemisphere->isChecked()) {
Mat image0small, imageCursmall;
cv::resize(image0, image0small, Size(), scaling, scaling, INTER_CUBIC);
cv::resize(imageCur, imageCursmall, Size(), scaling, scaling, INTER_CUBIC);
calculateMatches(image0small, imageCursmall, pts0, ptsCur, detector, sbNndr->value(), isDebug);
cv::Mat mask;
findFundamentalMat(pts0, ptsCur, mask);
for (int i = pts0.size() - 1; i >= 0; i--) {
if (mask.at<unsigned char>(i, 0) == 0) {
pts0.erase(pts0.begin() + i);
ptsCur.erase(ptsCur.begin() + i);
}
}
// center of mass for matched points with respect to image center
Point2f center0 = getCenterOfMass(pts0);
Point2f centerCur = getCenterOfMass(ptsCur);
// start angles for image positive hemispheres
startAngle0 = (1000 * center0.x / image0small.cols) * 360 - 90;
startAngleCur = (1000 * centerCur.x / imageCursmall.cols) * 360 - 90;
}
Mat sphere1p, sphere1m, sphere2m, sphere2p;
convertCylindricToSphericalPair(image0, sphere1p, sphere1m, startAngle0);
convertCylindricToSphericalPair(imageCur, sphere2p, sphere2m, startAngleCur);
{
imwrite(QString("sphere1m_%1.png").arg(name1).toLatin1().data(), sphere1m);
imwrite(QString("sphere1p_%1.png").arg(name1).toLatin1().data(), sphere1p);
imwrite(QString("sphere2m_%1.png").arg(name2).toLatin1().data(), sphere2m);
imwrite(QString("sphere2p_%1.png").arg(name2).toLatin1().data(), sphere2p);
}
// get matches for spherical images
if (rbForward->isChecked())
calculateMatches(sphere1p, sphere2p, pts0, ptsCur, detector, sbNndr->value(), isDebug, name1, name2);
else
calculateMatches(sphere1m, sphere2m, pts0, ptsCur, detector, sbNndr->value(), isDebug, name1, name2);
Mat D1, D2, D3;
Mat iD1, iD2, iD3;
if (pts0.size() == 0) {
addlog("Failed to find correspondencies");
continue;
}
addlog(QString("Found %1 good matches").arg(pts0.size()));
// algebraic part to get list of camera model parameters candidates
createQepMatrices(pts0, ptsCur, D1, D2, D3);
createInvertedQepMatrices(D1, D2, D3, iD1, iD2, iD3);
std::vector<Mat> eigvectors;
auto AB = solveQepAndGetCandidateParameters(iD1, iD2, iD3, eigvectors); // list of prefiltered (a,b) candidate pairs
cv::Mat E;
double a, b;
double err = estimateBestEssentialMatrix(pts0, ptsCur, AB, E, a, b);
if (E.cols > 0 && cv::norm(E, NORM_L2) < 1e+5) {
addlog("Estimated E", E);
addlog(QString("E error is %1. Norm(E) = %2").arg(err).arg(cv::norm(E, NORM_L2)));
}
else {
/*if (!rbForward->isChecked())
calculateMatches(sphere1p, sphere2p, pts0, ptsCur, detector, sbNndr->value(), isDebug, name1, name2);
else
calculateMatches(sphere1m, sphere2m, pts0, ptsCur, detector, sbNndr->value(), isDebug, name1, name2);
createQepMatrices(pts0, ptsCur, D1, D2, D3);
createInvertedQepMatrices(D1, D2, D3, iD1, iD2, iD3);
AB = solveQepAndGetCandidateParameters(iD1, iD2, iD3, eigvectors);
err = estimateBestEssentialMatrix(pts0, ptsCur, AB, E, a, b);
if (E.cols > 0 || cv::norm(E, NORM_L2) < 1e+5) {
color = Qt::blue;
}
else */{
if (E.cols == 0)
addlog("Failed to estimate E");
else
addlog(QString("E seems incorrect as norm(E) = %1").arg(cv::norm(E, NORM_L2)));
color = (Qt::GlobalColor)(color + 3);
scene->addPoint(CloudPoint(cumT.at<double>(0, 0),
cumT.at<double>(1, 0),
cumT.at<double>(2, 0),
color));
cumR = cv::Mat::eye(3, 3, CV_64FC1);
addlog("");
continue;
}
}
int removed = removeOutliersUsingFundamentalMatrix(pts0, ptsCur, E, a, b);
addlog(QString("Removed %1 outliers").arg(removed));
err = calculateEpipolarErrorForEssentialMatrix(E, pts0, ptsCur, a, b);
/* if (chkCustomHemisphere->isChecked() && removed > 0) {
// undistorted vectors
std::vector<Point2f> outpts0, outptsCur;
getUndistortedVectorsForEssentialMatrixEstimation(pts0, a, b, outpts0);
getUndistortedVectorsForEssentialMatrixEstimation(ptsCur, a, b, outptsCur);
cv::Mat E1;
estimateEssentialMatrixFromPoints(outpts0, outptsCur, E1);
addlog("Refined E", E1);
double err1 = calculateEpipolarErrorForEssentialMatrix(E, pts0, ptsCur, a, b);
if (err < err1)
addlog(QString("Old E is better than new (%1 against %2). Switching back to old one").arg(err).arg(err1));
else {
addlog(QString("Refined E is better than old (%2 against %1). Switching to new one").arg(err).arg(err1));
E = E1.clone();
}
}*/
cv::Mat R, t;
extractRotationAndTranslationFromEssentialMatrix(E, R, t);
cumT = cumR.inv() * t + cumT;
//cumT = cumT / cv::norm(cumT);
cumR = R * cumR;
addlog("R", R);
addlog("t", t);
addlog("cumT", cumT);
cv::Vec4d q = convertRotationMatrixToQuaternion(R);
addlog("Q", q);
double ang, x, y, z;
convertQuaternionToAngleAndAxis(q, ang, x, y, z);
addlog(QString("Rotation: %1, (%2, %3, %4)")
.arg(ang * 180 / M_PI)
.arg(x)
.arg(y)
.arg(z));
q = convertRotationMatrixToQuaternion(cumR);
convertQuaternionToAngleAndAxis(q, ang, x, y, z);
addlog(QString("Rotation cum: %1, (%2, %3, %4)")
.arg(ang * 180 / M_PI)
.arg(x)
.arg(y)
.arg(z));
Rs.push_back(cumR.clone());
Ts.push_back(cumT.clone());
scene->addPoint(CloudPoint(cumT.at<double>(0, 0),
cumT.at<double>(1, 0),
cumT.at<double>(2, 0),
QColor(color)));
addlog("");
}
QStringList L;
foreach(QString s, imFiles) {
QFileInfo inf(s);
L << inf.fileName();
}
dumpResultsToJSON("scene_geometry.jsn", L, Rs, Ts);
dumpCameraPositionsToObj("scene_geometry.obj", Ts);
btnStart->setEnabled(true);
progressBar->setValue(100);
progressBar->hide();
addlog("Calculation completed");
if (log->verticalScrollBar())
log->verticalScrollBar()->setValue(log->verticalScrollBar()->maximum());
}
void MainWindow::on_actionLoadObj_triggered()
{
QString s = QFileDialog::getOpenFileName(this, QString("Load .obj file"), QString(), "Obj files (*.obj)");
if (!s.isEmpty())
loadObj(s);
}
void MainWindow::loadObj(QString path)
{
QFile f(path);
f.open(QIODevice::ReadOnly);
QTextStream T(&f);
T.readLine();
PointCloud cloud;
while (!T.atEnd()) {
QString s = T.readLine();
s.replace(",", ".");
QStringList L = s.split(" ");
cloud << CloudPoint(L[1].toFloat(), L[2].toFloat(), L[3].toFloat(), Qt::red);
}
f.close();
scene->addCloud(cloud);
}
void MainWindow::on_btnStart_clicked()
{
process();
}
void MainWindow::on_btnClear_clicked()
{
log->clear();
scene->setCloud(PointCloud());
}