Subversion Repositories seema-scanner

Rev

Rev 208 | Rev 226 | Go to most recent revision | 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);
159 jakw 43
 
9 jakw 44
    camera1->setCameraSettings(cameraSettings);
45
 
23 jakw 46
    // Start capturing
47
    camera0->startCapture();
48
    camera1->startCapture();
49
 
27 jakw 50
    // Create projector
164 raly 51
    //int screenNum = settings.value("projector/screenNumber", -1).toInt();
52
    int screenNum = settings.value("projector/screenNumber", 1).toInt();
27 jakw 53
    if(screenNum != -1)
207 flgw 54
        projector.reset(new ProjectorOpenGL(screenNum));
27 jakw 55
 
51 jakw 56
    // Create rotation stage
223 flgw 57
    for(int i = 0; i<10; i++){
207 flgw 58
        try{
223 flgw 59
            delete rotationStage.get();
60
            rotationStage = std::make_shared<RotationStage>();
207 flgw 61
            break;
62
        }
223 flgw 63
        catch (...){
207 flgw 64
            std::cerr << "trying to setup rotation stage" << std::endl;
65
        }
66
    }
51 jakw 67
 
41 jakw 68
    // Create Algorithm
137 jakw 69
    unsigned int screenCols, screenRows;
70
    projector->getScreenRes(&screenCols, &screenRows);
71 jakw 71
    codec = settings.value("algorithm", "GrayCode").toString();
72
    if(codec == "GrayCode")
207 flgw 73
        algorithm.reset(new AlgorithmGrayCode(screenCols, screenRows));
107 jakw 74
    else if(codec == "GrayCodeHorzVert")
207 flgw 75
        algorithm.reset(new AlgorithmGrayCodeHorzVert(screenCols, screenRows));
128 jakw 76
    else if(codec == "PhaseShiftTwoFreq")
207 flgw 77
        algorithm.reset(new AlgorithmPhaseShiftTwoFreq(screenCols, screenRows));
128 jakw 78
    else if(codec == "PhaseShiftThreeFreq")
207 flgw 79
        algorithm.reset(new AlgorithmPhaseShiftThreeFreq(screenCols, screenRows));
200 jakw 80
    else if(codec == "PhaseShiftTwoFreqHorzVert")
208 flgw 81
        algorithm.reset(new AlgorithmPhaseShiftTwoFreqHorzVert(screenCols, screenRows));
192 jakw 82
    else if(codec == "PhaseShiftEmbedded")
207 flgw 83
        algorithm.reset(new AlgorithmPhaseShiftEmbedded(screenCols, screenRows));
123 jakw 84
    else if(codec == "LineShift")
207 flgw 85
        algorithm.reset(new AlgorithmLineShift(screenCols, screenRows));
27 jakw 86
    else
74 jakw 87
        std::cerr << "SMCaptureWorker: invalid codec " << codec.toStdString() << std::endl;
27 jakw 88
 
89
    // Upload patterns to projector/GPU
41 jakw 90
    for(unsigned int i=0; i<algorithm->getNPatterns(); i++){
91
        cv::Mat pattern = algorithm->getEncodingPattern(i);
27 jakw 92
        projector->setPattern(i, pattern.ptr(), pattern.cols, pattern.rows);
93
    }
94
 
95
    delay = settings.value("trigger/delay", 50).toInt();
139 jakw 96
    stackingCalibration = settings.value("stacking/calibration", 1).toInt();
97
    stackingAcquisition= settings.value("stacking/acquisition", 1).toInt();
167 jakw 98
 
99
    setupSuccessful = true;
9 jakw 100
}
101
 
102
 
103
void SMCaptureWorker::doWork(){
104
 
167 jakw 105
    if(!setupSuccessful)
106
        return;
107
 
23 jakw 108
    working = true;
9 jakw 109
 
199 jakw 110
    cv::Mat checkerboard(8, 8, CV_8UC3);
111
    checkerboard.setTo(0);
112
    checkerboard.rowRange(0, 4).colRange(0, 4).setTo(cv::Vec3b(255,255,255));
113
    checkerboard.rowRange(4, 8).colRange(4, 8).setTo(cv::Vec3b(255,255,255));
114
 
9 jakw 115
    // Processing loop
113 jakw 116
//    QTime time;
117
//    time.start();
23 jakw 118
    while(working){
9 jakw 119
 
199 jakw 120
        if(focusingPattern)
121
            projector->displayTexture(checkerboard.ptr(), checkerboard.cols, checkerboard.rows);
122
        else
123
            projector->displayWhite();
27 jakw 124
 
199 jakw 125
 
159 jakw 126
        // prevent image acquisition timeout
207 flgw 127
        QTest::qSleep(100);
134 jakw 128
 
9 jakw 129
        CameraFrame frame;
130
 
23 jakw 131
        // trigger cameras
132
        camera0->trigger();
133
        camera1->trigger();
9 jakw 134
 
113 jakw 135
        // retrieve raw frames
23 jakw 136
        frame = camera0->getFrame();
137
        cv::Mat frameCV;
121 jakw 138
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
23 jakw 139
        frameCV = frameCV.clone();
121 jakw 140
//        cvtools::rshift(frameCV, 8);
141
//        frameCV.convertTo(frameCV, CV_8UC1);
23 jakw 142
        emit newFrame(0, frameCV);
9 jakw 143
 
23 jakw 144
        frame = camera1->getFrame();
121 jakw 145
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
23 jakw 146
        frameCV = frameCV.clone();
121 jakw 147
//        cvtools::rshift(frameCV, 8);
148
//        frameCV.convertTo(frameCV, CV_8UC1);
23 jakw 149
        emit newFrame(1, frameCV);
9 jakw 150
 
23 jakw 151
        //std::cout << "SMCaptureWorker idle " << time.restart() << "ms" << std::endl;
9 jakw 152
 
23 jakw 153
        // Process events e.g. perform a task
9 jakw 154
        QCoreApplication::processEvents();
155
    }
156
 
157
    emit finished();
23 jakw 158
}
9 jakw 159
 
23 jakw 160
void SMCaptureWorker::rotateTo(float angle){
223 flgw 161
    // TODO is this the right check
162
    if(!setupSuccessful || !rotationStage || !rotationStage->Handle)
167 jakw 163
        return;
164
 
223 flgw 165
    rotationStage->moveAbsolute(angle);
92 jakw 166
    while(rotationStage->isMoving()){
167
 
134 jakw 168
        // prevent grab timeout in flycapture
169
        QTest::qSleep(10);
170
 
92 jakw 171
        // trigger cameras
172
        camera0->trigger();
173
        camera1->trigger();
174
 
175
        // retrieve frames
176
        CameraFrame frame;
177
        frame = camera0->getFrame();
178
        cv::Mat frameCV;
121 jakw 179
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
92 jakw 180
        frameCV = frameCV.clone();
181
        emit newFrame(0, frameCV);
182
        frame = camera1->getFrame();
121 jakw 183
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
92 jakw 184
        frameCV = frameCV.clone();
185
        emit newFrame(1, frameCV);
186
    }
187
 
30 jakw 188
    emit rotatedTo(angle);
9 jakw 189
}
190
 
23 jakw 191
void SMCaptureWorker::acquireCalibrationSet(float angle){
223 flgw 192
    if(!setupSuccessful || !rotationStage || !rotationStage->Handle)
167 jakw 193
        return;
194
 
27 jakw 195
    if(angle != -1.0)
196
        rotateTo(angle);
197
 
199 jakw 198
    projector->displayWhite();
199
 
134 jakw 200
    // just for safe measures
201
    QTest::qSleep(500);
202
 
23 jakw 203
    CameraFrame frame;
27 jakw 204
    SMCalibrationSet calibrationSet;
120 jakw 205
    cv::Mat frameCVStacked0(camera0->getFrameHeight(), camera0->getFrameWidth(), CV_32SC1, cv::Scalar(0));
206
    cv::Mat frameCVStacked1(camera1->getFrameHeight(), camera1->getFrameWidth(), CV_32SC1, cv::Scalar(0));
23 jakw 207
 
139 jakw 208
    for(int i=0; i<stackingCalibration; i++){
113 jakw 209
        // trigger cameras
210
        camera0->trigger();
211
        camera1->trigger();
23 jakw 212
 
113 jakw 213
        // retrieve frames
214
        frame = camera0->getFrame();
215
        cv::Mat frameCV;
121 jakw 216
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
113 jakw 217
        frameCV = frameCV.clone();
120 jakw 218
        cv::add(frameCV, frameCVStacked0, frameCVStacked0, cv::noArray(), CV_32SC1);
121 jakw 219
//cvtools::writeMat(frameCV, "frameCV.mat", "frameCV");
220
//cvtools::writeMat(frameCVStacked0, "frameCVStacked0.mat", "frameCVStacked0");
113 jakw 221
        emit newFrame(0, frameCV);
23 jakw 222
 
113 jakw 223
        frame = camera1->getFrame();
121 jakw 224
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
113 jakw 225
        frameCV = frameCV.clone();
120 jakw 226
        cv::add(frameCV, frameCVStacked1, frameCVStacked1, cv::noArray(), CV_32SC1);
23 jakw 227
 
113 jakw 228
        emit newFrame(1, frameCV);
23 jakw 229
 
113 jakw 230
    }
231
 
139 jakw 232
    frameCVStacked0.convertTo(frameCVStacked0, CV_8UC1, 1.0/stackingCalibration);
134 jakw 233
//cvtools::writeMat(frameCVStacked0, "frameCVStacked0a.mat", "frameCVStacked0a");
139 jakw 234
    frameCVStacked1.convertTo(frameCVStacked1, CV_8UC1, 1.0/stackingCalibration);
113 jakw 235
 
236
    calibrationSet.frame0 = frameCVStacked0;
237
    calibrationSet.frame1 = frameCVStacked1;
238
 
27 jakw 239
    calibrationSet.rotationAngle = rotationStage->getAngle();
23 jakw 240
 
241
    emit newCalibrationSet(calibrationSet);
9 jakw 242
}
243
 
30 jakw 244
void SMCaptureWorker::acquireCalibrationSets(std::vector<float> angles){
9 jakw 245
 
167 jakw 246
    if(!setupSuccessful)
247
        return;
248
 
249
    for(unsigned int i=0; i<angles.size(); i++)
30 jakw 250
        acquireCalibrationSet(angles[i]);
251
}
252
 
27 jakw 253
void SMCaptureWorker::acquireFrameSequence(float angle){
254
 
167 jakw 255
    if(!setupSuccessful)
256
        return;
257
 
27 jakw 258
    if(angle != -1.0)
259
        rotateTo(angle);
260
 
261
    CameraFrame frame;
262
    SMFrameSequence frameSequence;
263
 
41 jakw 264
    for(unsigned int i=0; i<algorithm->getNPatterns(); i++){
27 jakw 265
 
266
        // display pattern
267
        projector->displayPattern(i);
268
 
269
        QTest::qSleep(delay);
270
 
120 jakw 271
        cv::Mat frameCVStacked0(camera0->getFrameHeight(), camera0->getFrameWidth(), CV_32SC1, cv::Scalar(0));
272
        cv::Mat frameCVStacked1(camera1->getFrameHeight(), camera1->getFrameWidth(), CV_32SC1, cv::Scalar(0));
139 jakw 273
        for(int i=0; i<stackingAcquisition; i++){
113 jakw 274
            // trigger cameras
275
            camera0->trigger();
276
            camera1->trigger();
27 jakw 277
 
113 jakw 278
            // retrieve frames
279
            frame = camera0->getFrame();
280
            cv::Mat frameCV;
121 jakw 281
            frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
113 jakw 282
            frameCV = frameCV.clone();
120 jakw 283
            cv::add(frameCV, frameCVStacked0, frameCVStacked0, cv::noArray(), CV_32SC1);
27 jakw 284
 
113 jakw 285
            emit newFrame(0, frameCV);
27 jakw 286
 
113 jakw 287
            frame = camera1->getFrame();
121 jakw 288
            frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
113 jakw 289
            frameCV = frameCV.clone();
120 jakw 290
            cv::add(frameCV, frameCVStacked1, frameCVStacked1, cv::noArray(), CV_32SC1);
27 jakw 291
 
113 jakw 292
            emit newFrame(1, frameCV);
27 jakw 293
 
113 jakw 294
        }
295
 
139 jakw 296
        frameCVStacked0.convertTo(frameCVStacked0, CV_8UC1, 1.0/stackingAcquisition);
297
        frameCVStacked1.convertTo(frameCVStacked1, CV_8UC1, 1.0/stackingAcquisition);
113 jakw 298
 
299
        frameSequence.frames0.push_back(frameCVStacked0);
300
        frameSequence.frames1.push_back(frameCVStacked1);
301
 
27 jakw 302
    }
303
 
223 flgw 304
    if(rotationStage && rotationStage->Handle)// TODO is this the right check
207 flgw 305
        frameSequence.rotationAngle = rotationStage->getAngle();
306
    else
307
        frameSequence.rotationAngle = 0;
308
 
27 jakw 309
    frameSequence.codec = codec;
310
 
311
    emit newFrameSequence(frameSequence);
312
 
92 jakw 313
    projector->displayWhite();
27 jakw 314
}
315
 
316
 
30 jakw 317
void SMCaptureWorker::acquireFrameSequences(std::vector<float> angles){
23 jakw 318
 
167 jakw 319
    if(!setupSuccessful)
320
        return;
321
 
322
    for(unsigned int i=0; i<angles.size(); i++)
30 jakw 323
        acquireFrameSequence(angles[i]);
324
}
325
 
23 jakw 326
void SMCaptureWorker::abort(){}
327
 
328
void SMCaptureWorker::stopWork(){
329
    working = false;
330
}
331