Subversion Repositories seema-scanner

Rev

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