Subversion Repositories seema-scanner

Rev

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