Subversion Repositories seema-scanner

Rev

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