Subversion Repositories seema-scanner

Rev

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