Subversion Repositories seema-scanner

Rev

Rev 100 | Rev 107 | 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);
97 jakw 54
    else if(codec == "GrayCodeHQ")
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();
9 jakw 69
}
70
 
71
 
72
void SMCaptureWorker::doWork(){
73
 
23 jakw 74
    working = true;
9 jakw 75
 
76
    // Processing loop
23 jakw 77
    QTime time;
78
    time.start();
79
    while(working){
9 jakw 80
 
27 jakw 81
        projector->displayWhite();
82
 
9 jakw 83
        std::vector<cv::Mat> frameSet;
84
 
85
        CameraFrame frame;
86
 
23 jakw 87
        // trigger cameras
88
        camera0->trigger();
89
        camera1->trigger();
9 jakw 90
 
23 jakw 91
        // retrieve frames
92
        frame = camera0->getFrame();
93
        cv::Mat frameCV;
94
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC3, frame.memory);
95
        frameCV = frameCV.clone();
96
        frameSet.push_back(frameCV);
9 jakw 97
 
23 jakw 98
        emit newFrame(0, frameCV);
9 jakw 99
 
23 jakw 100
        frame = camera1->getFrame();
101
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC3, frame.memory);
102
        frameCV = frameCV.clone();
103
        frameSet.push_back(frameCV);
9 jakw 104
 
23 jakw 105
        emit newFrame(1, frameCV);
9 jakw 106
 
23 jakw 107
        //std::cout << "SMCaptureWorker idle " << time.restart() << "ms" << std::endl;
9 jakw 108
 
23 jakw 109
        // Process events e.g. perform a task
9 jakw 110
        QCoreApplication::processEvents();
111
    }
112
 
113
    emit finished();
23 jakw 114
}
9 jakw 115
 
23 jakw 116
void SMCaptureWorker::rotateTo(float angle){
117
 
118
    rotationStage->moveAbsolute(angle);
30 jakw 119
 
92 jakw 120
    while(rotationStage->isMoving()){
121
 
122
        // trigger cameras
123
        camera0->trigger();
124
        camera1->trigger();
125
 
126
        // retrieve frames
127
        CameraFrame frame;
128
        frame = camera0->getFrame();
129
        cv::Mat frameCV;
130
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC3, frame.memory);
131
        frameCV = frameCV.clone();
132
        emit newFrame(0, frameCV);
133
        frame = camera1->getFrame();
134
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC3, frame.memory);
135
        frameCV = frameCV.clone();
136
        emit newFrame(1, frameCV);
137
    }
138
 
30 jakw 139
    emit rotatedTo(angle);
9 jakw 140
}
141
 
23 jakw 142
void SMCaptureWorker::acquireCalibrationSet(float angle){
143
 
27 jakw 144
    if(angle != -1.0)
145
        rotateTo(angle);
146
 
23 jakw 147
    CameraFrame frame;
27 jakw 148
    SMCalibrationSet calibrationSet;
23 jakw 149
 
150
    // trigger cameras
151
    camera0->trigger();
152
    camera1->trigger();
153
 
154
    // retrieve frames
155
    frame = camera0->getFrame();
156
    cv::Mat frameCV;
157
    frameCV  = cv::Mat(frame.height, frame.width, CV_8UC3, frame.memory);
158
    frameCV = frameCV.clone();
159
 
27 jakw 160
    emit newFrame(0, frameCV);
23 jakw 161
    calibrationSet.frame0 = frameCV;
162
 
163
    frame = camera1->getFrame();
164
    frameCV  = cv::Mat(frame.height, frame.width, CV_8UC3, frame.memory);
165
    frameCV = frameCV.clone();
166
 
27 jakw 167
    emit newFrame(1, frameCV);
23 jakw 168
    calibrationSet.frame1 = frameCV;
169
 
27 jakw 170
    calibrationSet.rotationAngle = rotationStage->getAngle();
23 jakw 171
 
172
    emit newCalibrationSet(calibrationSet);
9 jakw 173
}
174
 
30 jakw 175
void SMCaptureWorker::acquireCalibrationSets(std::vector<float> angles){
9 jakw 176
 
30 jakw 177
    for(int i=0; i<angles.size(); i++)
178
        acquireCalibrationSet(angles[i]);
179
}
180
 
27 jakw 181
void SMCaptureWorker::acquireFrameSequence(float angle){
182
 
183
    if(angle != -1.0)
184
        rotateTo(angle);
185
 
186
    CameraFrame frame;
187
 
188
    SMFrameSequence frameSequence;
189
 
41 jakw 190
    for(unsigned int i=0; i<algorithm->getNPatterns(); i++){
27 jakw 191
 
192
        // display pattern
193
        projector->displayPattern(i);
194
 
195
        QTest::qSleep(delay);
196
 
197
        // trigger cameras
198
        camera0->trigger();
199
        camera1->trigger();
200
 
201
        // retrieve frames
202
        frame = camera0->getFrame();
103 jakw 203
        cv::Mat frameCV;
27 jakw 204
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC3, frame.memory);
205
        frameCV = frameCV.clone();
206
 
207
        emit newFrame(0, frameCV);
103 jakw 208
        frameSequence.frames0.push_back(frameCV);
27 jakw 209
 
210
        frame = camera1->getFrame();
211
        frameCV  = cv::Mat(frame.height, frame.width, CV_8UC3, frame.memory);
212
        frameCV = frameCV.clone();
213
 
214
        emit newFrame(1, frameCV);
103 jakw 215
        frameSequence.frames1.push_back(frameCV);
27 jakw 216
 
217
    }
218
 
219
    frameSequence.rotationAngle = rotationStage->getAngle();
220
    frameSequence.codec = codec;
221
 
222
    emit newFrameSequence(frameSequence);
223
 
92 jakw 224
    projector->displayWhite();
27 jakw 225
}
226
 
227
 
30 jakw 228
void SMCaptureWorker::acquireFrameSequences(std::vector<float> angles){
23 jakw 229
 
30 jakw 230
    for(int i=0; i<angles.size(); i++)
231
        acquireFrameSequence(angles[i]);
232
}
233
 
23 jakw 234
void SMCaptureWorker::abort(){}
235
 
236
void SMCaptureWorker::stopWork(){
237
    working = false;
238
}
239
 
9 jakw 240
SMCaptureWorker::~SMCaptureWorker(){
23 jakw 241
    delete projector;
51 jakw 242
//    delete camera0;
243
//    delete camera1;
23 jakw 244
    delete rotationStage;
9 jakw 245
}