Subversion Repositories seema-scanner

Rev

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