-
Notifications
You must be signed in to change notification settings - Fork 3
/
mainwindow.cpp
756 lines (617 loc) · 32.1 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/*#-------------------------------------------------
#
# Disparity and depth maps with OpenCV
#
# by AbsurdePhoton - www.absurdephoton.fr
#
# v1.3 - 2019/06/03
#
#-------------------------------------------------*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include "mpo.h"
#include "mat-image-tools.h"
#include <opencv2/stereo.hpp>
using namespace cv;
using namespace cv::ximgproc;
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Init default values
ui->horizontalSlider_pre_filter_size->setValue(41);
ui->horizontalSlider_pre_filter_cap->setValue(31);
ui->horizontalSlider_SAD_window_size->setValue(9);
ui->horizontalSlider_min_disparity->setValue(-64);
ui->horizontalSlider_num_of_disparity->setValue(144);
ui->horizontalSlider_texture_threshold->setValue(10);
ui->horizontalSlider_uniqueness_ratio->setValue(15);
ui->horizontalSlider_speckle_window_size->setValue(15);
ui->horizontalSlider_speckle_window_size->setValue(15);
ui->horizontalSlider_speckle_window_size->setValue(0);
ui->horizontalSlider_speckle_range->setValue(0);
ui->horizontalSlider_disp_12_max_diff->setValue(-1);
ui->horizontalSlider_disparity_levels->setValue(50);
// Adjust P1 and P2 values auto
int value = ui->horizontalSlider_SAD_window_size->value();
ui->horizontalSlider_P2->setValue(96*value*value);
ui->horizontalSlider_P1->setValue(24*value*value);
ui->doubleSpinBox_lambda->setValue(8000);
ui->doubleSpinBox_sigma->setValue(1.5);
// SGBM modes in the drop box
ui->mode->addItem("SGBM", QVariant(0));
ui->mode->addItem("HH", QVariant(1));
ui->mode->addItem("SGBM_3WAY", QVariant(2));
ui->mode->addItem("HH4", QVariant(3));
ui->mode->setCurrentIndex(2);
// Set visible items
ui->frame_BM_SGBM->setVisible(true);
ui->frame_quasi->setVisible(false);
ui->label_depth_map->setBackgroundRole(QPalette::Base);
ui->label_depth_map->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
ui->label_depth_map->setScaledContents(true);
ui->scrollArea->setBackgroundRole(QPalette::Dark);
ui->scrollArea->setWidgetResizable(true);
ui->scrollAreaWidgetContents->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
computed = false; // depthmap not yet computed
parameters = false; // XML file not loaded
basedir = "/home";
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::ShowDepthmap() {
if (!computed)
return;
QPixmap D;
if (ui->checkBox_fit->isChecked()) { // Fit to the display area
D = Mat2QPixmapResized(disp_color, ui->scrollArea->width(), ui->scrollArea->height(), true); // Display depthmap
ui->scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // Make scrollbars disappear
ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
} else { // Zoom = 100%
D = Mat2QPixmapResized(disp_color, disp_color.cols-18, disp_color.rows-18, true);
ui->scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); // Make scrollbars appear
ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
}
ui->label_depth_map->setFixedWidth(D.width()); // Resize the depthmap label
ui->label_depth_map->setFixedHeight(D.height());
ui->scrollAreaWidgetContents->setFixedWidth(D.width()); // Resize the container
ui->scrollAreaWidgetContents->setFixedHeight(D.height());
ui->label_depth_map->setPixmap(D); // Set the image content
}
//// COMPUTE DEPTHMAP
void MainWindow::on_Compute_clicked() { // compute the depth map
// check that both images have been loaded
if (this->left_image.empty() || this->right_image.empty())
return;
// check that both images have the same size
if (left_image.rows != right_image.rows || left_image.cols != right_image.cols) {
ui->label_depth_map->setText("Can't compute depth map: left and right images should be the same size");
return;
}
QApplication::setOverrideCursor(Qt::WaitCursor); // Waiting cursor
// Initialize variables to compute
cv::String algo; // stereo matching method (bm or sgbm)
if (ui->radioButton_BM->isChecked()) algo = "bm";
if (ui->radioButton_SGBM->isChecked()) algo = "sgbm";
if (ui->radioButton_quasi->isChecked()) algo = "quasi";
int max_disp = ui->horizontalSlider_num_of_disparity->value();
int wsize = ui->horizontalSlider_SAD_window_size->value(); // Window size (SAD)
int mode = ui->mode->itemData(ui->mode->currentIndex()).toInt(); // SGBM 3-way mode
// post-filtering - Lambda is a parameter defining the amount of regularization during filtering.
// Larger values force filtered disparity map edges to adhere more to source image edges. Typical value is 8000
double lambda = ui->doubleSpinBox_lambda->value();
// post-filtering - SigmaColor is a parameter defining how sensitive the filtering process is to source image edges.
// Large values can lead to disparity leakage through low-contrast edges.
// Small values can make the filter too sensitive to noise and textures in the source image.
// Typical values range from 0.8 to 2.0
double sigma = ui->doubleSpinBox_sigma->value();
// used post-filtering (wls_conf or wls_no_conf)
cv::String filter;
if (ui->checkBox_confidence->isChecked()) filter = "wls_conf"; else filter = "wls_no_conf";
if(algo=="quasi")
{
cv::Size frameSize = left_image.size();
Ptr<stereo::QuasiDenseStereo> stereo = stereo::QuasiDenseStereo::create(frameSize);
stereo->process(left_image, right_image);
uint8_t displvl = ui->horizontalSlider_disparity_levels->value();
cv::Mat disp;
disp = stereo->getDisparity(displvl);
// Visualization
computed = true; // Indicate that a depthmap exists
// convert the image to a QPixmap and display it
cv::cvtColor(disp, disp_color, COLOR_GRAY2RGB); // convert to color, better for saving the file
ShowDepthmap();
}
else {
// load_views
cv::Mat left = left_image;
cv::Mat right = right_image;
cv::Mat left_for_matcher, right_for_matcher;
cv::Mat left_disp,right_disp;
cv::Mat filtered_disp;
cv::Mat conf_map = cv::Mat(left.rows,left.cols,CV_8U);
conf_map = cv::Scalar(255);
cv::Rect ROI;
Ptr<cv::ximgproc::DisparityWLSFilter> wls_filter;
if(filter=="wls_conf") // filtering with confidence (significantly better quality than wls_no_conf)
{
left_for_matcher = left.clone();
right_for_matcher = right.clone();
if(algo=="bm")
{
Ptr<cv::StereoBM> left_matcher = cv::StereoBM::create(max_disp,wsize);
left_matcher->setPreFilterSize(ui->horizontalSlider_pre_filter_size->value()); // must be an odd between 5 and 255
left_matcher->setPreFilterCap(ui->horizontalSlider_pre_filter_cap->value()); // must be within 1 and 63
left_matcher->setBlockSize(ui->horizontalSlider_SAD_window_size->value()); // must be odd, be within 5..255 and be not larger than image width or height
left_matcher->setMinDisparity(ui->horizontalSlider_min_disparity->value()); // normally at 0
left_matcher->setNumDisparities(ui->horizontalSlider_num_of_disparity->value()); // must be > 0 and divisible by 16
left_matcher->setTextureThreshold(ui->horizontalSlider_texture_threshold->value()); // must be non-negative
left_matcher->setUniquenessRatio(ui->horizontalSlider_uniqueness_ratio->value()); // must be non-negative
left_matcher->setSpeckleWindowSize(ui->horizontalSlider_speckle_window_size->value()); // 0 to disable, set to 50-200
left_matcher->setSpeckleRange(ui->horizontalSlider_speckle_range->value()); // 1 or 2 is good, 0 to disable
left_matcher->setDisp12MaxDiff(ui->horizontalSlider_disp_12_max_diff->value()); // negative = disabled
wls_filter = cv::ximgproc::createDisparityWLSFilter(left_matcher);
Ptr<cv::StereoMatcher> right_matcher = cv::ximgproc::createRightMatcher(left_matcher);
cv::cvtColor(left_for_matcher, left_for_matcher, cv::COLOR_BGR2GRAY); // Convert to gray, needed by bm function
cv::cvtColor(right_for_matcher, right_for_matcher, cv::COLOR_BGR2GRAY);
left_matcher-> compute(left_for_matcher, right_for_matcher,left_disp);
right_matcher->compute(right_for_matcher,left_for_matcher, right_disp);
}
else if(algo=="sgbm")
{
Ptr<cv::StereoSGBM> left_matcher = cv::StereoSGBM::create(0,max_disp,wsize);
left_matcher->setPreFilterCap(ui->horizontalSlider_pre_filter_size->value()); // must be within 1 and 63
left_matcher->setBlockSize(ui->horizontalSlider_SAD_window_size->value()); // must be odd, be within 5..255 and be not larger than image width or height
left_matcher->setMinDisparity(ui->horizontalSlider_min_disparity->value()); // normally at 0
left_matcher->setNumDisparities(ui->horizontalSlider_num_of_disparity->value()); // must be > 0 and divisible by 16
left_matcher->setUniquenessRatio(ui->horizontalSlider_uniqueness_ratio->value()); // must be non-negative
left_matcher->setSpeckleWindowSize(ui->horizontalSlider_speckle_window_size->value()); // 0 to disable, set to 50-200
left_matcher->setSpeckleRange(ui->horizontalSlider_speckle_range->value()); // 1 or 2 is good, 0 to disable
left_matcher->setDisp12MaxDiff(ui->horizontalSlider_disp_12_max_diff->value()); // negative = disabled
left_matcher->setP1(ui->horizontalSlider_P1->value());
left_matcher->setP2(ui->horizontalSlider_P2->value());
left_matcher->setMode(mode);
wls_filter = cv::ximgproc::createDisparityWLSFilter(left_matcher);
Ptr<cv::StereoMatcher> right_matcher = cv::ximgproc::createRightMatcher(left_matcher);
left_matcher-> compute(left_for_matcher, right_for_matcher,left_disp);
right_matcher->compute(right_for_matcher,left_for_matcher, right_disp);
}
// filtering
wls_filter->setLambda(lambda);
wls_filter->setSigmaColor(sigma);
wls_filter->filter(left_disp,left,filtered_disp,right_disp);
conf_map = wls_filter->getConfidenceMap();
ROI = wls_filter->getROI(); // Get the ROI that was used in the last filter call
}
else if(filter=="wls_no_conf")
{
/* There is no convenience function for the case of filtering with no confidence, so we
will need to set the ROI and matcher parameters manually */
left_for_matcher = left.clone();
right_for_matcher = right.clone();
if(algo=="bm")
{
Ptr<cv::StereoBM> matcher = cv::StereoBM::create(max_disp,wsize);
matcher->setPreFilterSize(ui->horizontalSlider_pre_filter_size->value()); // must be an odd between 5 and 255
matcher->setPreFilterCap(ui->horizontalSlider_pre_filter_cap->value()); // must be within 1 and 63
matcher->setBlockSize(ui->horizontalSlider_SAD_window_size->value()); // must be odd, be within 5..255 and be not larger than image width or height
matcher->setMinDisparity(ui->horizontalSlider_min_disparity->value()); // normally at 0
matcher->setNumDisparities(ui->horizontalSlider_num_of_disparity->value()); // must be > 0 and divisible by 16
matcher->setTextureThreshold(ui->horizontalSlider_texture_threshold->value()); // must be non-negative
matcher->setUniquenessRatio(ui->horizontalSlider_uniqueness_ratio->value()); // must be non-negative
matcher->setSpeckleWindowSize(ui->horizontalSlider_speckle_window_size->value()); // 0 to disable, set to 50-200
matcher->setSpeckleRange(ui->horizontalSlider_speckle_range->value()); // 1 or 2 is good, 0 to disable
matcher->setDisp12MaxDiff(ui->horizontalSlider_disp_12_max_diff->value()); // negative = disabled
cv::cvtColor(left_for_matcher, left_for_matcher, cv::COLOR_BGR2GRAY); // Convert to gray, needed by bm function
cv::cvtColor(right_for_matcher, right_for_matcher, cv::COLOR_BGR2GRAY);
ROI = computeROI(left_for_matcher.size(),matcher);
wls_filter = cv::ximgproc::createDisparityWLSFilterGeneric(false);
wls_filter->setDepthDiscontinuityRadius((int)ceil(0.33*wsize));
matcher->compute(left_for_matcher,right_for_matcher,left_disp);
}
else if(algo=="sgbm")
{
Ptr<cv::StereoSGBM> matcher = cv::StereoSGBM::create(0,max_disp,wsize);
matcher->setPreFilterCap(ui->horizontalSlider_pre_filter_size->value()); // must be within 1 and 63
matcher->setBlockSize(ui->horizontalSlider_SAD_window_size->value()); // must be odd, be within 5..255 and be not larger than image width or height
matcher->setMinDisparity(ui->horizontalSlider_min_disparity->value()); // normally at 0
matcher->setNumDisparities(ui->horizontalSlider_num_of_disparity->value()); // must be > 0 and divisible by 16
matcher->setUniquenessRatio(ui->horizontalSlider_uniqueness_ratio->value()); // must be non-negative
matcher->setSpeckleWindowSize(ui->horizontalSlider_speckle_window_size->value()); // 0 to disable, set to 50-200
matcher->setSpeckleRange(ui->horizontalSlider_speckle_range->value()); // 1 or 2 is good, 0 to disable
matcher->setDisp12MaxDiff(ui->horizontalSlider_disp_12_max_diff->value()); // negative = disabled
matcher->setP1(ui->horizontalSlider_P1->value());
matcher->setP2(ui->horizontalSlider_P2->value());
matcher->setMode(mode);
ROI = computeROI(left_for_matcher.size(),matcher);
wls_filter = cv::ximgproc::createDisparityWLSFilterGeneric(false);
wls_filter->setDepthDiscontinuityRadius((int)ceil(0.5*wsize));
matcher->compute(left_for_matcher,right_for_matcher,left_disp);
}
// filtering
wls_filter->setLambda(lambda);
wls_filter->setSigmaColor(sigma);
wls_filter->filter(left_disp,left,filtered_disp,Mat(),ROI);
}
// Visualization
cv::Mat raw_disp_vis;
getDisparityVis(left_disp,raw_disp_vis,1);
Mat filtered_disp_vis;
getDisparityVis(filtered_disp,filtered_disp_vis,1);
computed = true; // Indicate that a depthmap exists
// convert the image to a QPixmap and display it
cv::cvtColor(filtered_disp_vis, disp_color, COLOR_GRAY2RGB); // convert to color, better for saving the file
ShowDepthmap();
}
// Reprojection 3D
//Mat project3D;
//cv::reprojectImageTo3D(filtered_disp, project3D, saveQ, true, CV_32F);
//project3D.convertTo(project3D, CV_8U);
//ui->label_3D->setPixmap(Mat2QPixmapResized(project3D, ui->label_3D->width(),ui->label_3D->height()));
QApplication::restoreOverrideCursor(); // Restore cursor
}
Rect MainWindow::computeROI(Size2i src_sz, Ptr<StereoMatcher> matcher_instance) // used for filtering
{
int min_disparity = matcher_instance->getMinDisparity();
int num_disparities = matcher_instance->getNumDisparities();
int block_size = matcher_instance->getBlockSize();
int bs2 = block_size/2;
int minD = min_disparity, maxD = min_disparity + num_disparities - 1;
int xmin = maxD + bs2;
int xmax = src_sz.width + minD - bs2;
int ymin = bs2;
int ymax = src_sz.height - bs2;
Rect r(xmin, ymin, xmax - xmin, ymax - ymin);
return r;
}
/////////////////// Sliders management (callbacks and constraints) //////////////////////
///// pre filter size
void MainWindow::on_horizontalSlider_pre_filter_size_valueChanged(int value)
{
if ((value % 2) == 0) { // must be an odd number
value -= 1;
ui->horizontalSlider_pre_filter_size->setValue(value);
}
}
///// SAD window size
void MainWindow::set_SADWindowSize() {
int value = 255; // max value allowed
// we check that the value is not bigger than the size of the pictures
if (! left_image.empty())
value = std::min(value, std::min(left_image.cols, left_image.rows));
if (! right_image.empty())
value = std::min(value, std::min(right_image.cols, right_image.rows));
// we check that the value is >= 5
value = std::max(value, 5);
ui->horizontalSlider_SAD_window_size->setMaximum(value);
}
void MainWindow::on_horizontalSlider_SAD_window_size_valueChanged(int value)
{
if ((value % 2) == 0) { // must be an odd number
value -= 1;
ui->horizontalSlider_SAD_window_size->setValue(value);
}
// Adjust P1 and P2 values auto
ui->horizontalSlider_P2->setValue(96*value*value);
ui->horizontalSlider_P1->setValue(24*value*value);
}
///// Number of disparities
void MainWindow::on_horizontalSlider_num_of_disparity_sliderMoved(int value)
{
set_num_of_disparity_slider_to_multiple_16(value); // allow only values that are divisible by 16
}
void MainWindow::on_horizontalSlider_num_of_disparity_valueChanged(int value)
{
set_num_of_disparity_slider_to_multiple_16(value); //allow only values that are divisible by 16
}
void MainWindow::set_num_of_disparity_slider_to_multiple_16(int value) {
if ((value % 16) != 0) {
value -= (value % 16);
ui->horizontalSlider_num_of_disparity->setValue(value);
}
}
///// SGBM P1
void MainWindow::on_horizontalSlider_P1_valueChanged(int value)
{
if (ui->horizontalSlider_P2->value() <= value) {
value = ui->horizontalSlider_P2->value() - 1;
ui->horizontalSlider_P1->setValue(value);
}
}
///// SGBM P2
void MainWindow::on_horizontalSlider_P2_valueChanged(int value)
{
if (value <= ui->horizontalSlider_P1->value()) {
value = ui->horizontalSlider_P1->value() + 1;
ui->horizontalSlider_P2->setValue(value);
}
}
///// BM
void MainWindow::on_radioButton_BM_clicked ()
{
if (ui->radioButton_BM->isChecked())
{
ui->frame_BM_SGBM->setVisible(true);
ui->frame_quasi->setVisible(false);
ui->checkBox_confidence->setVisible(true);
ui->horizontalSlider_P1->setVisible(false);
ui->label_P1->setVisible(false);
ui->label_11->setVisible(false);
ui->horizontalSlider_P2->setVisible(false);
ui->label_P2->setVisible(false);
ui->label_12->setVisible(false);
ui->mode->setVisible(false);
ui->label_mode->setVisible(false);
ui->Disparity->setVisible(true);
}
}
///// SGBM
void MainWindow::on_radioButton_SGBM_clicked ()
{
if (ui->radioButton_SGBM->isChecked())
{
ui->frame_BM_SGBM->setVisible(true);
ui->frame_quasi->setVisible(false);
ui->checkBox_confidence->setVisible(true);
ui->horizontalSlider_P1->setVisible(true);
ui->label_P1->setVisible(true);
ui->label_11->setVisible(true);
ui->horizontalSlider_P2->setVisible(true);
ui->label_P2->setVisible(true);
ui->label_12->setVisible(true);
ui->mode->setVisible(true);
ui->label_mode->setVisible(true);
ui->Disparity->setVisible(true);
}
}
///// QUASI
void MainWindow::on_radioButton_quasi_clicked ()
{
if (ui->radioButton_quasi->isChecked())
{
ui->frame_BM_SGBM->setVisible(false);
ui->frame_quasi->setVisible(true);
ui->checkBox_confidence->setVisible(false);
ui->Disparity->setVisible(false);
}
}
//// Unused
void MainWindow::on_horizontalSlider_pre_filter_cap_valueChanged(int value) {};
void MainWindow::on_horizontalSlider_min_disparity_valueChanged(int value) {};
void MainWindow::on_horizontalSlider_texture_threshold_valueChanged(int value) {};
void MainWindow::on_horizontalSlider_uniqueness_ratio_valueChanged(int value) {};
void MainWindow::on_horizontalSlider_speckle_window_size_valueChanged(int value) {};
void MainWindow::on_horizontalSlider_speckle_range_valueChanged(int value) {};
void MainWindow::on_horizontalSlider_disp_12_max_diff_valueChanged(int value) {};
///// Other functions in the gui
void MainWindow::on_Camera_clicked () { // load stereo camera matrix
int ref_width, ref_height, ref_size;
double error;
// Read calibration results from XML file
QString filename = QFileDialog::getOpenFileName(this, "Select XML calibration file", QString::fromStdString(basedir), "XML parameters (*.xml *.XML)");
if (filename.isNull())
return;
std::string filename_stereo = filename.toUtf8().constData(); // convert filename from QString to std::string
// Read file
FileStorage fs_stereo(filename_stereo, FileStorage::READ);
fs_stereo["BoardWidth"] >> ref_width;
fs_stereo["BoardHeight"] >> ref_height;
fs_stereo["PatternSize"] >> ref_size;
fs_stereo["KL"] >> KL;
fs_stereo["KR"] >> KR;
fs_stereo["DL"] >> DL;
fs_stereo["DR"] >> DR;
fs_stereo["R"] >> R;
fs_stereo["T"] >> T;
fs_stereo["E"] >> E;
fs_stereo["F"] >> F;
fs_stereo["ErrorStereo"] >> error;
QMessageBox::information(this, "Calibration file loaded", "Average error : " + QString::number(error)); // Display useful info about matrix quality
parameters = true;
}
void MainWindow::on_Rectify_clicked () {
if (this->left_image.empty() || this->right_image.empty()) // check that both images have been loaded
return;
if (!parameters) // XML file not loaded
return;
// Rectify stereo
Mat R1, R2, P1, P2, Q;
int flags = CALIB_ZERO_DISPARITY;
double alpha = ui->doubleSpinBox_alpha->value();
cv::Size size = left_image.size();
stereoRectify(KL, DL, KR, DR, size, R, T, R1, R2, P1, P2, Q, flags, alpha);
// Init undistortion
Mat map11, map12, map21, map22;
initUndistortRectifyMap(KL, DL, R1, P1, size, CV_32FC2, map11, map12);
initUndistortRectifyMap(KR, DR, R2, P2, size, CV_32FC2, map21, map22);
// Remap images
flags = INTER_LINEAR;
int flags2 = BORDER_CONSTANT;
remap(left_image, left_image, map11, map12, flags, flags2);
remap(right_image, right_image, map21, map22, flags, flags2);
// Display rectified images
QPixmap L, R;
L = Mat2QPixmapResized(left_image, ui->label_image_left->width(), ui->label_image_left->height(), true);
R = Mat2QPixmapResized(right_image, ui->label_image_right->width(), ui->label_image_right->height(), true);
ui->label_image_left->setPixmap(L);
ui->label_image_right->setPixmap(R);
rectified = true;
}
void MainWindow::on_MPO_clicked () {
QString filename = QFileDialog::getOpenFileName(this, "Select MPO image file", QString::fromStdString(basedir), "MPO image (*.mpo *.MPO)");
if (filename.isNull())
return;
basename = filename.toUtf8().constData(); // basename is used after to save other files
size_t found = basename.find_last_of("/"); // find last directory
basedir = basename.substr(0,found) + "/"; // extract file location
basename = basename.substr(0, basename.size()-4);
bool success;
vector<cv::Mat> matImages(2);
success = MPO2Mat(filename, matImages); // Load MPO file
if (!success)
return;
// display images
left_image = matImages[0];
right_image = matImages[1];
QPixmap L, R;
L = Mat2QPixmapResized(left_image, ui->label_image_left->width(), ui->label_image_left->height(), true);
R = Mat2QPixmapResized(right_image, ui->label_image_right->width(), ui->label_image_right->height(), true);
ui->label_image_left->setPixmap(L);
ui->label_image_right->setPixmap(R);
ui->horizontalSlider_min_disparity->setMaximum(left_image.cols / 4);
ui->horizontalSlider_min_disparity->setMinimum(-left_image.cols / 4);
computed = false;
rectified = false;
}
void MainWindow::on_Negative_clicked () { // negative colors of the depthmap
if (this->disp_color.empty()) // check that depthmap exists
return;
cv::bitwise_not(disp_color,disp_color); // negative depthmap
// display depthmap
ShowDepthmap();
}
void MainWindow::on_Save_depthmap_clicked() { // save depthmap in PNG format
if (this->disp_color.empty()) // check that depthmap exists
return;
QString filename = QFileDialog::getSaveFileName(this, "Save depthmap to image...", "./" + QString::fromStdString(basename +"-depthmap") + ".png", NULL);
std::string filename_s = filename.toUtf8().constData();
if (filename.isNull() || filename.isEmpty())
return;
cv::imwrite(filename_s, disp_color); // save depthmap
}
void MainWindow::on_Save_Images_clicked() { // save both rectified images to PNG format
if (this->left_image.empty() || this->right_image.empty()) // check both images have been loaded
return;
QString filename = QFileDialog::getSaveFileName(this, "Save images (base name)...", "./" + QString::fromStdString(basename), NULL);
if (filename.isNull() || filename.isEmpty())
return;
// save the images
std::string filename_L = filename.toUtf8().constData() + std::string("-Rectified-L.png");
cv::imwrite(filename_L, left_image);
std::string filename_R = filename.toUtf8().constData() + std::string("-Rectified-R.png");
cv::imwrite(filename_R, right_image);
}
void MainWindow::on_Rotate_clicked() { // rotates the left and right images by 90°
if (this->left_image.empty() || this->right_image.empty()) // check both images have been loaded
return;
rotate(left_image, left_image, ROTATE_90_CLOCKWISE); // Others are ROTATE_180 and ROTATE_90_COUNTERCLOCKWISE
rotate(right_image, right_image, ROTATE_90_CLOCKWISE);
QPixmap L, R;
L = Mat2QPixmapResized(left_image, ui->label_image_left->width(), ui->label_image_left->height(), true);
R = Mat2QPixmapResized(right_image, ui->label_image_right->width(), ui->label_image_right->height(), true);
ui->label_image_left->setPixmap(L);
ui->label_image_right->setPixmap(R);
}
void MainWindow::on_Swap_clicked() { // Swap left and right image
if (this->left_image.empty() || this->right_image.empty()) // check both images have been loaded
return;
Mat temp;
temp = left_image;
left_image = right_image;
right_image = temp;
QPixmap L, R;
L = Mat2QPixmapResized(left_image, ui->label_image_left->width(), ui->label_image_left->height(), true);
R = Mat2QPixmapResized(right_image, ui->label_image_right->width(), ui->label_image_right->height(), true);
ui->label_image_left->setPixmap(L);
ui->label_image_right->setPixmap(R);
}
void MainWindow::on_Normalize_clicked() { // normalize depthmap
if (!computed)
return;
cv::normalize(disp_color, disp_color, 0, 255, NORM_MINMAX, -1);
// display depthmap
ShowDepthmap();
}
void MainWindow::on_checkBox_fit_clicked() // Loads left image
{
ShowDepthmap();
}
void MainWindow::on_Left_clicked() // Loads left image
{
QString filename = QFileDialog::getOpenFileName(this, "Select left picture file", QString::fromStdString(basedir), "Images (*.jpg *.JPG *.jpeg *.JPEG *.jp2 *.JP2 *.png *.PNG *.tif *.TIF *.tiff *.TIFF *.bmp *.BMP)");
if (filename.isNull() || filename.isEmpty())
return;
basename = filename.toUtf8().constData(); // basename is used after to save other files
size_t found = basename.find_last_of("/"); // find last directory
basedir = basename.substr(0,found) + "/"; // extract file location
basename = basename.substr(0, basename.size()-6);
std::string filename_s = filename.toUtf8().constData();
cv::Mat mat = cv::imread(filename_s, IMREAD_COLOR); // Load image
ui->label_image_left->setPixmap(Mat2QPixmapResized(mat, ui->label_image_left->width(), ui->label_image_left->height(), true)); // Display left image
left_image = mat; // stores the image for further use
set_SADWindowSize(); // adapt parameters automatically
computed = false; // depthmap not yet computed
rectified = false;
ui->label_depth_map->setPixmap(QPixmap()); // Delete the depthmap image
ui->label_depth_map->setText("Depthmap not yet computed"); // Text in the depthmap area
ui->horizontalSlider_min_disparity->setMaximum(left_image.cols / 4);
ui->horizontalSlider_min_disparity->setMinimum(-left_image.cols / 4);
}
void MainWindow::on_Right_clicked() // Loads right image
{
QString filename = QFileDialog::getOpenFileName(this, "Select right picture file", QString::fromStdString(basedir), "Images (*.jpg *.JPG *.jpeg *.JPEG *.jp2 *.JP2 *.png *.PNG *.tif *.TIF *.tiff *.TIFF *.bmp *.BMP)");
if (filename.isNull() || filename.isEmpty())
return;
basename = filename.toUtf8().constData();
size_t found = basename.find_last_of("/"); // find last directory
basedir = basename.substr(0,found) + "/"; // extract file location
basename = basename.substr(0, basename.size()-6);
std::string filename_s = filename.toUtf8().constData();
cv::Mat mat = cv::imread(filename_s, IMREAD_COLOR); // Load image
ui->label_image_right->setPixmap(Mat2QPixmapResized(mat, ui->label_image_right->width(), ui->label_image_right->height(), true)); // Display right image
right_image = mat; // stores the image for further use
set_SADWindowSize(); // adapt parameters automatically
computed = false; // depthmap not yet computed
rectified = false;
ui->label_depth_map->setPixmap(QPixmap()); // Delete the depthmap image
ui->label_depth_map->setText("Depthmap not yet computed"); // Text in the depthmap area
ui->horizontalSlider_min_disparity->setMaximum(right_image.cols / 4);
ui->horizontalSlider_min_disparity->setMinimum(-right_image.cols / 4);
}
void MainWindow::on_Disparity_clicked() // Open disparity window
{
if (left_image.empty() || right_image.empty()) // check both images have been loaded AND rectified
return;
if (!rectified) {
int notRectified = QMessageBox::question(this, "Are you sure ?", "You didn't rectify the images with the camera parameters. Are you sure?", QMessageBox::Yes|QMessageBox::No);
if (notRectified == QMessageBox::No)
return;
}
Disparity *disp_form = new Disparity(this); // create form window
disp_form->setLeftImage(left_image); // pass the left & right images variables
disp_form->setRightImage(right_image);
int result = disp_form->exec(); // execute the form
if (result == QDialog::Rejected) // if cancel
return;
int min_disparity = disp_form->getBackgroundDisparity(); // get min disparity value
ui->horizontalSlider_min_disparity->setValue(min_disparity); // set min disparity in the gui
int num_disparity = std::abs(disp_form->getForegroundDisparity() - min_disparity); // get number of disparity value
if ((num_disparity % 16) != 0) { // must be divisible by 16
num_disparity -= (num_disparity % 16); // correct the value
num_disparity += 16; // add 16 to be sure
}
ui->horizontalSlider_num_of_disparity->setValue(num_disparity); // set number of disparity in the gui
}
void MainWindow::on_Adjust_clicked() // Open adjust window
{
if (left_image.empty() || right_image.empty()) // check both images have been loaded AND rectified
return;
if (!rectified) {
int notRectified = QMessageBox::question(this, "Are you sure ?", "You didn't rectify the images with the camera parameters. Are you sure?", QMessageBox::Yes|QMessageBox::No);
if (notRectified == QMessageBox::No)
return;
}
Adjust *adj_form = new Adjust(this); // create form window
adj_form->setLeftImage(left_image); // pass the left & right images variables
adj_form->setRightImage(right_image);
int result = adj_form->exec(); // execute the form
if (result == QDialog::Rejected) // if cancel
return;
right_image = adj_form->getResult();
ui->label_image_right->setPixmap(Mat2QPixmapResized(right_image, ui->label_image_right->width(), ui->label_image_right->height(), true)); // Display right image
}