Subversion Repositories seema-scanner

Rev

Rev 52 | Rev 71 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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