Subversion Repositories seema-scanner

Rev

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