-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqtopencvdepthmap.cpp
More file actions
457 lines (398 loc) · 14.4 KB
/
qtopencvdepthmap.cpp
File metadata and controls
457 lines (398 loc) · 14.4 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
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
#include <QFileDialog>
#include <QProgressDialog>
#include <QMessageBox>
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "processor.h"
#include "qtopencvdepthmap.h"
#include "ui_qtopencvdepthmap.h"
/**
* Constructor for the main GUI widget.
* @param args The command-line arguments object parsed by argp.
* @param parent Standard QWidget parent.
*/
QtOpenCVDepthmap::QtOpenCVDepthmap(Arguments& args, QWidget *parent) :
QMainWindow(parent),
arguments(args),
ui(new Ui::QtOpenCVDepthmap)
{
first_load = true;
args_to_mapper();
ui->setupUi(this);
set_active(false);
std::string input = arguments.get_value<std::string>(Arguments::INPUT_FILENAME);
if (!input.empty()) {
open_filename(input);
}
}
/**
* Destructor cleans up the UI.
*/
QtOpenCVDepthmap::~QtOpenCVDepthmap()
{
delete ui;
}
/**
* Set whether the application is "active". Active meaning that a valid input is loaded and the configuration controls should be enabled.
* @param isActive If the application should be "active" or not.
*/
void QtOpenCVDepthmap::set_active(bool isActive) {
//set disparity inputs enabled/disabled
ui->input_minDisparity->setEnabled(isActive);
ui->input_numDisparities->setEnabled(isActive);
ui->input_SADWindowSize->setEnabled(isActive);
ui->input_P1->setEnabled(isActive);
ui->input_P2->setEnabled(isActive);
ui->input_disp12MAxDiff->setEnabled(isActive);
ui->input_preFilterCap->setEnabled(isActive);
ui->input_uniqunessRatio->setEnabled(isActive);
ui->input_speckleWindowSize->setEnabled(isActive);
ui->input_speckleRange->setEnabled(isActive);
ui->input_fullDP->setEnabled(isActive);
//set position inputs enabled/disabled
ui->horizontalSlider->setEnabled(isActive);
ui->spinBox_clip_start->setEnabled(isActive);
ui->spinBox_current_frame->setEnabled(isActive);
ui->spinBox_clip_end->setEnabled(isActive);
//check_clip_buttons assumes active
if (isActive) {
check_clip_buttons();
} else {
ui->button_set_clip_start->setEnabled(false);
ui->button_set_clip_end->setEnabled(false);
}
//set menu actions enabled/disabled
ui->actionExport->setEnabled(isActive);
is_active = isActive;
}
/**
* Set the appropriate depthmap settings from the application arguments.
*/
void QtOpenCVDepthmap::args_to_mapper() {
mapper.numberOfDisparities = arguments.get_value<int>(Arguments::NUM_DISPARITIES);
mapper.disp12MaxDiff = arguments.get_value<int>(Arguments::DISP12_MAX_DIFF);
mapper.fullDP = arguments.get_value<bool>(Arguments::FULL_DP);
mapper.minDisparity = arguments.get_value<int>(Arguments::MIN_DISPARITY);
mapper.P1 = arguments.get_value<int>(Arguments::P1);
mapper.P2 = arguments.get_value<int>(Arguments::P2);
mapper.preFilterCap = arguments.get_value<int>(Arguments::PRE_FILTER_CAP);
mapper.SADWindowSize = arguments.get_value<int>(Arguments::SAD_WINDOW_SIZE);
mapper.speckleRange = arguments.get_value<int>(Arguments::SPECKLE_RANGE);
mapper.speckleWindowSize = arguments.get_value<int>(Arguments::SPECKLE_WINDOW_SIZE);
mapper.uniquenessRatio = arguments.get_value<int>(Arguments::UNIQUENESS);
}
/**
* Open a video file for input.
* @param filename The video file to process.
*/
void QtOpenCVDepthmap::open_filename(const std::string& filename) {
feed_src.open(filename);
if (feed_src.isOpened()) {
arguments.set_value(Arguments::INPUT_FILENAME, filename);
current_pos_msec = feed_src.get(CV_CAP_PROP_POS_MSEC);
current_pos_frame = feed_src.get(CV_CAP_PROP_POS_FRAMES);
current_pos_radio = feed_src.get(CV_CAP_PROP_POS_AVI_RATIO);
input_frame_count = feed_src.get(CV_CAP_PROP_FRAME_COUNT);
input_width = feed_src.get(CV_CAP_PROP_FRAME_WIDTH);
input_height = feed_src.get(CV_CAP_PROP_FRAME_HEIGHT);
input_fps = feed_src.get(CV_CAP_PROP_FPS);
split_width = input_width * 0.5;
output_width = split_width;
output_height = input_height;
output_fps = input_fps;
int start_frame = arguments.get_value<int>(Arguments::START_FRAME);
int end_frame = arguments.get_value<int>(Arguments::END_FRAME);
if (!first_load) {
start_frame = 1;
end_frame = input_frame_count;
} else {
first_load = false;
}
correct_range(start_frame, 1, (int)input_frame_count-1);
if (end_frame <= 0) {
end_frame = input_frame_count;
}
correct_range(end_frame, start_frame, (int)input_frame_count);
//set scrubber range
ui->horizontalSlider->setRange(1, input_frame_count);
//set spinbox ranges
ui->spinBox_clip_start->setMaximum(input_frame_count);
ui->spinBox_clip_end->setMaximum(input_frame_count);
ui->spinBox_current_frame->setMaximum(input_frame_count);
//set position defaults
ui->horizontalSlider->setSliderPosition(1);
ui->spinBox_current_frame->setValue(1);
ui->spinBox_clip_end->setValue(end_frame);
ui->spinBox_clip_start->setValue(start_frame);
ui->label_total_frames->setText(QString::number(input_frame_count));
//only set application as active after all settings have been loaded
set_active(true);
fetch_frame(start_frame);
} else {
QMessageBox msgBox;
msgBox.setText(QString::fromStdString(filename) + " could not be opened for reading.");
msgBox.setIcon(QMessageBox::Critical);
msgBox.exec();
}
}
//Input is 1-indexed
/**
* Fetch and display the specified source frame.
* @param index A 1-indexed value of the frame.
*/
void QtOpenCVDepthmap::fetch_frame(int index) {
if (is_active) {
//fetch and display source frame (0-indexed)
feed_src.set(CV_CAP_PROP_POS_FRAMES, index-1);
feed_src >> frame_src;
ui->sbs_view->showImage(frame_src);
update_depthmap();
}
}
/**
* Take the current input frame, process it, and display the depthmap.
*/
void QtOpenCVDepthmap::update_depthmap() {
//compute depth map with current settings and display it
left_eye = frame_src.colRange(0, split_width);
right_eye = frame_src.colRange(split_width, input_width);
mapper(left_eye, right_eye, frame_dst_16_gray);
//the disparity mapper outputs CV_16UC1 when we need it in CV_8UC1
frame_dst_16_gray.convertTo(frame_dst_8_gray, CV_8UC1);
cvtColor(frame_dst_8_gray, frame_dst_8_colour, CV_GRAY2RGB);
//display depthmap frame
ui->depth_view->showImage(frame_dst_8_colour);
}
/**
* Action to start an open-file dialog.
*/
void QtOpenCVDepthmap::on_actionOpen_triggered()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Open Video"), "", tr("Video Files (*.avi *.mpg *.mp4);;All Files (*.*)"));
if (!filename.isNull()) {
open_filename(filename.toStdString());
}
}
/**
* Change the specified parameter.
* @param arg1 the new value.
*/
void QtOpenCVDepthmap::on_input_minDisparity_valueChanged(int arg1)
{
update_mapper_value<int>(Arguments::MIN_DISPARITY, arg1);
}
/**
* Change the specified parameter.
* @param arg1 the new value.
*/
void QtOpenCVDepthmap::on_input_numDisparities_valueChanged(int arg1)
{
update_mapper_value<int>(Arguments::NUM_DISPARITIES, arg1);
}
/**
* Change the specified parameter.
* @param arg1 the new value.
*/
void QtOpenCVDepthmap::on_input_SADWindowSize_valueChanged(int arg1)
{
update_mapper_value<int>(Arguments::SAD_WINDOW_SIZE, arg1);
}
/**
* Change the specified parameter.
* @param arg1 the new value.
*/
void QtOpenCVDepthmap::on_input_P1_valueChanged(int arg1)
{
//if return value is true, have to check both p1 and p2
update_mapper_value<int>(Arguments::P1, arg1);
}
/**
* Change the specified parameter.
* @param arg1 the new value.
*/
void QtOpenCVDepthmap::on_input_P2_valueChanged(int arg1)
{
//if return value is true, have to check both p1 and p2
update_mapper_value<int>(Arguments::P2, arg1);
}
/**
* Change the specified parameter.
* @param arg1 the new value.
*/
void QtOpenCVDepthmap::on_input_disp12MAxDiff_valueChanged(int arg1)
{
update_mapper_value<int>(Arguments::DISP12_MAX_DIFF, arg1);
}
/**
* Change the specified parameter.
* @param arg1 the new value.
*/
void QtOpenCVDepthmap::on_input_preFilterCap_valueChanged(int arg1)
{
update_mapper_value<int>(Arguments::PRE_FILTER_CAP, arg1);
}
/**
* Change the specified parameter.
* @param arg1 the new value.
*/
void QtOpenCVDepthmap::on_input_uniqunessRatio_valueChanged(int arg1)
{
update_mapper_value<int>(Arguments::UNIQUENESS, arg1);
}
/**
* Change the specified parameter.
* @param arg1 the new value.
*/
void QtOpenCVDepthmap::on_input_speckleWindowSize_valueChanged(int arg1)
{
update_mapper_value<int>(Arguments::SPECKLE_WINDOW_SIZE, arg1);
}
/**
* Change the specified parameter.
* @param arg1 the new value.
*/
void QtOpenCVDepthmap::on_input_speckleRange_valueChanged(int arg1)
{
update_mapper_value<int>(Arguments::SPECKLE_RANGE, arg1);
}
/**
* Change the specified parameter.
* @param arg1 the new value.
*/
void QtOpenCVDepthmap::on_input_fullDP_stateChanged(int arg1)
{
/*
Constant Value Description
Qt::Unchecked 0 The item is unchecked.
Qt::PartiallyChecked 1 The item is partially checked. Items in hierarchical models may be partially checked if some, but not all, of their children are checked.
Qt::Checked 2 The item is checked.
*/
update_mapper_value<bool>(Arguments::FULL_DP, (arg1 != 0) );
}
/**
* The user has changed the current preview frame.
* @param frame_index The new frame to display.
*/
void QtOpenCVDepthmap::on_horizontalSlider_valueChanged(int frame_index)
{
if (is_active) {
//std::cout<<"Frame: " << frame_index << "/" << input_frame_count << std::endl;
fetch_frame(frame_index);
}
}
//function to force a value into a range (inclusive)
/**
* Caps a value into a range.
* @param value The value to cap.
* @param min The minimum Value can be.
* @param max The maximum Value can be.
*/
template<typename T>
void QtOpenCVDepthmap::correct_range(T& value, T min, T max) {
value = std::min(std::max(value, min), max);
}
/**
* Handles the logic for modifying the end frame sub-range.
* @param value Which frame should mark the end of the processing range.
*/
void QtOpenCVDepthmap::check_end_frame(int value) {
int start_frame = ui->spinBox_clip_start->value();
int end_frame = value;
correct_range(end_frame, start_frame, (int)input_frame_count);
update_mapper_value<int>(Arguments::END_FRAME, value);
ui->horizontalSlider->sub_range_end_changed(end_frame);
ui->spinBox_clip_end->setValue(end_frame);
check_clip_buttons();
}
/**
* Handles the logic for modifying the start frame sub-range.
* @param value Which frame should mark the start of the processing range.
*/
void QtOpenCVDepthmap::check_start_frame(int value) {
int end_frame = ui->spinBox_clip_end->value();
int start_frame = value;
correct_range(start_frame, 1, end_frame);
update_mapper_value<int>(Arguments::START_FRAME, value);
ui->horizontalSlider->sub_range_start_changed(start_frame);
ui->spinBox_clip_start->setValue(start_frame);
check_clip_buttons();
}
/**
* Handles the logic for setting the current frame.
* @param value Which frame to try and set the current frame to.
*/
void QtOpenCVDepthmap::check_current_frame(int value) {
int original_spin = ui->spinBox_current_frame->value();
int original_slider = ui->horizontalSlider->value();
int current_frame = value;
correct_range(current_frame, 1, (int)input_frame_count);
if (current_frame != original_spin) {
ui->spinBox_current_frame->setValue(current_frame);
}
if (current_frame != original_slider) {
ui->horizontalSlider->setValue(current_frame);
}
check_clip_buttons();
}
/**
* Checks to see if the current frame is a valid value to set as the beginning or end of a clip range.
* Set the GUI controls accordingly. Assumes the application is active.
*/
void QtOpenCVDepthmap::check_clip_buttons() {
int start_frame = arguments.get_value<int>(Arguments::START_FRAME);
int end_frame = arguments.get_value<int>(Arguments::END_FRAME);
int current_frame = ui->horizontalSlider->value();
ui->button_set_clip_start->setEnabled(current_frame <= end_frame);
ui->button_set_clip_end->setEnabled(current_frame >= start_frame);
}
/**
* Quit the application.
*/
void QtOpenCVDepthmap::on_actionQuit_triggered()
{
this->close();
}
/**
* The user has chosen to export the current selection with the current settings.
* Request an output video filename, and process it.
*/
void QtOpenCVDepthmap::on_actionExport_triggered()
{
std::string output_filename = arguments.get_value<std::string>(Arguments::OUTPUT_FILENAME);
QString filename = QFileDialog::getSaveFileName(this, tr("Save Video"), output_filename.c_str(), tr("Video Files (*.avi *.mpg *.mp4);;All Files (*.*)"));
if (!filename.isNull()) {
if (filename.toStdString() != output_filename) {
arguments.set_value<std::string>(Arguments::OUTPUT_FILENAME, filename.toStdString());
}
QString exportLabel = "Exporting ";
exportLabel.append(filename);
Processor processor(arguments, this->feed_src);
std::shared_ptr<cv::VideoWriter> output = processor.create_writer();
size_t start_frame = arguments.get_value<int>(Arguments::START_FRAME);
size_t end_frame = arguments.get_value<int>(Arguments::END_FRAME);
size_t range = end_frame + 1 - start_frame;
//set up progress dialog
QProgressDialog progress(exportLabel, "Cancel", 0, range, this);
progress.setWindowModality(Qt::WindowModal);
processor.set_next_frame(start_frame);
for (size_t index = start_frame; index <= end_frame && !progress.wasCanceled(); ++index) {
progress.setValue(index - start_frame);
processor.process_next_frame(*output);
}
progress.setValue(range);
}
}
/**
* The user has specified that the current frame should be the start of the output clip.
*/
void QtOpenCVDepthmap::on_button_set_clip_start_clicked()
{
check_start_frame(ui->horizontalSlider->value());
}
/**
* The user has sepecified that the current frame should be the end of the output clip.
*/
void QtOpenCVDepthmap::on_button_set_clip_end_clicked()
{
check_end_frame(ui->horizontalSlider->value());
}