Subversion Repositories seema-scanner

Rev

Rev 121 | Rev 129 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 121 Rev 128
Line 1... Line 1...
1
#include "AlgorithmPhaseShift.h"
1
#include "AlgorithmPhaseShiftThreeFreq.h"
2
#include <math.h>
2
#include <math.h>
3
 
3
 
4
#include "cvtools.h"
4
#include "cvtools.h"
5
 
5
 
6
#ifndef M_PI
6
#ifndef M_PI
7
    #define M_PI 3.14159265358979323846
7
    #define M_PI 3.14159265358979323846
8
#endif
8
#endif
9
 
9
 
10
static unsigned int nStepsPrimary = 24; // number of shifts/steps in primary
10
static unsigned int nStepsPrimary = 16; // number of shifts/steps in primary
11
static unsigned int nStepsSecondary = 12; // number of shifts/steps in secondary
11
static unsigned int nStepsSecondary = 6; // number of shifts/steps in secondary
-
 
12
static unsigned int nStepsTertiary = 6; // number of shifts/steps in secondary
12
static float periodPrimary = 24; // primary period
13
static float periodPrimary = 32; // primary period
13
 
14
 
14
// Algorithm
15
// Algorithm
15
static cv::Mat computePhaseVector(unsigned int length, float phase, float pitch){
16
static cv::Mat computePhaseVector(unsigned int length, float phase, float pitch){
16
 
17
 
17
    cv::Mat phaseVector(length, 1, CV_8UC3);
18
    cv::Mat phaseVector(length, 1, CV_8UC3);
Line 27... Line 28...
27
    }
28
    }
28
 
29
 
29
    return phaseVector;
30
    return phaseVector;
30
}
31
}
31
 
32
 
32
AlgorithmPhaseShift::AlgorithmPhaseShift(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
33
AlgorithmPhaseShiftThreeFreq::AlgorithmPhaseShiftThreeFreq(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
33
 
34
 
34
    // Set N
35
    // Set N
35
    N = 2+nStepsPrimary+nStepsSecondary;
36
    N = 2+nStepsPrimary+nStepsSecondary+nStepsTertiary;
36
 
37
 
37
    // Determine the secondary (wider) period
38
    // Determine the secondary (wider) period
38
    float pSecondary = (screenCols*periodPrimary)/(screenCols-periodPrimary);
39
    float pSecondary = (screenCols*periodPrimary)/(screenCols-periodPrimary);
39
 
40
 
40
    // all on pattern
41
    // all on pattern
Line 63... Line 64...
63
        float pitch = pSecondary;
64
        float pitch = pSecondary;
64
        cv::Mat patternI(1,1,CV_8U);
65
        cv::Mat patternI(1,1,CV_8U);
65
        patternI = computePhaseVector(screenCols, phase, pitch);
66
        patternI = computePhaseVector(screenCols, phase, pitch);
66
        patterns.push_back(patternI.t());
67
        patterns.push_back(patternI.t());
67
    }
68
    }
-
 
69
    // Tertiary encoding patterns
-
 
70
    for(unsigned int i=0; i<nStepsTertiary; i++){
-
 
71
        float phase = 2.0*pi/nStepsTertiary * i;
-
 
72
        float pitch = pTertiary;
-
 
73
        cv::Mat patternI(1,1,CV_8U);
-
 
74
        patternI = computePhaseVector(screenCols, phase, pitch);
-
 
75
        patterns.push_back(patternI.t());
68
 
76
    }
69
 
77
 
70
}
78
}
71
 
79
 
72
cv::Mat AlgorithmPhaseShift::getEncodingPattern(unsigned int depth){
80
cv::Mat AlgorithmPhaseShiftThreeFreq::getEncodingPattern(unsigned int depth){
73
    return patterns[depth];
81
    return patterns[depth];
74
}
82
}
75
 
83
 
76
 
84
 
77
// Absolute phase from 3 frames
85
// Absolute phase from 3 frames
78
cv::Mat getPhase(const cv::Mat I1, const cv::Mat I2, const cv::Mat I3){
86
static cv::Mat getPhase(const cv::Mat I1, const cv::Mat I2, const cv::Mat I3){
79
 
87
 
80
    cv::Mat_<float> I1_(I1);
88
    cv::Mat_<float> I1_(I1);
81
    cv::Mat_<float> I2_(I2);
89
    cv::Mat_<float> I2_(I2);
82
    cv::Mat_<float> I3_(I3);
90
    cv::Mat_<float> I3_(I3);
83
 
91
 
Line 88... Line 96...
88
    return phase;
96
    return phase;
89
 
97
 
90
}
98
}
91
 
99
 
92
// Phase unwrapping by means of a phase cue
100
// Phase unwrapping by means of a phase cue
93
cv::Mat unwrapWithCue(const cv::Mat up, const cv::Mat upCue, float nPhases){
101
static cv::Mat unwrapWithCue(const cv::Mat up, const cv::Mat upCue, float nPhases){
94
 
102
 
95
    const float pi = M_PI;
103
    const float pi = M_PI;
96
 
104
 
97
    // Determine number of jumps
105
    // Determine number of jumps
98
    cv::Mat P = (upCue*nPhases-up)/(2*pi);
106
    cv::Mat P = (upCue*nPhases-up)/(2.0*pi);
99
 
107
 
100
    // Round to integers
108
    // Round to integers
101
    P.convertTo(P, CV_8U);
109
    P.convertTo(P, CV_8U);
102
    P.convertTo(P, CV_32F);
110
    P.convertTo(P, CV_32F);
103
 
111
 
Line 109... Line 117...
109
 
117
 
110
    return upUnwrapped;
118
    return upUnwrapped;
111
}
119
}
112
 
120
 
113
// Absolute phase and magnitude from N frames
121
// Absolute phase and magnitude from N frames
114
std::vector<cv::Mat> getDFTComponents(const std::vector<cv::Mat> frames){
122
static std::vector<cv::Mat> getDFTComponents(const std::vector<cv::Mat> frames){
115
 
123
 
116
    unsigned int N = frames.size();
124
    unsigned int N = frames.size();
117
 
125
 
118
//    std::vector<cv::Mat> framesReverse = frames;
126
//    std::vector<cv::Mat> framesReverse = frames;
119
//    std::reverse(framesReverse.begin(), framesReverse.end());
127
//    std::reverse(framesReverse.begin(), framesReverse.end());
Line 134... Line 142...
134
 
142
 
135
    return fIcomp;
143
    return fIcomp;
136
 
144
 
137
}
145
}
138
 
146
 
139
void AlgorithmPhaseShift::get3DPoints(SMCalibrationParameters calibration, const std::vector<cv::Mat>& frames0, const std::vector<cv::Mat>& frames1, std::vector<cv::Point3f>& Q, std::vector<cv::Vec3b>& color){
147
void AlgorithmPhaseShiftThreeFreq::get3DPoints(SMCalibrationParameters calibration, const std::vector<cv::Mat>& frames0, const std::vector<cv::Mat>& frames1, std::vector<cv::Point3f>& Q, std::vector<cv::Vec3b>& color){
140
 
148
 
141
    const float pi = M_PI;
149
    const float pi = M_PI;
142
 
150
 
143
    assert(frames0.size() == N);
151
    assert(frames0.size() == N);
144
    assert(frames1.size() == N);
152
    assert(frames1.size() == N);
Line 154... Line 162...
154
        cv::cvtColor(frames1[i], frames1Gray[i], CV_BayerBG2GRAY);
162
        cv::cvtColor(frames1[i], frames1Gray[i], CV_BayerBG2GRAY);
155
    }
163
    }
156
 
164
 
157
    // Decode camera0
165
    // Decode camera0
158
    std::vector<cv::Mat> frames0Primary(frames0Gray.begin()+2, frames0Gray.begin()+2+nStepsPrimary);
166
    std::vector<cv::Mat> frames0Primary(frames0Gray.begin()+2, frames0Gray.begin()+2+nStepsPrimary);
159
    std::vector<cv::Mat> frames0Secondary(frames0Gray.begin()+2+nStepsPrimary, frames0Gray.end());
167
    std::vector<cv::Mat> frames0Secondary(frames0Gray.begin()+2+nStepsPrimary, frames0Gray.end()-nStepsTertiary);
-
 
168
    std::vector<cv::Mat> frames0Tertiary(frames0Gray.end()-nStepsTertiary, frames0Gray.end());
160
    std::vector<cv::Mat> F0Primary = getDFTComponents(frames0Primary);
169
    std::vector<cv::Mat> F0Primary = getDFTComponents(frames0Primary);
-
 
170
 
161
    cv::Mat up0Primary;
171
    cv::Mat up0Primary;
162
    cv::phase(F0Primary[2], -F0Primary[3], up0Primary);
172
    cv::phase(F0Primary[2], -F0Primary[3], up0Primary);
163
    //cv::Mat up0Secondary = getPhase(frames0Secondary[0], frames0Secondary[1], frames0Secondary[2]);
-
 
164
    std::vector<cv::Mat> F0Secondary = getDFTComponents(frames0Secondary);
173
    std::vector<cv::Mat> F0Secondary = getDFTComponents(frames0Secondary);
165
    cv::Mat up0Secondary;
174
    cv::Mat up0Secondary;
166
    cv::phase(F0Secondary[2], -F0Secondary[3], up0Secondary);
175
    cv::phase(F0Secondary[2], -F0Secondary[3], up0Secondary);
-
 
176
    std::vector<cv::Mat> F0Tertiary = getDFTComponents(frames0Tertiary);
-
 
177
    cv::Mat up0Tertiary;
-
 
178
    cv::phase(F0Tertiary[2], -F0Tertiary[3], up0Tertiary);
-
 
179
 
167
    cv::Mat up0Equivalent = up0Primary - up0Secondary;
180
    cv::Mat up0EquivalentPS = up0Primary - up0Secondary;
-
 
181
    up0EquivalentPS = cvtools::modulo(up0EquivalentPS, 2.0*pi);
-
 
182
 
-
 
183
    cv::Mat up0EquivalentST = up0Secondary - up0Tertiary;
-
 
184
    up0EquivalentST = cvtools::modulo(up0EquivalentST, 2.0*pi);
-
 
185
 
-
 
186
    cv::Mat up0Equivalent = up0EquivalentPS - up0EquivalentST;
168
    up0Equivalent = cvtools::modulo(up0Equivalent, 2*pi);
187
    up0Equivalent = cvtools::modulo(up0Equivalent, 2.0*pi);
-
 
188
 
169
    cv::Mat up0 = unwrapWithCue(up0Primary, up0Equivalent, (float)screenCols/periodPrimary);
189
    cv::Mat up0 = unwrapWithCue(up0Primary, up0Equivalent, (float)screenCols/periodPrimary);
170
    up0 *= screenCols/(2*pi);
190
    up0 *= screenCols/(2.0*pi);
-
 
191
    cv::Mat amplitude0;
-
 
192
    cv::magnitude(F0Primary[2], -F0Primary[3], amplitude0);
171
 
193
 
172
    // Decode camera1
194
    // Decode camera1
173
    std::vector<cv::Mat> frames1Primary(frames1Gray.begin()+2, frames1Gray.begin()+2+nStepsPrimary);
195
    std::vector<cv::Mat> frames1Primary(frames1Gray.begin()+2, frames1Gray.begin()+2+nStepsPrimary);
174
    std::vector<cv::Mat> frames1Secondary(frames1Gray.begin()+2+nStepsPrimary, frames1Gray.end());
196
    std::vector<cv::Mat> frames1Secondary(frames1Gray.begin()+2+nStepsPrimary, frames1Gray.end());
175
    std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
197
    std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
176
    cv::Mat up1Primary;
198
    cv::Mat up1Primary;
177
    cv::phase(F1Primary[2], -F1Primary[3], up1Primary);
199
    cv::phase(F1Primary[2], -F1Primary[3], up1Primary);
178
    //cv::Mat up1Secondary = getPhase(frames1Secondary[0], frames1Secondary[1], frames1Secondary[2]);
-
 
179
    std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
200
    std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
180
    cv::Mat up1Secondary;
201
    cv::Mat up1Secondary;
181
    cv::phase(F1Secondary[2], -F1Secondary[3], up1Secondary);
202
    cv::phase(F1Secondary[2], -F1Secondary[3], up1Secondary);
-
 
203
    std::vector<cv::Mat> F1Tertiary = getDFTComponents(frames1Tertiary);
-
 
204
    cv::Mat up1Tertiary;
-
 
205
    cv::phase(F1Tertiary[2], -F1Tertiary[3], up1Tertiary);
-
 
206
 
182
    cv::Mat up1Equivalent = up1Primary - up1Secondary;
207
    cv::Mat up1EquivalentPS = up1Primary - up1Secondary;
183
    up1Equivalent = cvtools::modulo(up1Equivalent, 2*pi);
208
    up1EquivalentPS = cvtools::modulo(up1EquivalentPS, 2.0*pi);
-
 
209
 
184
    cv::Mat up1 = unwrapWithCue(up1Primary, up1Equivalent, (float)screenCols/periodPrimary);
210
    cv::Mat up1EquivalentST = up1Secondary - up1Tertiary;
185
    up1 *= screenCols/(2*pi);
211
    up1EquivalentST = cvtools::modulo(up1EquivalentST, 2.0*pi);
186
 
212
 
-
 
213
    cv::Mat up1Equivalent = up1EquivalentPS - up1EquivalentST;
-
 
214
    up1Equivalent = cvtools::modulo(up1Equivalent, 2.0*pi);
-
 
215
 
-
 
216
    cv::Mat up1 = unwrapWithCue(up1Primary, up1Equivalent, (float)screenCols/periodPrimary);
-
 
217
    up1 *= screenCols/(2.0*pi);
-
 
218
    cv::Mat amplitude1;
-
 
219
    cv::magnitude(F1Primary[2], -F1Primary[3], amplitude1);
-
 
220
 
-
 
221
cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
-
 
222
cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
-
 
223
cvtools::writeMat(up0Equivalent, "up0Equivalent.mat", "up0Equivalent");
-
 
224
cvtools::writeMat(up0, "up0.mat", "up0");
-
 
225
cvtools::writeMat(amplitude0, "amplitude0.mat", "amplitude0");
187
 
226
 
188
    // Rectifying homographies (rotation+projections)
227
    // Rectifying homographies (rotation+projections)
189
    cv::Size frameSize(frameCols, frameRows);
228
    cv::Size frameSize(frameCols, frameRows);
190
    cv::Mat R, T;
229
    cv::Mat R, T;
191
    // stereoRectify segfaults unless R is double precision
230
    // stereoRectify segfaults unless R is double precision
Line 202... Line 241...
202
    // Phase remaps
241
    // Phase remaps
203
    cv::Mat up0Rect, up1Rect;
242
    cv::Mat up0Rect, up1Rect;
204
    cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_LINEAR);
243
    cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_LINEAR);
205
    cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_LINEAR);
244
    cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_LINEAR);
206
 
245
 
-
 
246
    // amplitude remaps
-
 
247
    cv::Mat amplitude0Rect, amplitude1Rect;
-
 
248
    cv::remap(amplitude0, amplitude0Rect, map0X, map0Y, CV_INTER_LINEAR);
-
 
249
    cv::remap(amplitude1, amplitude1Rect, map1X, map1Y, CV_INTER_LINEAR);
-
 
250
 
207
//cvtools::writeMat(up0Rect, "up0Rect.mat", "up0Rect");
251
//cvtools::writeMat(up0Rect, "up0Rect.mat", "up0Rect");
208
//cvtools::writeMat(up1Rect, "up1Rect.mat", "up1Rect");
252
//cvtools::writeMat(up1Rect, "up1Rect.mat", "up1Rect");
209
 
253
 
210
    // color debayer and remap
254
    // color debayer and remap
211
    cv::Mat color0Rect, color1Rect;
255
    cv::Mat color0Rect, color1Rect;
Line 237... Line 281...
237
    cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
281
    cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
238
    occlusion0Rect = (occlusion0Rect > 25) & (occlusion0Rect < 250);
282
    occlusion0Rect = (occlusion0Rect > 25) & (occlusion0Rect < 250);
239
    cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
283
    cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
240
    occlusion1Rect = (occlusion1Rect > 25) & (occlusion1Rect < 250);
284
    occlusion1Rect = (occlusion1Rect > 25) & (occlusion1Rect < 250);
241
 
285
 
-
 
286
    // Threshold on energy at primary frequency
-
 
287
    occlusion0Rect = occlusion0Rect & (amplitude0Rect > 5.0*nStepsPrimary);
-
 
288
    occlusion1Rect = occlusion1Rect & (amplitude1Rect > 5.0*nStepsPrimary);
-
 
289
 
242
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
290
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
243
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
291
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
244
 
292
 
245
    // Erode occlusion masks
293
    // Erode occlusion masks
246
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
294
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
247
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
295
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
248
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
296
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
249
 
297
 
250
    // Threshold on gradient of phase
298
    // Threshold on gradient of phase
251
    cv::Mat edges0;
299
    cv::Mat edges0;
252
    cv::Sobel(up0Rect, edges0, -1, 1, 0, 5);
300
    cv::Sobel(up0Rect, edges0, -1, 1, 1, 5);
253
    occlusion0Rect = occlusion0Rect & (abs(edges0) < 150);
301
    occlusion0Rect = occlusion0Rect & (abs(edges0) < 150);
254
 
302
 
255
    cv::Mat edges1;
303
    cv::Mat edges1;
256
    cv::Sobel(up1Rect, edges1, -1, 1, 0, 5);
304
    cv::Sobel(up1Rect, edges1, -1, 1, 1, 5);
257
    occlusion1Rect = occlusion1Rect & (abs(edges1) < 150);
305
    occlusion1Rect = occlusion1Rect & (abs(edges1) < 150);
258
 
306
 
259
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
307
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
260
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
308
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
261
 
309
 
Line 278... Line 326...
278
                    continue;
326
                    continue;
279
 
327
 
280
                float up1Left = up1Rect.at<float>(row,col1);
328
                float up1Left = up1Rect.at<float>(row,col1);
281
                float up1Right = up1Rect.at<float>(row,col1+1);
329
                float up1Right = up1Rect.at<float>(row,col1+1);
282
 
330
 
283
                if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 1) && (up1Right-up0i < 1)){
331
                if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 0.5) && (up1Right-up0i < 0.5)){
284
 
332
 
285
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
333
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
286
 
334
 
287
                    q0Rect.push_back(cv::Point2f(col, row));
335
                    q0Rect.push_back(cv::Point2f(col, row));
288
                    q1Rect.push_back(cv::Point2f(col1i, row));
336
                    q1Rect.push_back(cv::Point2f(col1i, row));