Subversion Repositories seema-scanner

Rev

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