Subversion Repositories seema-scanner

Rev

Rev 182 | Rev 199 | 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
 
98
    // Processing loop
113 jakw 99
//    QTime time;
100
//    time.start();
23 jakw 101
    while(working){
9 jakw 102
 
27 jakw 103
        projector->displayWhite();
104
 
159 jakw 105
        // prevent image acquisition timeout
134 jakw 106
        QTest::qSleep(10);
107
 
9 jakw 108
        CameraFrame frame;
109
 
23 jakw 110
        // trigger cameras
111
        camera0->trigger();
112
        camera1->trigger();
9 jakw 113
 
113 jakw 114
        // retrieve raw frames
23 jakw 115
        frame = camera0->getFrame();
116
        cv::Mat frameCV;
121 jakw 117
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
23 jakw 118
        frameCV = frameCV.clone();
121 jakw 119
//        cvtools::rshift(frameCV, 8);
120
//        frameCV.convertTo(frameCV, CV_8UC1);
23 jakw 121
        emit newFrame(0, frameCV);
9 jakw 122
 
23 jakw 123
        frame = camera1->getFrame();
121 jakw 124
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
23 jakw 125
        frameCV = frameCV.clone();
121 jakw 126
//        cvtools::rshift(frameCV, 8);
127
//        frameCV.convertTo(frameCV, CV_8UC1);
23 jakw 128
        emit newFrame(1, frameCV);
9 jakw 129
 
23 jakw 130
        //std::cout << "SMCaptureWorker idle " << time.restart() << "ms" << std::endl;
9 jakw 131
 
23 jakw 132
        // Process events e.g. perform a task
9 jakw 133
        QCoreApplication::processEvents();
134
    }
135
 
136
    emit finished();
23 jakw 137
}
9 jakw 138
 
23 jakw 139
void SMCaptureWorker::rotateTo(float angle){
140
 
167 jakw 141
    if(!setupSuccessful)
142
        return;
143
 
23 jakw 144
    rotationStage->moveAbsolute(angle);
30 jakw 145
 
92 jakw 146
    while(rotationStage->isMoving()){
147
 
134 jakw 148
        // prevent grab timeout in flycapture
149
        QTest::qSleep(10);
150
 
92 jakw 151
        // trigger cameras
152
        camera0->trigger();
153
        camera1->trigger();
154
 
155
        // retrieve frames
156
        CameraFrame frame;
157
        frame = camera0->getFrame();
158
        cv::Mat frameCV;
121 jakw 159
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
92 jakw 160
        frameCV = frameCV.clone();
161
        emit newFrame(0, frameCV);
162
        frame = camera1->getFrame();
121 jakw 163
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
92 jakw 164
        frameCV = frameCV.clone();
165
        emit newFrame(1, frameCV);
166
    }
167
 
30 jakw 168
    emit rotatedTo(angle);
9 jakw 169
}
170
 
23 jakw 171
void SMCaptureWorker::acquireCalibrationSet(float angle){
172
 
167 jakw 173
    if(!setupSuccessful)
174
        return;
175
 
27 jakw 176
    if(angle != -1.0)
177
        rotateTo(angle);
178
 
134 jakw 179
    // just for safe measures
180
    QTest::qSleep(500);
181
 
23 jakw 182
    CameraFrame frame;
27 jakw 183
    SMCalibrationSet calibrationSet;
120 jakw 184
    cv::Mat frameCVStacked0(camera0->getFrameHeight(), camera0->getFrameWidth(), CV_32SC1, cv::Scalar(0));
185
    cv::Mat frameCVStacked1(camera1->getFrameHeight(), camera1->getFrameWidth(), CV_32SC1, cv::Scalar(0));
23 jakw 186
 
139 jakw 187
    for(int i=0; i<stackingCalibration; i++){
113 jakw 188
        // trigger cameras
189
        camera0->trigger();
190
        camera1->trigger();
23 jakw 191
 
113 jakw 192
        // retrieve frames
193
        frame = camera0->getFrame();
194
        cv::Mat frameCV;
121 jakw 195
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
113 jakw 196
        frameCV = frameCV.clone();
120 jakw 197
        cv::add(frameCV, frameCVStacked0, frameCVStacked0, cv::noArray(), CV_32SC1);
121 jakw 198
//cvtools::writeMat(frameCV, "frameCV.mat", "frameCV");
199
//cvtools::writeMat(frameCVStacked0, "frameCVStacked0.mat", "frameCVStacked0");
113 jakw 200
        emit newFrame(0, frameCV);
23 jakw 201
 
113 jakw 202
        frame = camera1->getFrame();
121 jakw 203
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
113 jakw 204
        frameCV = frameCV.clone();
120 jakw 205
        cv::add(frameCV, frameCVStacked1, frameCVStacked1, cv::noArray(), CV_32SC1);
23 jakw 206
 
113 jakw 207
        emit newFrame(1, frameCV);
23 jakw 208
 
113 jakw 209
    }
210
 
139 jakw 211
    frameCVStacked0.convertTo(frameCVStacked0, CV_8UC1, 1.0/stackingCalibration);
134 jakw 212
//cvtools::writeMat(frameCVStacked0, "frameCVStacked0a.mat", "frameCVStacked0a");
139 jakw 213
    frameCVStacked1.convertTo(frameCVStacked1, CV_8UC1, 1.0/stackingCalibration);
113 jakw 214
 
215
    calibrationSet.frame0 = frameCVStacked0;
216
    calibrationSet.frame1 = frameCVStacked1;
217
 
27 jakw 218
    calibrationSet.rotationAngle = rotationStage->getAngle();
23 jakw 219
 
220
    emit newCalibrationSet(calibrationSet);
9 jakw 221
}
222
 
30 jakw 223
void SMCaptureWorker::acquireCalibrationSets(std::vector<float> angles){
9 jakw 224
 
167 jakw 225
    if(!setupSuccessful)
226
        return;
227
 
228
    for(unsigned int i=0; i<angles.size(); i++)
30 jakw 229
        acquireCalibrationSet(angles[i]);
230
}
231
 
27 jakw 232
void SMCaptureWorker::acquireFrameSequence(float angle){
233
 
167 jakw 234
    if(!setupSuccessful)
235
        return;
236
 
27 jakw 237
    if(angle != -1.0)
238
        rotateTo(angle);
239
 
240
    CameraFrame frame;
241
    SMFrameSequence frameSequence;
242
 
41 jakw 243
    for(unsigned int i=0; i<algorithm->getNPatterns(); i++){
27 jakw 244
 
245
        // display pattern
246
        projector->displayPattern(i);
247
 
248
        QTest::qSleep(delay);
249
 
120 jakw 250
        cv::Mat frameCVStacked0(camera0->getFrameHeight(), camera0->getFrameWidth(), CV_32SC1, cv::Scalar(0));
251
        cv::Mat frameCVStacked1(camera1->getFrameHeight(), camera1->getFrameWidth(), CV_32SC1, cv::Scalar(0));
139 jakw 252
        for(int i=0; i<stackingAcquisition; i++){
113 jakw 253
            // trigger cameras
254
            camera0->trigger();
255
            camera1->trigger();
27 jakw 256
 
113 jakw 257
            // retrieve frames
258
            frame = camera0->getFrame();
259
            cv::Mat frameCV;
121 jakw 260
            frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
113 jakw 261
            frameCV = frameCV.clone();
120 jakw 262
            cv::add(frameCV, frameCVStacked0, frameCVStacked0, cv::noArray(), CV_32SC1);
27 jakw 263
 
113 jakw 264
            emit newFrame(0, frameCV);
27 jakw 265
 
113 jakw 266
            frame = camera1->getFrame();
121 jakw 267
            frameCV  = cv::Mat(frame.height, frame.width, CV_8UC1, frame.memory);
113 jakw 268
            frameCV = frameCV.clone();
120 jakw 269
            cv::add(frameCV, frameCVStacked1, frameCVStacked1, cv::noArray(), CV_32SC1);
27 jakw 270
 
113 jakw 271
            emit newFrame(1, frameCV);
27 jakw 272
 
113 jakw 273
        }
274
 
139 jakw 275
        frameCVStacked0.convertTo(frameCVStacked0, CV_8UC1, 1.0/stackingAcquisition);
276
        frameCVStacked1.convertTo(frameCVStacked1, CV_8UC1, 1.0/stackingAcquisition);
113 jakw 277
 
278
        frameSequence.frames0.push_back(frameCVStacked0);
279
        frameSequence.frames1.push_back(frameCVStacked1);
280
 
27 jakw 281
    }
282
 
283
    frameSequence.rotationAngle = rotationStage->getAngle();
284
    frameSequence.codec = codec;
285
 
286
    emit newFrameSequence(frameSequence);
287
 
92 jakw 288
    projector->displayWhite();
27 jakw 289
}
290
 
291
 
30 jakw 292
void SMCaptureWorker::acquireFrameSequences(std::vector<float> angles){
23 jakw 293
 
167 jakw 294
    if(!setupSuccessful)
295
        return;
296
 
297
    for(unsigned int i=0; i<angles.size(); i++)
30 jakw 298
        acquireFrameSequence(angles[i]);
299
}
300
 
23 jakw 301
void SMCaptureWorker::abort(){}
302
 
303
void SMCaptureWorker::stopWork(){
304
    working = false;
305
}
306
 
9 jakw 307
SMCaptureWorker::~SMCaptureWorker(){
23 jakw 308
    delete projector;
137 jakw 309
    delete camera0;
310
    delete camera1;
23 jakw 311
    delete rotationStage;
9 jakw 312
}