Subversion Repositories seema-scanner

Rev

Rev 142 | Rev 144 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jakw 1
#include "SMScanner.h"
2
#include "ui_SMScanner.h"
3
 
4 jakw 4
#include <QMetaObject>
36 jakw 5
#include <QFileDialog>
67 jakw 6
#include <QMessageBox>
141 jakw 7
#include <QProgressDialog>
4 jakw 8
 
44 jakw 9
#include <pcl/io/pcd_io.h>
10
#include <pcl/io/ascii_io.h>
11
#include <pcl/io/ply_io.h>
12
#include <pcl/io/png_io.h>
13
#include <pcl/io/vtk_io.h>
14
#include <vtkPolyDataWriter.h>
15
#include <pcl/conversions.h>
16
 
139 jakw 17
#include "cvtools.h"
18
 
27 jakw 19
SMScanner::SMScanner(QWidget *parent) :QMainWindow(parent), ui(new Ui::SMScanner),
67 jakw 20
                                        calibrationReviewMode(false), captureReviewMode(false), lastCaptureId(-1){
4 jakw 21
 
27 jakw 22
    // Register metatypes
23
    qRegisterMetaType<cv::Mat>("cv::Mat");
24
    qRegisterMetaType< std::vector<cv::Mat> >("std::vector<cv::Mat>");
30 jakw 25
    qRegisterMetaType< std::vector<float> >("std::vector<float>");
27 jakw 26
    qRegisterMetaType<SMCalibrationSet>("SMCalibrationSet");
27
    qRegisterMetaType<SMCalibrationParameters>("SMCalibrationParameters");
28
    qRegisterMetaTypeStreamOperators<SMCalibrationParameters>("SMCalibrationParameters");
29
    qRegisterMetaType< std::vector<SMCalibrationSet> >("std::vector<SMCalibrationSet>");
30
    qRegisterMetaType<SMFrameSequence>("SMFrameSequence");
42 jakw 31
    qRegisterMetaType< std::vector<SMFrameSequence> >("std::vector<SMFrameSequence>");
45 jakw 32
    qRegisterMetaType<pcl::PointCloud<pcl::PointXYZRGB>::Ptr>("pcl::PointCloud<pcl::PointXYZRGB>::Ptr");
42 jakw 33
    qRegisterMetaType<SMPointCloud>("SMPointCloud");
27 jakw 34
 
42 jakw 35
    // Setup ui (requires stream operators registered)
36
    ui->setupUi(this);
37
 
4 jakw 38
    // Restore geometry
39
    this->restoreGeometry(settings.value("geometry/mainwindow").toByteArray());
40
    this->restoreState(settings.value("state/mainwindow").toByteArray());
41
 
42
    // Set up threads
43
    captureWorker = new SMCaptureWorker;
44
    captureWorkerThread = new QThread(this);
45
    captureWorkerThread->setObjectName("captureWorkerThread");
46
    captureWorker->moveToThread(captureWorkerThread);
47
    captureWorkerThread->start();
48
 
49
    // Connections
23 jakw 50
    connect(captureWorker, SIGNAL(newFrame(unsigned int, cv::Mat)), this, SLOT(onReceiveFrame(unsigned int, cv::Mat)));
27 jakw 51
    connect(captureWorker, SIGNAL(newCalibrationSet(SMCalibrationSet)), this, SLOT(onReceiveCalibrationSet(SMCalibrationSet)));
52
    connect(captureWorker, SIGNAL(newFrameSequence(SMFrameSequence)), this, SLOT(onReceiveFrameSequence(SMFrameSequence)));
30 jakw 53
    connect(captureWorker, SIGNAL(rotatedTo(float)), this, SLOT(onReceiveRotatedTo(float)));
4 jakw 54
 
55
    // Start capturing
56
    QMetaObject::invokeMethod(captureWorker, "setup");
57
    QMetaObject::invokeMethod(captureWorker, "doWork");
58
 
1 jakw 59
}
60
 
23 jakw 61
void SMScanner::onReceiveFrame(unsigned int camId, cv::Mat frame){
4 jakw 62
 
23 jakw 63
    if(camId == 0){
25 jakw 64
        if(!calibrationReviewMode)
65
            ui->calibrationCamera0Widget->showImageCV(frame);
27 jakw 66
        if(!captureReviewMode)
67
            ui->captureCamera0Widget->showImageCV(frame);
23 jakw 68
    } else if(camId == 1){
25 jakw 69
        if(!calibrationReviewMode)
70
            ui->calibrationCamera1Widget->showImageCV(frame);
27 jakw 71
        if(!captureReviewMode)
72
            ui->captureCamera1Widget->showImageCV(frame);
23 jakw 73
    }
74
}
4 jakw 75
 
23 jakw 76
void SMScanner::on_actionPreferences_triggered(){
4 jakw 77
 
74 jakw 78
    connect(&preferenceDialog, SIGNAL(accepted()), this, SLOT(onPreferencesChanged()));
73 jakw 79
 
4 jakw 80
    preferenceDialog.show();
81
}
82
 
73 jakw 83
void SMScanner::onPreferencesChanged(){
84
 
137 jakw 85
    // Stop capturing thread
86
    connect(captureWorker, SIGNAL(finished()), captureWorker, SLOT(deleteLater()));
87
    connect(captureWorker, SIGNAL(finished()), captureWorkerThread, SLOT(quit()));
88
    QMetaObject::invokeMethod(captureWorker, "stopWork");
89
    captureWorkerThread->quit();
90
    captureWorkerThread->wait();
73 jakw 91
 
137 jakw 92
    // Restart capture worker
93
    captureWorker = new SMCaptureWorker;
94
    captureWorkerThread = new QThread(this);
95
    captureWorkerThread->setObjectName("captureWorkerThread");
96
    captureWorker->moveToThread(captureWorkerThread);
97
    captureWorkerThread->start();
73 jakw 98
 
137 jakw 99
    connect(captureWorker, SIGNAL(newFrame(unsigned int, cv::Mat)), this, SLOT(onReceiveFrame(unsigned int, cv::Mat)));
100
    connect(captureWorker, SIGNAL(newCalibrationSet(SMCalibrationSet)), this, SLOT(onReceiveCalibrationSet(SMCalibrationSet)));
101
    connect(captureWorker, SIGNAL(newFrameSequence(SMFrameSequence)), this, SLOT(onReceiveFrameSequence(SMFrameSequence)));
102
    connect(captureWorker, SIGNAL(rotatedTo(float)), this, SLOT(onReceiveRotatedTo(float)));
74 jakw 103
 
137 jakw 104
    QMetaObject::invokeMethod(captureWorker, "setup");
105
    QMetaObject::invokeMethod(captureWorker, "doWork");
74 jakw 106
 
137 jakw 107
//    QMessageBox::warning(this, "Information", "You must quit and restart the program for new settings to take effect!", QMessageBox::Ok);
74 jakw 108
 
73 jakw 109
}
110
 
4 jakw 111
void SMScanner::closeEvent(QCloseEvent *event){
112
 
23 jakw 113
    // Stop capturing thread
114
    connect(captureWorker, SIGNAL(finished()), captureWorker, SLOT(deleteLater()));
115
    connect(captureWorker, SIGNAL(finished()), captureWorkerThread, SLOT(quit()));
116
    QMetaObject::invokeMethod(captureWorker, "stopWork");
117
    captureWorkerThread->quit();
118
    captureWorkerThread->wait();
119
 
4 jakw 120
    // Save window geometry
121
    settings.setValue("geometry/mainwindow", this->saveGeometry());
122
    settings.setValue("state/mainwindow", this->saveState());
123
 
124
    event->accept();
125
 
126
}
127
 
2 jakw 128
SMScanner::~SMScanner(){
1 jakw 129
    delete ui;
130
}
4 jakw 131
 
23 jakw 132
void SMScanner::on_singleCalibrationButton_clicked(){
133
 
25 jakw 134
    // If in review mode, go back to aquisition mode
135
    if(calibrationReviewMode){
26 jakw 136
        ui->calibrationListWidget->clearSelection();
25 jakw 137
        ui->singleCalibrationButton->setText("Single Aquisition");
138
        ui->batchCalibrationButton->setText("Batch Aquisition");
139
        calibrationReviewMode = false;
140
        return;
141
    }
142
 
27 jakw 143
    QMetaObject::invokeMethod(captureWorker, "acquireCalibrationSet", Q_ARG(float, -1.0));
23 jakw 144
 
145
}
146
 
30 jakw 147
 
148
void SMScanner::on_batchCalibrationButton_clicked(){
149
 
150
    // If in review mode, go back to aquisition mode
151
    if(calibrationReviewMode){
152
        ui->calibrationListWidget->clearSelection();
153
        ui->singleCalibrationButton->setText("Single Aquisition");
154
        ui->batchCalibrationButton->setText("Batch Aquisition");
155
        calibrationReviewMode = false;
156
        return;
157
    }
158
 
159
    // Construct vector of angles
160
    int angleStart = ui->calibrationBatchStartSpinBox->value();
161
    int angleEnd = ui->calibrationBatchEndSpinBox->value();
162
    int angleStep = ui->calibrationBatchStepSpinBox->value();
163
 
164
    if(angleStart > angleEnd)
165
        angleEnd += 360;
166
 
167
    std::vector<float> angles;
168
    for(int i=angleStart; i<=angleEnd; i+=angleStep)
169
        angles.push_back(i % 360);
170
 
171
    QMetaObject::invokeMethod(captureWorker, "acquireCalibrationSets", Q_ARG(std::vector<float>, angles));
172
 
173
    std::cout << "Aquiring sets at ";
75 jakw 174
    for(unsigned int i=0; i<angles.size(); i++)
30 jakw 175
        std::cout << angles[i] << " ";
176
    std::cout << " degrees" <<std::endl;
177
}
178
 
179
 
23 jakw 180
void SMScanner::on_calibrationRotationDial_sliderReleased(){
27 jakw 181
 
23 jakw 182
    float angle = ui->calibrationRotationDial->value();
183
    std::cout << "Rotation stage target: " << angle << std::endl;
184
    QMetaObject::invokeMethod(captureWorker, "rotateTo", Q_ARG(float, angle));
27 jakw 185
 
186
    ui->captureRotationDial->setValue(ui->calibrationRotationDial->value());
23 jakw 187
}
24 jakw 188
 
27 jakw 189
void SMScanner::onReceiveCalibrationSet(SMCalibrationSet calibrationSet){
123 jakw 190
 
191
    int id = ui->calibrationListWidget->count();
192
    calibrationSet.id = id;
193
 
23 jakw 194
    calibrationData.push_back(calibrationSet);
25 jakw 195
 
196
    // Add identifier to list
123 jakw 197
    QListWidgetItem* item = new QListWidgetItem(QString("Calibration Set %1 -- %2 deg").arg(id).arg(calibrationSet.rotationAngle), ui->calibrationListWidget);
25 jakw 198
    item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
199
    item->setCheckState(Qt::Checked);
200
    ui->calibrationListWidget->addItem(item);
201
 
202
    // Set enabled checkmark
203
    if(calibrationData.size() >= 2)
204
        ui->calibrateButton->setEnabled(true);
23 jakw 205
}
25 jakw 206
 
27 jakw 207
void SMScanner::on_calibrateButton_clicked(){
25 jakw 208
 
29 jakw 209
    // disable ui elements
210
    ui->calibrateButton->setEnabled(false);
211
    ui->calibrationFrame->setEnabled(false);
27 jakw 212
 
213
    // set checked flags
75 jakw 214
    for(unsigned int i=0; i<calibrationData.size(); i++){
27 jakw 215
        calibrationData[i].checked = (ui->calibrationListWidget->item(i)->checkState() == Qt::Checked);
216
    }
217
 
218
//    SMCalibrationWorker calibrationWorker;
219
//    calibrationWorker.performCalibration(calibrationData);
220
 
221
    // Set up calibration thread
78 jakw 222
    calibrationWorker = new SMCalibrationWorker;
27 jakw 223
    calibrationWorkerThread = new QThread(this);
224
    calibrationWorkerThread->setObjectName("calibrationWorkerThread");
123 jakw 225
    calibrationWorker->moveToThread(calibrationWorkerThread);
27 jakw 226
    calibrationWorkerThread->start();
227
 
228
    // Connections
28 jakw 229
    connect(calibrationWorker, SIGNAL(newSetProcessed(int)), this, SLOT(onCalibrationSetProcessed(int)));
29 jakw 230
    connect(calibrationWorker, SIGNAL(newFrameResult(int,int,bool,cv::Mat)), this, SLOT(onCalibrationFrameResult(int,int,bool,cv::Mat)));
27 jakw 231
    connect(calibrationWorker, SIGNAL(done()), this, SLOT(onCalibrationDone()));
32 jakw 232
    connect(calibrationWorker, SIGNAL(done()), ui->pointCloudWidget, SLOT(updateCalibrationParameters()));
27 jakw 233
    connect(calibrationWorker, SIGNAL(done()), calibrationWorkerThread, SLOT(quit()));
234
    connect(calibrationWorker, SIGNAL(done()), calibrationWorker, SLOT(deleteLater()));
235
 
32 jakw 236
    // Start calibration
27 jakw 237
    QMetaObject::invokeMethod(calibrationWorker, "performCalibration", Q_ARG(std::vector<SMCalibrationSet>, calibrationData));
238
 
239
}
240
 
30 jakw 241
 
242
 
27 jakw 243
void SMScanner::onCalibrationSetProcessed(int idx){
244
 
137 jakw 245
    if(calibrationData[idx].frame0Result.empty() && calibrationData[idx].frame1Result.empty())
246
        ui->calibrationListWidget->item(idx)->setCheckState(Qt::Unchecked);
247
 
27 jakw 248
    ui->calibrationListWidget->setCurrentRow(idx);
249
}
250
 
251
void SMScanner::onCalibrationDone(){
252
 
253
    std::cout << "Calibration done!" << std::endl;
29 jakw 254
    ui->calibrateButton->setEnabled(true);
255
    ui->calibrationFrame->setEnabled(true);
27 jakw 256
 
257
}
258
 
259
void SMScanner::on_calibrationListWidget_itemSelectionChanged(){
260
 
261
    // if selection is just cleared
262
    if(ui->calibrationListWidget->selectedItems().empty())
263
        return;
264
 
265
    int currentRow = ui->calibrationListWidget->currentRow();
266
 
25 jakw 267
    calibrationReviewMode = true;
268
    ui->singleCalibrationButton->setText("Live View");
269
    ui->batchCalibrationButton->setText("Live View");
270
 
271
    if(!calibrationData[currentRow].frame0Result.empty())
272
        ui->calibrationCamera0Widget->showImageCV(calibrationData[currentRow].frame0Result);
273
    else
274
        ui->calibrationCamera0Widget->showImageCV(calibrationData[currentRow].frame0);
275
 
276
    if(!calibrationData[currentRow].frame1Result.empty())
277
        ui->calibrationCamera1Widget->showImageCV(calibrationData[currentRow].frame1Result);
278
    else
279
        ui->calibrationCamera1Widget->showImageCV(calibrationData[currentRow].frame1);
27 jakw 280
 
281
//     std::cout << "on_calibrationListWidget_itemSelectionChanged" << std::endl;
25 jakw 282
}
283
 
29 jakw 284
void SMScanner::onCalibrationFrameResult(int idx, int camID, bool success, cv::Mat result){
25 jakw 285
 
137 jakw 286
    if(success){
29 jakw 287
        if(camID == 0)
288
            calibrationData[idx].frame0Result = result;
289
        else if(camID == 1)
290
            calibrationData[idx].frame1Result = result;
291
    }
27 jakw 292
 
293
}
294
 
295
void SMScanner::on_singleCaptureButton_clicked(){
296
 
297
    // If in review mode, go back to aquisition mode
298
    if(captureReviewMode){
299
        ui->captureTreeWidget->clearSelection();
300
        ui->singleCaptureButton->setText("Single Aquisition");
301
        ui->batchCaptureButton->setText("Batch Aquisition");
302
        captureReviewMode = false;
303
        return;
25 jakw 304
    }
305
 
27 jakw 306
    QMetaObject::invokeMethod(captureWorker, "acquireFrameSequence", Q_ARG(float, -1.0));
25 jakw 307
 
308
}
26 jakw 309
 
27 jakw 310
 
30 jakw 311
void SMScanner::on_batchCaptureButton_clicked(){
312
 
313
    // If in review mode, go back to aquisition mode
314
    if(captureReviewMode){
315
        ui->captureTreeWidget->clearSelection();
316
        ui->singleCaptureButton->setText("Single Aquisition");
317
        ui->batchCaptureButton->setText("Batch Aquisition");
318
        captureReviewMode = false;
319
        return;
320
    }
321
 
322
    // Construct vector of angles
323
    int angleStart = ui->captureBatchStartSpinBox->value();
324
    int angleEnd = ui->captureBatchEndSpinBox->value();
325
    int angleStep = ui->captureBatchStepSpinBox->value();
326
 
327
    if(angleStart > angleEnd)
328
        angleEnd += 360;
329
 
330
    std::vector<float> angles;
331
    for(int i=angleStart; i<=angleEnd; i+=angleStep)
332
        angles.push_back(i % 360);
333
 
334
    std::cout << "Aquiring sequences at ";
75 jakw 335
    for(unsigned int i=0; i<angles.size(); i++)
30 jakw 336
        std::cout << angles[i] << " ";
337
    std::cout << " degrees" <<std::endl;
338
 
339
    QMetaObject::invokeMethod(captureWorker, "acquireFrameSequences", Q_ARG(std::vector<float>, angles));
340
}
341
 
27 jakw 342
void SMScanner::on_captureRotationDial_sliderReleased(){
343
 
344
    float angle = ui->captureRotationDial->value();
345
    std::cout << "Rotation stage target: " << angle << std::endl;
346
    QMetaObject::invokeMethod(captureWorker, "rotateTo", Q_ARG(float, angle));
347
 
348
    ui->calibrationRotationDial->setValue(ui->captureRotationDial->value());
26 jakw 349
}
27 jakw 350
 
351
void SMScanner::onReceiveFrameSequence(SMFrameSequence frameSequence){
352
 
67 jakw 353
    int id = lastCaptureId + 1;
354
    lastCaptureId += 1;
355
 
45 jakw 356
    frameSequence.id = id;
357
 
27 jakw 358
    captureData.push_back(frameSequence);
359
 
360
    // Add identifier to list
361
    QTreeWidgetItem* item = new QTreeWidgetItem(ui->captureTreeWidget);
45 jakw 362
    item->setText(0, QString("Frame Sequence %1 -- %2 deg").arg(id).arg(frameSequence.rotationAngle));
139 jakw 363
    //item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
67 jakw 364
    item->setData(0, Qt::UserRole, QPoint(captureData.size()-1, -1));
139 jakw 365
    //item->setCheckState(0, Qt::Checked);
27 jakw 366
    //ui->captureTreeWidget->addItem(item);
367
 
103 jakw 368
    for(unsigned int i=0; i<frameSequence.frames0.size(); i++){
27 jakw 369
        QTreeWidgetItem* subItem = new QTreeWidgetItem(item);
370
        subItem->setText(0, QString("frames %1").arg(i));
45 jakw 371
        subItem->setData(0, Qt::UserRole, QPoint(id, i));
27 jakw 372
    }
41 jakw 373
 
374
    ui->reconstructButton->setEnabled(true);
27 jakw 375
}
376
 
377
void SMScanner::on_captureTreeWidget_itemSelectionChanged(){
378
 
379
    // if selection is just cleared
380
    if(ui->captureTreeWidget->selectedItems().empty())
381
        return;
382
 
383
    QTreeWidgetItem *item = ui->captureTreeWidget->currentItem();
384
 
385
    captureReviewMode = true;
386
 
387
    ui->singleCaptureButton->setText("Live View");
388
    ui->batchCaptureButton->setText("Live View");
389
 
390
    QPoint idx = item->data(0, Qt::UserRole).toPoint();
391
 
392
    if(idx.y() != -1){
103 jakw 393
        ui->captureCamera0Widget->showImageCV(captureData[idx.x()].frames0[idx.y()]);
394
        ui->captureCamera1Widget->showImageCV(captureData[idx.x()].frames1[idx.y()]);
27 jakw 395
    }
396
 
397
//     std::cout << "on_captureTreeWidget_itemSelectionChanged" << std::endl;
398
}
30 jakw 399
 
400
 
401
void SMScanner::onReceiveRotatedTo(float angle){
402
 
403
    // update ui with new position
404
    ui->calibrationRotationDial->setValue(angle);
405
    ui->captureRotationDial->setValue(angle);
406
 
407
}
36 jakw 408
 
409
void SMScanner::on_actionExport_Sets_triggered(){
410
 
143 jakw 411
    QString dirName = QFileDialog::getExistingDirectory(this, "Export calibration sets", QString());
412
 
413
    QProgressDialog progressDialog("Exporting Sets...", "Cancel", 0, 100, this);
414
    progressDialog.setWindowModality(Qt::WindowModal);
415
    progressDialog.setMinimumDuration(1000);
416
 
417
    progressDialog.show();
112 jakw 418
    cv::Mat frameBGR;
75 jakw 419
    for(unsigned int i=0; i<calibrationData.size(); i++){
36 jakw 420
        QString fileName = QString("%1/frame0_%2.png").arg(dirName).arg(i);
136 jakw 421
 
143 jakw 422
        progressDialog.setValue(100.0*i/calibrationData.size());
423
 
136 jakw 424
        // Convert to BGR
425
        if(calibrationData[i].frame0.channels() == 1)
426
            cv::cvtColor(calibrationData[i].frame0, frameBGR, CV_BayerBG2BGR);
427
        else
428
            cv::cvtColor(calibrationData[i].frame1, frameBGR, CV_RGB2BGR);
429
 
36 jakw 430
        cv::imwrite(fileName.toStdString(), frameBGR);
431
        fileName = QString("%1/frame1_%2.png").arg(dirName).arg(i);
136 jakw 432
 
433
        // Convert to BGR
434
        if(calibrationData[i].frame1.channels() == 1)
435
            cv::cvtColor(calibrationData[i].frame1, frameBGR, CV_BayerBG2BGR);
436
        else
437
            cv::cvtColor(calibrationData[i].frame1, frameBGR, CV_RGB2BGR);
438
 
36 jakw 439
        cv::imwrite(fileName.toStdString(), frameBGR);
137 jakw 440
 
441
        // Necessary to prevent pileup of video frame signals
112 jakw 442
        QCoreApplication::processEvents();
143 jakw 443
 
444
        if(progressDialog.wasCanceled())
445
            return;
36 jakw 446
    }
447
 
448
}
449
 
450
void SMScanner::on_actionExport_Sequences_triggered(){
451
 
143 jakw 452
    QString dirName = QFileDialog::getExistingDirectory(this, "Export frame sequences", QString());
453
 
454
    QProgressDialog progressDialog("Exporting Sequences...", "Cancel", 0, 100, this);
142 jakw 455
    progressDialog.setWindowModality(Qt::WindowModal);
141 jakw 456
    progressDialog.setMinimumDuration(1000);
457
 
142 jakw 458
    progressDialog.show();
141 jakw 459
 
112 jakw 460
    cv::Mat frameBGR;
103 jakw 461
    for(unsigned int i=0; i<captureData.size(); i++){
141 jakw 462
 
103 jakw 463
        QString seqDirName = QString("%1/sequence_%2").arg(dirName).arg(i);
464
        if(!QDir().mkdir(seqDirName))
465
            std::cerr << "Could not create directory " << seqDirName.toStdString() << std::endl;
466
        for(unsigned int j=0; j<captureData[i].frames0.size(); j++){
142 jakw 467
 
143 jakw 468
            progressDialog.setValue(100.0*i/captureData.size() + 100.0/captureData.size()*j/captureData[i].frames0.size());
142 jakw 469
 
470
            QString fileName0 = QString("%1/frames0_%2.png").arg(seqDirName).arg(j);
113 jakw 471
            // Convert Bayer to rgb (png needs BGR order)
472
            cv::cvtColor(captureData[i].frames0[j], frameBGR, CV_BayerBG2BGR);
142 jakw 473
            cv::imwrite(fileName0.toStdString(), frameBGR);
137 jakw 474
 
475
            // Necessary to prevent pileup of video frame signals
112 jakw 476
            QCoreApplication::processEvents();
142 jakw 477
 
478
            QString fileName1 = QString("%1/frames1_%2.png").arg(seqDirName).arg(j);
113 jakw 479
            // Convert Bayer to rgb (png needs BGR order)
480
            cv::cvtColor(captureData[i].frames1[j], frameBGR, CV_BayerBG2BGR);
142 jakw 481
            cv::imwrite(fileName1.toStdString(), frameBGR);
137 jakw 482
 
483
            // Necessary to prevent pileup of video frame signals
112 jakw 484
            QCoreApplication::processEvents();
142 jakw 485
            if(progressDialog.wasCanceled())
486
                return;
103 jakw 487
        }
488
    }
36 jakw 489
}
41 jakw 490
 
115 jakw 491
void SMScanner::on_actionExport_White_Frames_triggered(){
492
 
143 jakw 493
    QString dirName = QFileDialog::getExistingDirectory(this, "Export frame sequences", QString());
494
 
495
    QProgressDialog progressDialog("Exporting White Frames...", "Cancel", 0, 100, this);
496
    progressDialog.setWindowModality(Qt::WindowModal);
497
    progressDialog.setMinimumDuration(1000);
498
 
115 jakw 499
    cv::Mat frameBGR;
500
    for(unsigned int i=0; i<captureData.size(); i++){
143 jakw 501
 
502
        progressDialog.setValue(100.0*i/captureData.size());
503
 
115 jakw 504
        QString seqDirName = QString("%1/sequence_%2").arg(dirName).arg(i);
505
        if(!QDir().mkdir(seqDirName))
506
            std::cerr << "Could not create directory " << seqDirName.toStdString() << std::endl;
507
 
508
        QString fileName = QString("%1/frames0_0.png").arg(seqDirName);
509
        // Convert Bayer to rgb (png needs BGR order)
510
        cv::cvtColor(captureData[i].frames0[0], frameBGR, CV_BayerBG2BGR);
511
        cv::imwrite(fileName.toStdString(), frameBGR);
128 jakw 512
        // Necessary to prevent memory pileup
115 jakw 513
        QCoreApplication::processEvents();
514
 
515
        fileName = QString("%1/frames1_0.png").arg(seqDirName);
516
        // Convert Bayer to rgb (png needs BGR order)
517
        cv::cvtColor(captureData[i].frames1[0], frameBGR, CV_BayerBG2BGR);
518
        cv::imwrite(fileName.toStdString(), frameBGR);
128 jakw 519
        // Necessary to prevent memory pileup
115 jakw 520
        QCoreApplication::processEvents();
143 jakw 521
        if(progressDialog.wasCanceled())
522
            return;
115 jakw 523
    }
524
 
525
}
526
 
41 jakw 527
void SMScanner::on_reconstructButton_clicked(){
528
 
529
    // Set up reconstruction thread
530
    reconstructionWorker = new SMReconstructionWorker;
531
    reconstructionWorkerThread = new QThread(this);
532
    reconstructionWorkerThread->setObjectName("reconstructionWorkerThread");
533
    reconstructionWorker->moveToThread(reconstructionWorkerThread);
534
    reconstructionWorkerThread->start();
535
 
536
    // Connections
42 jakw 537
    connect(reconstructionWorker, SIGNAL(newPointCloud(SMPointCloud)), this, SLOT(onNewPointCloud(SMPointCloud)));
41 jakw 538
    connect(reconstructionWorker, SIGNAL(done()), reconstructionWorkerThread, SLOT(quit()));
539
    connect(reconstructionWorker, SIGNAL(done()), reconstructionWorker, SLOT(deleteLater()));
540
 
541
    // Start reconstructing
42 jakw 542
    QMetaObject::invokeMethod(reconstructionWorker, "setup");
41 jakw 543
 
75 jakw 544
    for(unsigned int i=0; i<captureData.size(); i++){
139 jakw 545
        if(!captureData[i].reconstructed){ // & (ui->captureTreeWidget->topLevelItem(i)->checkState(0) == Qt::Checked))
546
            captureData[i].reconstructed = true;
45 jakw 547
            QMetaObject::invokeMethod(reconstructionWorker, "reconstructPointCloud", Q_ARG(SMFrameSequence, captureData[i]));
139 jakw 548
        }
45 jakw 549
    }
550
 
41 jakw 551
}
552
 
42 jakw 553
void SMScanner::onNewPointCloud(SMPointCloud smCloud){
41 jakw 554
 
45 jakw 555
    int id = smCloud.id;
556
 
139 jakw 557
//    for(int i=0; i<captureData.size(); i++){
558
//        if(captureData[i].id == id)
559
//            captureData[i].reconstructed = true;
560
//    }
82 jakw 561
 
41 jakw 562
    pointCloudData.push_back(smCloud);
563
 
42 jakw 564
    // Add identifier to list
43 jakw 565
    QListWidgetItem* item = new QListWidgetItem(QString("Point Cloud %1 -- %2 deg").arg(id).arg(smCloud.rotationAngle), ui->pointCloudsListWidget);
42 jakw 566
    item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
44 jakw 567
    item->setData(Qt::UserRole, QVariant(id));
42 jakw 568
    item->setCheckState(Qt::Checked);
44 jakw 569
 
42 jakw 570
    ui->pointCloudsListWidget->addItem(item);
571
 
71 jakw 572
//    ui->pointCloudWidget->addPointCloud(smCloud, id);
41 jakw 573
}
42 jakw 574
 
575
 
44 jakw 576
 
577
void SMScanner::on_actionExport_Point_Clouds_triggered(){
578
 
579
    QString directory = QFileDialog::getExistingDirectory(this, "Export Point Clouds", QString());
580
 
581
//    //  Non native file dialog
582
//    QFileDialog saveDirectoryDialog(this, "Export Point Clouds", QString(), "*.pcd;;*.ply;;*.vtk;;*.png;;*.txt");
583
//    saveDirectoryDialog.setDefaultSuffix("ply");
584
//    saveDirectoryDialog.setFileMode(QFileDialog::Directory);
585
//    saveDirectoryDialog.exec();
586
//    QString directory = saveDirectoryDialog.directory().path();
587
//    QString type = saveDirectoryDialog.selectedNameFilter();
588
 
143 jakw 589
    QProgressDialog progressDialog("Exporting White Frames...", "Cancel", 0, 100, this);
590
    progressDialog.setWindowModality(Qt::WindowModal);
591
    progressDialog.setMinimumDuration(1000);
592
 
44 jakw 593
    // save point clouds in ply format
75 jakw 594
    for(unsigned int i=0; i<pointCloudData.size(); i++){
143 jakw 595
 
596
        progressDialog.setValue(100.0*i/pointCloudData.size());
597
 
44 jakw 598
        QString fileName = QString("%1/pointcloud_%2.ply").arg(directory).arg(i);
128 jakw 599
        pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr pointCloudPCL(pointCloudData[i].pointCloud);
44 jakw 600
        //pcl::io::savePLYFileBinary(fileName.toStdString(), *pointCloudPCL);
601
        pcl::PLYWriter w;
602
        // Write to ply in binary without camera
128 jakw 603
        w.write<pcl::PointXYZRGBNormal> (fileName.toStdString(), *pointCloudPCL, true, false);
143 jakw 604
 
605
        QCoreApplication::processEvents();
606
        if(progressDialog.wasCanceled())
607
            return;
44 jakw 608
    }
609
 
610
    // save meshlab aln project file
611
    std::ofstream s(QString("%1/alignment.aln").arg(directory).toLocal8Bit());
612
    s << pointCloudData.size() << std::endl;
75 jakw 613
    for(unsigned int i=0; i<pointCloudData.size(); i++){
44 jakw 614
        QString fileName = QString("pointcloud_%1.ply").arg(i);
615
        s << fileName.toStdString() << std::endl << "#" << std::endl;
616
        cv::Mat Tr = cv::Mat::eye(4, 4, CV_32F);
617
        cv::Mat(pointCloudData[i].R.t()).copyTo(Tr.colRange(0, 3).rowRange(0, 3));
618
        cv::Mat(-pointCloudData[i].R.t()*pointCloudData[i].T).copyTo(Tr.col(3).rowRange(0, 3));
619
        for(int j=0; j<Tr.rows; j++){
620
            for(int k=0; k<Tr.cols; k++){
621
                s << Tr.at<float>(j,k) << " ";
622
            }
623
            s << std::endl;
624
        }
625
    }
626
    s << "0" << std::flush;
627
    s.close();
628
}
629
 
630
void SMScanner::on_pointCloudsListWidget_itemChanged(QListWidgetItem *item){
631
 
632
    int id = item->data(Qt::UserRole).toInt();
633
 
634
    if(item->checkState() == Qt::Checked)
635
        ui->pointCloudWidget->addPointCloud(pointCloudData[id], id);
636
    else
637
        ui->pointCloudWidget->removePointCloud(id);
638
 
639
}
640
 
641
void SMScanner::on_actionExport_Parameters_triggered(){
642
 
42 jakw 643
    QString fileName = QFileDialog::getSaveFileName(this, "Export calibration parameters", QString(), "*.xml");
644
    QFileInfo info(fileName);
645
    QString type = info.suffix();
646
    if(type == ""){
647
        fileName.append(".xml");
648
    }
649
 
650
    SMCalibrationParameters calibration = settings.value("calibration/parameters").value<SMCalibrationParameters>();
651
    calibration.exportToXML(fileName);
652
}
67 jakw 653
 
654
void SMScanner::on_actionClear_Sequences_triggered(){
655
 
656
    int res = QMessageBox::question(this, "Clear Captured Sequences", "Clear all captured data?", QMessageBox::Ok, QMessageBox::Cancel);
657
 
658
    if(res == QMessageBox::Ok){
659
        captureData.clear();
660
        ui->captureTreeWidget->clear();
661
    }
662
}
115 jakw 663
 
664
 
135 jakw 665
 
666
void SMScanner::on_actionImport_Parameters_triggered(){
667
 
668
    QString fileName = QFileDialog::getOpenFileName(this, "Import calibration parameters", QString(), "*.xml");
669
    QFileInfo info(fileName);
670
    QString type = info.suffix();
136 jakw 671
    if(type != "xml"){
135 jakw 672
        std::cerr << "Error: calibration parameters must be in .xml file." << std::endl;
673
        return;
674
    }
675
 
676
    SMCalibrationParameters cal;
677
    cal.importFromXML(fileName);
678
 
679
    settings.setValue("calibration/parameters",  QVariant::fromValue(cal));
680
    ui->pointCloudWidget->updateCalibrationParameters();
139 jakw 681
 
682
    std::cout << "Imported calibration parameters " << fileName.toStdString() << std::endl;
135 jakw 683
}
684
 
685
void SMScanner::on_actionImport_Sets_triggered(){
686
 
687
    QString dirName = QFileDialog::getExistingDirectory(this, "Import calibration sets", QString());
688
 
689
    QDir dir(dirName);
690
    QStringList fileNames0 = dir.entryList(QStringList("frame0_*.png"));
691
    QStringList fileNames1 = dir.entryList(QStringList("frame1_*.png"));
692
 
693
    if(fileNames0.empty() || fileNames1.empty() || fileNames0.count() != fileNames1.count()){
694
        std::cerr << "Error: could not load calibration sets. Directory must contain .png files for both cameras." << std::endl;
695
        return;
696
    }
697
 
698
    calibrationData.clear();
699
    ui->calibrationListWidget->clear();
700
 
143 jakw 701
    QProgressDialog progressDialog("Importing Sets...", "Cancel", 0, 100, this);
702
    progressDialog.setWindowModality(Qt::WindowModal);
703
    progressDialog.setMinimumDuration(1000);
704
 
135 jakw 705
    int nSets = fileNames0.size();
706
    for(unsigned int i=0; i<nSets; i++){
707
 
143 jakw 708
        progressDialog.setValue(100.0*i/nSets);
709
 
135 jakw 710
        SMCalibrationSet calibrationSet;
711
 
140 jakw 712
        QString fileName0 = QString("%1/frame0_%2.png").arg(dirName).arg(i);
139 jakw 713
        cv::Mat frame0BGR = cv::imread(fileName0.toStdString());
714
        cvtools::cvtColorBGRToBayerBG(frame0BGR, calibrationSet.frame0);
135 jakw 715
 
140 jakw 716
        QString fileName1 = QString("%1/frame1_%2.png").arg(dirName).arg(i);
139 jakw 717
        cv::Mat frame1BGR = cv::imread(fileName1.toStdString());
718
        cvtools::cvtColorBGRToBayerBG(frame1BGR, calibrationSet.frame1);
719
 
720
 
135 jakw 721
        int id = ui->calibrationListWidget->count();
722
        calibrationSet.id = id;
723
 
724
        calibrationData.push_back(calibrationSet);
725
 
726
        // Add identifier to list
727
        QListWidgetItem* item = new QListWidgetItem(QString("Calibration Set %1 -- %2 deg").arg(id).arg(calibrationSet.rotationAngle), ui->calibrationListWidget);
728
        item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
140 jakw 729
        item->setCheckState(Qt::Unchecked);
135 jakw 730
        ui->calibrationListWidget->addItem(item);
731
 
732
        QCoreApplication::processEvents();
143 jakw 733
        if(progressDialog.wasCanceled())
734
            return;
135 jakw 735
    }
736
 
737
    // Set enabled checkmark
738
    if(calibrationData.size() >= 2)
739
        ui->calibrateButton->setEnabled(true);
740
}
139 jakw 741
 
742
void SMScanner::on_actionImport_Sequences_triggered(){
743
 
744
    // NOTE: we do not know which algorithm was used!!!
143 jakw 745
 
139 jakw 746
    QString dirName = QFileDialog::getExistingDirectory(this, "Import captured sequences", QString());
747
 
748
    QDir dir(dirName);
749
    QStringList sequenceDirNames = dir.entryList(QStringList("sequence_*"));
750
 
751
    if(sequenceDirNames.empty()){
752
        std::cerr << "Error: could not find sequences." << std::endl;
753
        return;
754
    }
755
 
756
    captureData.clear();
757
    ui->captureTreeWidget->clear();
758
 
143 jakw 759
    QProgressDialog progressDialog("Importing Sequences...", "Cancel", 0, 100, this);
760
    progressDialog.setWindowModality(Qt::WindowModal);
761
    progressDialog.setMinimumDuration(1000);
762
 
139 jakw 763
    for(int s=0; s<sequenceDirNames.count(); s++){
764
 
765
        QDir sequenceDir(QString("%1/%2").arg(dirName).arg(sequenceDirNames.at(s)));
766
 
767
        QStringList fileNames0 = sequenceDir.entryList(QStringList("frames0_*.png"));
768
        QStringList fileNames1 = sequenceDir.entryList(QStringList("frames1_*.png"));
769
 
770
        if(fileNames0.empty() || fileNames1.empty() || fileNames0.count() != fileNames1.count()){
771
            std::cerr << "Error: could not load sequence. Directory must contain .png files for both cameras." << std::endl;
772
            return;
773
        }
774
 
775
        int nFrames = fileNames0.size();
776
 
777
        SMFrameSequence sequence;
778
 
779
        for(int f=0; f<nFrames; f++){
780
 
143 jakw 781
            progressDialog.setValue(100.0*s/sequenceDirNames.count() + 100.0/sequenceDirNames.count()*f/nFrames);
782
 
139 jakw 783
            cv::Mat frame0BGR, frame0BayerBG, frame1BGR, frame1BayerBG;
784
 
785
            QString fileName0 = QString("%1/%2/frames0_%3.png").arg(dirName).arg(sequenceDirNames.at(s)).arg(f);
786
            frame0BGR = cv::imread(fileName0.toStdString());
787
            cvtools::cvtColorBGRToBayerBG(frame0BGR, frame0BayerBG);
788
 
789
            QString fileName1 = QString("%1/%2/frames1_%3.png").arg(dirName).arg(sequenceDirNames.at(s)).arg(f);
790
            frame1BGR = cv::imread(fileName1.toStdString());
791
            cvtools::cvtColorBGRToBayerBG(frame1BGR, frame1BayerBG);
792
 
793
            sequence.frames0.push_back(frame0BayerBG);
794
            sequence.frames1.push_back(frame1BayerBG);
795
 
796
            QCoreApplication::processEvents();
143 jakw 797
            if(progressDialog.wasCanceled())
798
                return;
139 jakw 799
        }
143 jakw 800
 
801
        // Assign whatever algorithm is configured
139 jakw 802
        sequence.codec = settings.value("algorithm").toString();
803
        sequence.rotationAngle = 0;
804
        sequence.reconstructed = false;
805
 
806
        onReceiveFrameSequence(sequence);
807
    }
808
 
809
}
810
 
811
void SMScanner::on_actionClear_Point_Clouds_triggered(){
812
 
813
    int res = QMessageBox::question(this, "Clear Reconstructed Point Clouds", "Clear all reconstructed point clouds?", QMessageBox::Ok, QMessageBox::Cancel);
814
 
815
    if(res == QMessageBox::Ok){
816
 
817
        pointCloudData.clear();
818
        ui->pointCloudsListWidget->clear();
819
        ui->pointCloudWidget->removeAllPointClouds();
820
 
821
        for(int i=0; i<captureData.size(); i++)
822
            captureData[i].reconstructed = false;
823
    }
824
}