Subversion Repositories seema-scanner

Rev

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