Subversion Repositories seema-scanner

Rev

Rev 245 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9 jakw 1
#include "SMCaptureWorker.h"
2
 
41 jakw 3
#include "AlgorithmGrayCode.h"
99 jakw 4
#include "AlgorithmGrayCodeHorzVert.h"
128 jakw 5
#include "AlgorithmPhaseShiftTwoFreq.h"
200 jakw 6
#include "AlgorithmPhaseShiftTwoFreqHorzVert.h"
128 jakw 7
#include "AlgorithmPhaseShiftThreeFreq.h"
192 jakw 8
#include "AlgorithmPhaseShiftEmbedded.h"
123 jakw 9
#include "AlgorithmLineShift.h"
27 jakw 10
 
9 jakw 11
#include <QCoreApplication>
12
#include <QTime>
13
#include <QSettings>
27 jakw 14
#include <QtTest/QTest>
9 jakw 15
 
120 jakw 16
#include "cvtools.h"
17
 
9 jakw 18
void SMCaptureWorker::setup(){
19
 
23 jakw 20
    QSettings settings;
21
 
9 jakw 22
    // Create cameras
167 jakw 23
    int iNum0 = settings.value("camera0/interfaceNumber", -1).toInt();
24
    int cNum0 = settings.value("camera0/cameraNumber", -1).toInt();
207 flgw 25
    camera0.reset(CameraFactory::NewCamera(iNum0,cNum0,triggerModeSoftware));
169 jakw 26
 
27
    if(!camera0)
167 jakw 28
        return;
9 jakw 29
 
167 jakw 30
    int iNum1 = settings.value("camera1/interfaceNumber", -1).toInt();
31
    int cNum1 = settings.value("camera1/cameraNumber", -1).toInt();
207 flgw 32
    camera1.reset(CameraFactory::NewCamera(iNum1,cNum1,triggerModeSoftware));
169 jakw 33
 
34
    if(!camera1)
167 jakw 35
        return;
9 jakw 36
 
37
    // Set camera settings
38
    CameraSettings cameraSettings;
39
    cameraSettings.shutter = settings.value("camera/shutter", 16.666).toFloat();
40
    cameraSettings.gain = 0.0;
41
 
42
    camera0->setCameraSettings(cameraSettings);
43
    camera1->setCameraSettings(cameraSettings);
44
 
23 jakw 45
    // Start capturing
46
    camera0->startCapture();
47
    camera1->startCapture();
48
 
27 jakw 49
    // Create projector
164 raly 50
    //int screenNum = settings.value("projector/screenNumber", -1).toInt();
51
    int screenNum = settings.value("projector/screenNumber", 1).toInt();
27 jakw 52
    if(screenNum != -1)
207 flgw 53
        projector.reset(new ProjectorOpenGL(screenNum));
27 jakw 54
 
226 jakw 55
//    // Create rotation stage
56
//    for(int i = 0; i<10; i++){
57
//        try{
58
//            delete rotationStage.get();
59
//            rotationStage = std::make_shared<RotationStage>();
60
//            break;
61
//        }
62
//        catch (...){
63
//            std::cerr << "trying to setup rotation stage" << std::endl;
64
//        }
65
//    }
66
    rotationStage.reset();
67
    rotationStage.reset(new RotationStage());
51 jakw 68
 
41 jakw 69
    // Create Algorithm
137 jakw 70
    unsigned int screenCols, screenRows;
71
    projector->getScreenRes(&screenCols, &screenRows);
255 - 72
    std::cout << "Screen res: " << screenCols << " x " << screenRows << std::endl;
71 jakw 73
    codec = settings.value("algorithm", "GrayCode").toString();
74
    if(codec == "GrayCode")
207 flgw 75
        algorithm.reset(new AlgorithmGrayCode(screenCols, screenRows));
107 jakw 76
    else if(codec == "GrayCodeHorzVert")
207 flgw 77
        algorithm.reset(new AlgorithmGrayCodeHorzVert(screenCols, screenRows));
128 jakw 78
    else if(codec == "PhaseShiftTwoFreq")
207 flgw 79
        algorithm.reset(new AlgorithmPhaseShiftTwoFreq(screenCols, screenRows));
128 jakw 80
    else if(codec == "PhaseShiftThreeFreq")
207 flgw 81
        algorithm.reset(new AlgorithmPhaseShiftThreeFreq(screenCols, screenRows));
200 jakw 82
    else if(codec == "PhaseShiftTwoFreqHorzVert")
208 flgw 83
        algorithm.reset(new AlgorithmPhaseShiftTwoFreqHorzVert(screenCols, screenRows));
192 jakw 84
    else if(codec == "PhaseShiftEmbedded")
207 flgw 85
        algorithm.reset(new AlgorithmPhaseShiftEmbedded(screenCols, screenRows));
123 jakw 86
    else if(codec == "LineShift")
207 flgw 87
        algorithm.reset(new AlgorithmLineShift(screenCols, screenRows));
27 jakw 88
    else
74 jakw 89
        std::cerr << "SMCaptureWorker: invalid codec " << codec.toStdString() << std::endl;
27 jakw 90
 
91
    // Upload patterns to projector/GPU
41 jakw 92
    for(unsigned int i=0; i<algorithm->getNPatterns(); i++){
93
        cv::Mat pattern = algorithm->getEncodingPattern(i);
27 jakw 94
        projector->setPattern(i, pattern.ptr(), pattern.cols, pattern.rows);
95
    }
96
 
97
    delay = settings.value("trigger/delay", 50).toInt();
139 jakw 98
    stackingCalibration = settings.value("stacking/calibration", 1).toInt();
99
    stackingAcquisition= settings.value("stacking/acquisition", 1).toInt();
167 jakw 100
 
101
    setupSuccessful = true;
9 jakw 102
}
103
 
104
 
105
void SMCaptureWorker::doWork(){
106
 
167 jakw 107
    if(!setupSuccessful)
108
        return;
109
 
23 jakw 110
    working = true;
9 jakw 111
 
199 jakw 112
    cv::Mat checkerboard(8, 8, CV_8UC3);
113
    checkerboard.setTo(0);
114
    checkerboard.rowRange(0, 4).colRange(0, 4).setTo(cv::Vec3b(255,255,255));
115
    checkerboard.rowRange(4, 8).colRange(4, 8).setTo(cv::Vec3b(255,255,255));
116
 
9 jakw 117
    // Processing loop
113 jakw 118
//    QTime time;
119
//    time.start();
23 jakw 120
    while(working){
9 jakw 121
 
199 jakw 122
        if(focusingPattern)
123
            projector->displayTexture(checkerboard.ptr(), checkerboard.cols, checkerboard.rows);
124
        else
125
            projector->displayWhite();
27 jakw 126
 
199 jakw 127
 
159 jakw 128
        // prevent image acquisition timeout
207 flgw 129
        QTest::qSleep(100);
134 jakw 130
 
9 jakw 131
        CameraFrame frame;
132
 
23 jakw 133
        // trigger cameras
134
        camera0->trigger();
135
        camera1->trigger();
9 jakw 136
 
113 jakw 137
        // retrieve raw frames
23 jakw 138
        frame = camera0->getFrame();
139
        cv::Mat frameCV;
121 jakw 140
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
23 jakw 141
        frameCV = frameCV.clone();
121 jakw 142
//        cvtools::rshift(frameCV, 8);
143
//        frameCV.convertTo(frameCV, CV_8UC1);
23 jakw 144
        emit newFrame(0, frameCV);
9 jakw 145
 
23 jakw 146
        frame = camera1->getFrame();
121 jakw 147
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
23 jakw 148
        frameCV = frameCV.clone();
121 jakw 149
//        cvtools::rshift(frameCV, 8);
150
//        frameCV.convertTo(frameCV, CV_8UC1);
23 jakw 151
        emit newFrame(1, frameCV);
9 jakw 152
 
23 jakw 153
        //std::cout << "SMCaptureWorker idle " << time.restart() << "ms" << std::endl;
9 jakw 154
 
23 jakw 155
        // Process events e.g. perform a task
9 jakw 156
        QCoreApplication::processEvents();
157
    }
158
 
159
    emit finished();
23 jakw 160
}
9 jakw 161
 
23 jakw 162
void SMCaptureWorker::rotateTo(float angle){
223 flgw 163
    // TODO is this the right check
164
    if(!setupSuccessful || !rotationStage || !rotationStage->Handle)
167 jakw 165
        return;
166
 
223 flgw 167
    rotationStage->moveAbsolute(angle);
92 jakw 168
    while(rotationStage->isMoving()){
169
 
134 jakw 170
        // prevent grab timeout in flycapture
171
        QTest::qSleep(10);
172
 
92 jakw 173
        // trigger cameras
174
        camera0->trigger();
175
        camera1->trigger();
176
 
177
        // retrieve frames
178
        CameraFrame frame;
179
        frame = camera0->getFrame();
180
        cv::Mat frameCV;
121 jakw 181
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
92 jakw 182
        frameCV = frameCV.clone();
183
        emit newFrame(0, frameCV);
184
        frame = camera1->getFrame();
121 jakw 185
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
92 jakw 186
        frameCV = frameCV.clone();
187
        emit newFrame(1, frameCV);
188
    }
189
 
30 jakw 190
    emit rotatedTo(angle);
9 jakw 191
}
192
 
23 jakw 193
void SMCaptureWorker::acquireCalibrationSet(float angle){
223 flgw 194
    if(!setupSuccessful || !rotationStage || !rotationStage->Handle)
167 jakw 195
        return;
196
 
27 jakw 197
    if(angle != -1.0)
198
        rotateTo(angle);
199
 
245 jakw 200
    //projector->displayWhite();
199 jakw 201
 
134 jakw 202
    // just for safe measures
203
    QTest::qSleep(500);
204
 
23 jakw 205
    CameraFrame frame;
27 jakw 206
    SMCalibrationSet calibrationSet;
120 jakw 207
    cv::Mat frameCVStacked0(camera0->getFrameHeight(), camera0->getFrameWidth(), CV_32SC1, cv::Scalar(0));
208
    cv::Mat frameCVStacked1(camera1->getFrameHeight(), camera1->getFrameWidth(), CV_32SC1, cv::Scalar(0));
23 jakw 209
 
139 jakw 210
    for(int i=0; i<stackingCalibration; i++){
113 jakw 211
        // trigger cameras
212
        camera0->trigger();
213
        camera1->trigger();
23 jakw 214
 
113 jakw 215
        // retrieve frames
216
        frame = camera0->getFrame();
217
        cv::Mat frameCV;
121 jakw 218
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
113 jakw 219
        frameCV = frameCV.clone();
120 jakw 220
        cv::add(frameCV, frameCVStacked0, frameCVStacked0, cv::noArray(), CV_32SC1);
121 jakw 221
//cvtools::writeMat(frameCV, "frameCV.mat", "frameCV");
222
//cvtools::writeMat(frameCVStacked0, "frameCVStacked0.mat", "frameCVStacked0");
113 jakw 223
        emit newFrame(0, frameCV);
23 jakw 224
 
113 jakw 225
        frame = camera1->getFrame();
121 jakw 226
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
113 jakw 227
        frameCV = frameCV.clone();
120 jakw 228
        cv::add(frameCV, frameCVStacked1, frameCVStacked1, cv::noArray(), CV_32SC1);
23 jakw 229
 
113 jakw 230
        emit newFrame(1, frameCV);
23 jakw 231
 
113 jakw 232
    }
233
 
139 jakw 234
    frameCVStacked0.convertTo(frameCVStacked0, CV_8UC1, 1.0/stackingCalibration);
134 jakw 235
//cvtools::writeMat(frameCVStacked0, "frameCVStacked0a.mat", "frameCVStacked0a");
139 jakw 236
    frameCVStacked1.convertTo(frameCVStacked1, CV_8UC1, 1.0/stackingCalibration);
113 jakw 237
 
238
    calibrationSet.frame0 = frameCVStacked0;
239
    calibrationSet.frame1 = frameCVStacked1;
240
 
27 jakw 241
    calibrationSet.rotationAngle = rotationStage->getAngle();
23 jakw 242
 
243
    emit newCalibrationSet(calibrationSet);
9 jakw 244
}
245
 
30 jakw 246
void SMCaptureWorker::acquireCalibrationSets(std::vector<float> angles){
9 jakw 247
 
167 jakw 248
    if(!setupSuccessful)
249
        return;
250
 
251
    for(unsigned int i=0; i<angles.size(); i++)
30 jakw 252
        acquireCalibrationSet(angles[i]);
253
}
254
 
242 jakw 255
void SMCaptureWorker::acquireFrameSequenceLDR(SMFrameSequence &frameSequence){
27 jakw 256
 
245 jakw 257
 
258
 
27 jakw 259
    CameraFrame frame;
260
 
41 jakw 261
    for(unsigned int i=0; i<algorithm->getNPatterns(); i++){
27 jakw 262
 
263
        // display pattern
264
        projector->displayPattern(i);
265
 
266
        QTest::qSleep(delay);
267
 
120 jakw 268
        cv::Mat frameCVStacked0(camera0->getFrameHeight(), camera0->getFrameWidth(), CV_32SC1, cv::Scalar(0));
269
        cv::Mat frameCVStacked1(camera1->getFrameHeight(), camera1->getFrameWidth(), CV_32SC1, cv::Scalar(0));
139 jakw 270
        for(int i=0; i<stackingAcquisition; i++){
113 jakw 271
            // trigger cameras
272
            camera0->trigger();
273
            camera1->trigger();
27 jakw 274
 
113 jakw 275
            // retrieve frames
276
            frame = camera0->getFrame();
277
            cv::Mat frameCV;
121 jakw 278
            frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
113 jakw 279
            frameCV = frameCV.clone();
120 jakw 280
            cv::add(frameCV, frameCVStacked0, frameCVStacked0, cv::noArray(), CV_32SC1);
27 jakw 281
 
113 jakw 282
            emit newFrame(0, frameCV);
27 jakw 283
 
113 jakw 284
            frame = camera1->getFrame();
121 jakw 285
            frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
113 jakw 286
            frameCV = frameCV.clone();
120 jakw 287
            cv::add(frameCV, frameCVStacked1, frameCVStacked1, cv::noArray(), CV_32SC1);
27 jakw 288
 
113 jakw 289
            emit newFrame(1, frameCV);
27 jakw 290
 
113 jakw 291
        }
292
 
139 jakw 293
        frameCVStacked0.convertTo(frameCVStacked0, CV_8UC1, 1.0/stackingAcquisition);
294
        frameCVStacked1.convertTo(frameCVStacked1, CV_8UC1, 1.0/stackingAcquisition);
113 jakw 295
 
296
        frameSequence.frames0.push_back(frameCVStacked0);
297
        frameSequence.frames1.push_back(frameCVStacked1);
298
 
27 jakw 299
    }
300
 
242 jakw 301
 
302
}
303
 
304
void SMCaptureWorker::acquireFrameSequenceHDR(SMFrameSequence &frameSequence){
305
 
306
    QSettings settings;
307
    QString shuttersString = settings.value("camera/shuttersHDR").toString();
308
    QStringList list = shuttersString.split("/");
309
    std::vector<float> shutters(list.size());
310
    for(int i=0; i<list.size(); i++)
311
        shutters[i] = list[i].toFloat();
312
    if(shutters.empty()){
313
        std::cerr << "Could not read HDR shutter times" << std::endl;
314
        return;
315
    }
316
 
317
    int nShutters = shutters.size();
318
 
319
    CameraSettings cameraSettings;
320
 
321
    for(int i=0; i<nShutters; i++){
255 - 322
        std::cout << "Acquiring HDR frame sequence "
323
                  << i + 1 << "/" << nShutters << std::endl;
324
        std::cout << "Shutter time [ms] "
325
                  << shutters[i] << std::endl;
242 jakw 326
 
327
        // Set camera shutter
328
        cameraSettings.shutter = shutters[i];
329
 
330
        // assert that shutter is given in ms
331
        assert(cameraSettings.shutter < 2000.0);
332
 
333
        camera0->setCameraSettings(cameraSettings);
334
        camera1->setCameraSettings(cameraSettings);
335
 
336
        // Project/acquire sequence
245 jakw 337
        SMFrameSequence frameSequenceLDR;
338
        acquireFrameSequenceLDR(frameSequenceLDR);
242 jakw 339
 
245 jakw 340
        frameSequence.frames0.insert(frameSequence.frames0.end(), frameSequenceLDR.frames0.begin(), frameSequenceLDR.frames0.end());
341
        frameSequence.frames1.insert(frameSequence.frames1.end(), frameSequenceLDR.frames1.begin(), frameSequenceLDR.frames1.end());
242 jakw 342
 
244 jakw 343
    }
344
 
245 jakw 345
    frameSequence.shutters = shutters;
242 jakw 346
 
347
    // Set camera shutter back to default
348
    cameraSettings.shutter = settings.value("shutter", 66.666).toFloat();
349
 
350
    camera0->setCameraSettings(cameraSettings);
351
    camera1->setCameraSettings(cameraSettings);
352
 
353
}
354
 
355
 
356
void SMCaptureWorker::acquireFrameSequence(float angle){
357
 
358
    if(!setupSuccessful)
359
        return;
360
 
361
    if(int(angle) != -1.0)
362
        rotateTo(angle);
363
 
364
    SMFrameSequence frameSequence;
365
 
366
    QSettings settings;
255 - 367
    bool HDR = settings.value("camera/shuttersHDR").toString() != QString("");
368
    if(HDR)
242 jakw 369
        acquireFrameSequenceHDR(frameSequence);
370
    else
371
        acquireFrameSequenceLDR(frameSequence);
372
 
373
 
223 flgw 374
    if(rotationStage && rotationStage->Handle)// TODO is this the right check
207 flgw 375
        frameSequence.rotationAngle = rotationStage->getAngle();
376
    else
377
        frameSequence.rotationAngle = 0;
378
 
27 jakw 379
    frameSequence.codec = codec;
380
 
381
    emit newFrameSequence(frameSequence);
382
 
92 jakw 383
    projector->displayWhite();
27 jakw 384
}
385
 
386
 
30 jakw 387
void SMCaptureWorker::acquireFrameSequences(std::vector<float> angles){
23 jakw 388
 
167 jakw 389
    if(!setupSuccessful)
390
        return;
391
 
392
    for(unsigned int i=0; i<angles.size(); i++)
30 jakw 393
        acquireFrameSequence(angles[i]);
394
}
395
 
23 jakw 396
void SMCaptureWorker::abort(){}
397
 
398
void SMCaptureWorker::stopWork(){
399
    working = false;
400
}
401