Subversion Repositories seema-scanner

Rev

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