Subversion Repositories seema-scanner

Rev

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