Subversion Repositories seema-scanner

Rev

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