-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.cpp
560 lines (476 loc) · 21.4 KB
/
processor.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
/*
* MVL Stereo Processor: processor
* Copyright (C) 2014-2016 Rok Mandeljc
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "processor.h"
#include "debug.h"
#include "utils.h"
#include "source_image.h"
#include "source_video.h"
#include "source_vrms.h"
#include <stereo-pipeline/pipeline.h>
#include <stereo-pipeline/plugin_manager.h>
#include <stereo-pipeline/plugin_factory.h>
#include <stereo-pipeline/utils.h>
#include <opencv2/imgcodecs.hpp>
namespace MVL {
namespace StereoProcessor {
Processor::Processor ()
{
}
Processor::~Processor ()
{
}
// *********************************************************************
// * Main function *
// *********************************************************************
void Processor::run ()
{
// Parse command-line arguments
parseCommandLine();
// Display options
qCInfo(mvlStereoProcessor) << "";
qCInfo(mvlStereoProcessor) << "Input file:" << inputFile;
qCInfo(mvlStereoProcessor) << "Input file type:" << inputFileType;
qCInfo(mvlStereoProcessor) << "";
qCInfo(mvlStereoProcessor) << "Stereo calibration file:" << stereoCalibrationFile;
qCInfo(mvlStereoProcessor) << "Stereo method config file:" << stereoMethodFile;
qCInfo(mvlStereoProcessor) << "";
qCInfo(mvlStereoProcessor) << "Frame range(s):";
for (const FrameRange &range : frameRanges) {
qCInfo(mvlStereoProcessor) << " *" << range.start << "to" << range.end << "with step" << range.step;
}
qCInfo(mvlStereoProcessor) << "";
qCInfo(mvlStereoProcessor) << "Output frame format(s):";
for (const QString &format : outputFrames) {
qCInfo(mvlStereoProcessor) << " *" << format;
}
qCInfo(mvlStereoProcessor) << "Output rectified format(s):";
for (const QString &format : outputRectified) {
qCInfo(mvlStereoProcessor) << " *" << format;
}
qCInfo(mvlStereoProcessor) << "Output disparity format(s):";
for (const QString &format : outputDisparity) {
qCInfo(mvlStereoProcessor) << " *" << format;
}
qCInfo(mvlStereoProcessor) << "Output points format(s):";
for (const QString &format : outputPoints) {
qCInfo(mvlStereoProcessor) << " *" << format;
}
qCInfo(mvlStereoProcessor) << "";
// Validate options
validateOptions();
// Setup pipeline
setupPipeline();
// Process
for (const FrameRange &range : frameRanges) {
qCInfo(mvlStereoProcessor) << "";
qCInfo(mvlStereoProcessor) << "Processing frame range:" << range.start << "to" << range.end << "with step" << range.step;
processFrameRange(range);
qCInfo(mvlStereoProcessor) << "Done!";
}
}
// *********************************************************************
// * Main processing loop *
// *********************************************************************
void Processor::processFrameRange (const FrameRange &range)
{
cv::Mat imageLeft, imageRight;
cv::Mat rectifiedLeft, rectifiedRight;
cv::Mat disparity;
cv::Mat points;
int numDisparities;
// Variable map for filename formatting
QHash<QString, QVariant> variableMap;
variableMap["rangeStart"] = range.start;
variableMap["rangeEnd"] = range.end;
variableMap["rangeStep"] = range.step;
for (int frame = range.start; range.end < 0 || frame <= range.end; frame += range.step) {
variableMap["f"] = frame;
qCDebug(mvlStereoProcessor) << "Processing frame" << frame;
// *** Grab frames ***
try {
inputSource->getFrame(frame, imageLeft, imageRight);
} catch (const QString &error) {
// If range is open-ended, just break; otherwise, propagate
// the error
if (range.end < 0) {
qCInfo(mvlStereoProcessor) << "Reached end of sequence!";
break;
} else {
throw error;
}
}
// Export frames
for (const QString &format : outputFrames) {
// Left
variableMap["s"] = "L";
QString filenameLeft = Utils::formatString(format, variableMap);
Utils::ensureParentDirectoryExists(filenameLeft);
if (!cv::imwrite(filenameLeft.toStdString(), imageLeft)) {
throw QString("Failed to write output image '%1'").arg(filenameLeft);
}
// Right
variableMap["s"] = "R";
QString filenameRight = Utils::formatString(format, variableMap);
Utils::ensureParentDirectoryExists(filenameRight);
if (!cv::imwrite(filenameRight.toStdString(), imageRight)) {
throw QString("Failed to write output image '%1'").arg(filenameRight);
}
}
// *** Undistort frames ***
if (stereoRectification) {
// Rectify
stereoRectification->rectifyImagePair(imageLeft, imageRight, rectifiedLeft, rectifiedRight);
} else {
// Passthrough (assume images are already rectified)
rectifiedLeft = imageLeft;
rectifiedRight = imageRight;
}
// Export rectified frames
for (const QString &format : outputRectified) {
// Left
variableMap["s"] = "L";
QString filenameLeft = Utils::formatString(format, variableMap);
Utils::ensureParentDirectoryExists(filenameLeft);
if (!cv::imwrite(filenameLeft.toStdString(), rectifiedLeft)) {
throw QString("Failed to write output image '%1'").arg(filenameLeft);
}
// Right
variableMap["s"] = "R";
QString filenameRight = Utils::formatString(format, variableMap);
Utils::ensureParentDirectoryExists(filenameRight);
if (!cv::imwrite(filenameRight.toStdString(), rectifiedRight)) {
throw QString("Failed to write output image '%1'").arg(filenameRight);
}
}
// *** Compute disparity ***
if (stereoMethod) {
// Compute disparity
qobject_cast<MVL::StereoToolbox::Pipeline::StereoMethod *>(stereoMethod)->computeDisparity(rectifiedLeft, rectifiedRight, disparity, numDisparities);
// Export disparity
for (const QString &format : outputDisparity) {
QString filename = Utils::formatString(format, variableMap);
QString ext = QFileInfo(filename).completeSuffix();
Utils::ensureParentDirectoryExists(filename);
if (ext == "xml" || ext == "yml" || ext == "yaml") {
// Save raw disparity in OpenCV storage format
try {
cv::FileStorage fs(filename.toStdString(), cv::FileStorage::WRITE);
fs << "disparity" << disparity;
} catch (const cv::Exception &error) {
throw QString("Failed to save matrix to file %1: %2").arg(filename).arg(QString::fromStdString(error.what()));
}
} else if (ext == "bin") {
// Save raw disparity in custom binary matrix format
try {
MVL::StereoToolbox::Pipeline::Utils::writeMatrixToBinaryFile(disparity, filename);
} catch (const QString &error) {
throw QString("Failed to save binary file %1: %2").arg(filename).arg(error);
}
} else {
// Save disparity visualization as image using cv::imwrite
try {
cv::Mat visualization;
MVL::StereoToolbox::Pipeline::Utils::createColorCodedDisparityCpu(disparity, visualization, numDisparities);
cv::imwrite(filename.toStdString(), visualization);
} catch (const cv::Exception &error) {
throw QString("Failed to save image %1: %2").arg(filename).arg(QString::fromStdString(error.what()));
}
}
}
} else {
continue; // No further steps possible if stereo method is not active
}
// *** Reproject point cloud ***
if (stereoReprojection) {
stereoReprojection->reprojectDisparity(disparity, points);
// Export point cloud
for (const QString &format : outputPoints) {
QString filename = Utils::formatString(format, variableMap);
QString ext = QFileInfo(filename).completeSuffix();
Utils::ensureParentDirectoryExists(filename);
if (ext == "xml" || ext == "yml" || ext == "yaml") {
// Save raw matrix in OpenCV storage format
try {
cv::FileStorage fs(filename.toStdString(), cv::FileStorage::WRITE);
fs << "points" << points;
} catch (const cv::Exception &error) {
throw QString("Failed to save matrix to file %1: %2").arg(filename).arg(QString::fromStdString(error.what()));
}
} else if (ext == "bin") {
// Save raw matrix in custom binary matrix format
try {
MVL::StereoToolbox::Pipeline::Utils::writeMatrixToBinaryFile(points, filename);
} catch (const QString &error) {
throw QString("Failed to save binary file %1: %2").arg(filename).arg(error);
}
} else if (ext == "pcd") {
try {
MVL::StereoToolbox::Pipeline::Utils::writePointCloudToPcdFile(rectifiedLeft, points, filename, true);
} catch (const QString &error) {
throw QString("Failed to save PCD file %1: %2").arg(filename).arg(error);
}
} else {
throw QString("Invalid output format for reprojection: %1").arg(ext);
}
}
}
}
}
// *********************************************************************
// * Pipeline setup *
// *********************************************************************
void Processor::setupPipeline ()
{
qCDebug(mvlStereoProcessor) << "Setting up pipeline...";
// Create input source
if (inputFileType == "image") {
inputSource = new SourceImage(inputFile);
} else if (inputFileType == "vrms") {
inputSource = new SourceVrms(inputFile);
} else if (inputFileType == "video") {
inputSource = new SourceVideo(inputFile);
} else {
throw QString("Unhandled input source type: %1").arg(inputFileType);
}
// Create rectitifaction and load stereo calibration
if (!stereoCalibrationFile.isEmpty()) {
qCDebug(mvlStereoProcessor) << "Setting up rectification:" << qPrintable(stereoCalibrationFile);
stereoRectification = new MVL::StereoToolbox::Pipeline::Rectification();
try {
stereoRectification->loadStereoCalibration(stereoCalibrationFile);
} catch (const QString &error) {
throw QString("Failed to load stereo calibration: %1").arg(error);
} catch (const std::exception &error) {
throw QString("Failed to load stereo calibration: %1").arg(error.what());
}
}
// Create stereo method
if (!stereoMethodFile.isEmpty()) {
qCDebug(mvlStereoProcessor) << "Setting up stereo method:" << qPrintable(stereoMethodFile);
// Open config file
cv::FileStorage storage(stereoMethodFile.toStdString(), cv::FileStorage::READ);
if (!storage.isOpened()) {
throw QString("Failed to open OpenCV file storage on '%1'").arg(stereoMethodFile);
}
std::string methodName;
storage["MethodName"] >> methodName;
// Traverse list of plugins and try to find stereo method based
// on its name
MVL::StereoToolbox::Pipeline::PluginManager pluginManager;
for (QObject *pluginFactoryObject : pluginManager.getAvailablePlugins()) {
MVL::StereoToolbox::Pipeline::PluginFactory *pluginFactory = qobject_cast<MVL::StereoToolbox::Pipeline::PluginFactory *>(pluginFactoryObject);
if (pluginFactory->getPluginType() == MVL::StereoToolbox::Pipeline::PluginFactory::PluginStereoMethod) {
if (pluginFactory->getShortName().toStdString() == methodName) {
stereoMethod = pluginFactory->createObject();
break;
}
}
}
if (!stereoMethod) {
throw QString("Plugin for stereo method '%1' not found!").arg(QString::fromStdString(methodName));
}
// Load config
try {
qobject_cast<MVL::StereoToolbox::Pipeline::StereoMethod *>(stereoMethod)->loadParameters(stereoMethodFile);
} catch (const QString &error) {
throw QString("Failed to load method parameters: %1").arg(error);
}
}
// Create reprojection (only if we have rectification available!)
if (stereoRectification) {
qCDebug(mvlStereoProcessor) << "Setting up reprojection object...";
stereoReprojection = new MVL::StereoToolbox::Pipeline::Reprojection();
stereoReprojection->setReprojectionMatrix(stereoRectification->getReprojectionMatrix());
}
}
// *********************************************************************
// * Command-line parser *
// *********************************************************************
Processor::FrameRange Processor::parseFrameRange (const QString &range) const
{
// Split on colon(s)
QStringList tokens = range.split(":");
if (tokens.size() != 2 && tokens.size() != 3) {
throw QString("Invalid frame range string '%1'").arg(range);
}
int start = 0;
int step = 1;
int end = -1;
bool ok;
if (tokens.size() == 2) {
// Two-token version: start:end
if (!tokens[0].isEmpty()) {
start = tokens[0].toInt(&ok);
if (!ok) {
throw QString("Invalid number token in frame range: '%1'").arg(tokens[0]);
}
}
if (!tokens[1].isEmpty()) {
end = tokens[1].toInt(&ok);
if (!ok) {
throw QString("Invalid number token in frame range: '%1'").arg(tokens[1]);
}
}
} else {
// Three-token version: start:step:end
if (!tokens[0].isEmpty()) {
start = tokens[0].toInt(&ok);
if (!ok) {
throw QString("Invalid number token in frame range: '%1'").arg(tokens[0]);
}
}
if (!tokens[1].isEmpty()) {
step = tokens[1].toInt(&ok);
if (!ok) {
throw QString("Invalid number token in frame range: '%1'").arg(tokens[1]);
}
}
if (!tokens[2].isEmpty()) {
end = tokens[2].toInt(&ok);
if (!ok) {
throw QString("Invalid number token in frame range: '%1'").arg(tokens[2]);
}
}
}
return FrameRange({ start, step, end });
}
void Processor::parseCommandLine ()
{
// *** Setup command-line parser ***
parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
parser.setApplicationDescription("MVL Stereo Processor");
parser.addHelpOption();
parser.addVersionOption();
// Input file
parser.addPositionalArgument("input-file", QCoreApplication::translate("main", "Input file."));
// Input type
QCommandLineOption optionInputType("input-type",
QCoreApplication::translate("main", "Input file type (image, video, vrms)."),
QCoreApplication::translate("main", "type"));
parser.addOption(optionInputType);
// Stereo calibration
QCommandLineOption optionStereoCalibration("stereo-calibration",
QCoreApplication::translate("main", "Stereo calibration file."),
QCoreApplication::translate("main", "file"));
parser.addOption(optionStereoCalibration);
// Stereo method
QCommandLineOption optionStereoMethod("stereo-method",
QCoreApplication::translate("main", "Stereo method configuration file."),
QCoreApplication::translate("main", "file"));
parser.addOption(optionStereoMethod);
// Frame range
QCommandLineOption optionFrameRange(QStringList() << "f" << "frame-range",
QCoreApplication::translate("main", "Frame range to process."),
QCoreApplication::translate("main", "start:step:end"));
optionFrameRange.setDefaultValue("0:1:-1");
parser.addOption(optionFrameRange);
// Output: frames
QCommandLineOption optionOutputFrames("output-frames",
QCoreApplication::translate("main", "Output format for extracted frames."),
QCoreApplication::translate("main", "format"));
parser.addOption(optionOutputFrames);
// Output: rectified
QCommandLineOption optionOutputRectified("output-rectified",
QCoreApplication::translate("main", "Output format for rectified frames."),
QCoreApplication::translate("main", "format"));
parser.addOption(optionOutputRectified);
// Output: disparity
QCommandLineOption optionOutputDisparity("output-disparity",
QCoreApplication::translate("main", "Output format for disparity."),
QCoreApplication::translate("main", "format"));
parser.addOption(optionOutputDisparity);
// Output: points
QCommandLineOption optionOutputPoints("output-points",
QCoreApplication::translate("main", "Output format for point cloud."),
QCoreApplication::translate("main", "format"));
parser.addOption(optionOutputPoints);
// *** Process ***
parser.process(*qApp);
// *** Gather options ***
inputFileType = parser.value(optionInputType);
stereoCalibrationFile = parser.value(optionStereoCalibration);
stereoMethodFile = parser.value(optionStereoMethod);
outputFrames = parser.values(optionOutputFrames);
outputRectified = parser.values(optionOutputRectified);
outputDisparity = parser.values(optionOutputDisparity);
outputPoints = parser.values(optionOutputPoints);
// Parse frame range(s)
for (const QString &range : parser.values(optionFrameRange)) {
frameRanges.append(parseFrameRange(range));
}
// We require exactly one positional argument
QStringList positionalArguments = parser.positionalArguments();
if (positionalArguments.size() != 1) {
throw QString("Exactly one positional argument (input-file) is required; %1 were provided!").arg(positionalArguments.size());
}
inputFile = positionalArguments[0];
}
void Processor::validateOptions ()
{
// Validate input file type string, if provided
if (!inputFileType.isEmpty()) {
if (inputFileType != "image" &&
inputFileType != "video" &&
inputFileType != "vrms") {
throw QString("Invalid input file type specified: '%1'").arg(inputFileType);
}
} else {
QString suffix = QFileInfo(inputFile).suffix();
if (suffix == "jpeg" || suffix == "jpg" || suffix == "png" || suffix == "ppm" || suffix == "bmp") {
inputFileType = "image";
} else if (suffix == "vrms") {
inputFileType = "vrms";
} else if (suffix == "avi" || suffix == "mp4" || suffix == "mkv" || suffix == "mpg") {
inputFileType = "video";
} else {
throw QString("Unrecognized input file type; unhandled suffix '%1'").arg(suffix);
}
qCDebug(mvlStereoProcessor) << "Auto-determined input type:" << inputFileType;
}
// Is some output required?
if (outputFrames.isEmpty() && outputRectified.isEmpty() &&
outputDisparity.isEmpty() && outputPoints.isEmpty()) {
throw QString("No output formats specified; nothing to do!");
}
// Stereo calibration is needed for rectified images, disparity, and reprojected points
if (stereoCalibrationFile.isEmpty()) {
if (!outputRectified.isEmpty()) {
throw QString("Rectified images output requires stereo calibration!");
}
// If stereo calibration is not given, we assume that images are
// already rectified
//if (!outputDisparity.isEmpty()) {
// throw QString("Disparity output requires stereo calibration!");
//}
if (!outputPoints.isEmpty()) {
throw QString("Reprojected points output requires stereo calibration!");
}
}
// Stereo method is needed for disparity image and reprojected points
if (stereoMethodFile.isEmpty()) {
if (!outputDisparity.isEmpty()) {
throw QString("Disparity output requires stereo method!");
}
if (!outputPoints.isEmpty()) {
throw QString("Reprojected points output requires stereo method!");
}
}
}
} // StereoProcessor
} // MVL