Subversion Repositories seema-scanner

Rev

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