Subversion Repositories seema-scanner

Rev

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