Subversion Repositories seema-scanner

Rev

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