Subversion Repositories seema-scanner

Rev

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