Subversion Repositories seema-scanner

Rev

Rev 121 | Rev 133 | 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 "AlgorithmPhaseShiftTwoFreq.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 float periodPrimary = 24; // primary period
12
static float periodPrimary = 32; // primary period
13
 
13
 
14
// Algorithm
14
// Algorithm
15
static cv::Mat computePhaseVector(unsigned int length, float phase, float pitch){
15
static cv::Mat computePhaseVector(unsigned int length, float phase, float pitch){
16
 
16
 
17
    cv::Mat phaseVector(length, 1, CV_8UC3);
17
    cv::Mat phaseVector(length, 1, CV_8UC3);
Line 27... Line 27...
27
    }
27
    }
28
 
28
 
29
    return phaseVector;
29
    return phaseVector;
30
}
30
}
31
 
31
 
32
AlgorithmPhaseShift::AlgorithmPhaseShift(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
32
AlgorithmPhaseShiftTwoFreq::AlgorithmPhaseShiftTwoFreq(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
33
 
33
 
34
    // Set N
34
    // Set N
35
    N = 2+nStepsPrimary+nStepsSecondary;
35
    N = 2+nStepsPrimary+nStepsSecondary;
36
 
36
 
37
    // Determine the secondary (wider) period
37
    // Determine the secondary (wider) period
Line 67... Line 67...
67
    }
67
    }
68
 
68
 
69
 
69
 
70
}
70
}
71
 
71
 
72
cv::Mat AlgorithmPhaseShift::getEncodingPattern(unsigned int depth){
72
cv::Mat AlgorithmPhaseShiftTwoFreq::getEncodingPattern(unsigned int depth){
73
    return patterns[depth];
73
    return patterns[depth];
74
}
74
}
75
 
75
 
76
 
76
 
77
// Absolute phase from 3 frames
77
// Absolute phase from 3 frames
78
cv::Mat getPhase(const cv::Mat I1, const cv::Mat I2, const cv::Mat I3){
78
static cv::Mat getPhase(const cv::Mat I1, const cv::Mat I2, const cv::Mat I3){
79
 
79
 
80
    cv::Mat_<float> I1_(I1);
80
    cv::Mat_<float> I1_(I1);
81
    cv::Mat_<float> I2_(I2);
81
    cv::Mat_<float> I2_(I2);
82
    cv::Mat_<float> I3_(I3);
82
    cv::Mat_<float> I3_(I3);
83
 
83
 
Line 88... Line 88...
88
    return phase;
88
    return phase;
89
 
89
 
90
}
90
}
91
 
91
 
92
// Phase unwrapping by means of a phase cue
92
// Phase unwrapping by means of a phase cue
93
cv::Mat unwrapWithCue(const cv::Mat up, const cv::Mat upCue, float nPhases){
93
static cv::Mat unwrapWithCue(const cv::Mat up, const cv::Mat upCue, float nPhases){
94
 
94
 
95
    const float pi = M_PI;
95
    const float pi = M_PI;
96
 
96
 
97
    // Determine number of jumps
97
    // Determine number of jumps
98
    cv::Mat P = (upCue*nPhases-up)/(2*pi);
98
    cv::Mat P = (upCue*nPhases-up)/(2.0*pi);
99
 
99
 
100
    // Round to integers
100
    // Round to integers
101
    P.convertTo(P, CV_8U);
101
    P.convertTo(P, CV_8U);
102
    P.convertTo(P, CV_32F);
102
    P.convertTo(P, CV_32F);
103
 
103
 
Line 109... Line 109...
109
 
109
 
110
    return upUnwrapped;
110
    return upUnwrapped;
111
}
111
}
112
 
112
 
113
// Absolute phase and magnitude from N frames
113
// Absolute phase and magnitude from N frames
114
std::vector<cv::Mat> getDFTComponents(const std::vector<cv::Mat> frames){
114
static std::vector<cv::Mat> getDFTComponents(const std::vector<cv::Mat> frames){
115
 
115
 
116
    unsigned int N = frames.size();
116
    unsigned int N = frames.size();
117
 
117
 
118
//    std::vector<cv::Mat> framesReverse = frames;
118
//    std::vector<cv::Mat> framesReverse = frames;
119
//    std::reverse(framesReverse.begin(), framesReverse.end());
119
//    std::reverse(framesReverse.begin(), framesReverse.end());
Line 134... Line 134...
134
 
134
 
135
    return fIcomp;
135
    return fIcomp;
136
 
136
 
137
}
137
}
138
 
138
 
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){
139
void AlgorithmPhaseShiftTwoFreq::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
 
140
 
141
    const float pi = M_PI;
141
    const float pi = M_PI;
142
 
142
 
143
    assert(frames0.size() == N);
143
    assert(frames0.size() == N);
144
    assert(frames1.size() == N);
144
    assert(frames1.size() == N);
Line 163... Line 163...
163
    //cv::Mat up0Secondary = getPhase(frames0Secondary[0], frames0Secondary[1], frames0Secondary[2]);
163
    //cv::Mat up0Secondary = getPhase(frames0Secondary[0], frames0Secondary[1], frames0Secondary[2]);
164
    std::vector<cv::Mat> F0Secondary = getDFTComponents(frames0Secondary);
164
    std::vector<cv::Mat> F0Secondary = getDFTComponents(frames0Secondary);
165
    cv::Mat up0Secondary;
165
    cv::Mat up0Secondary;
166
    cv::phase(F0Secondary[2], -F0Secondary[3], up0Secondary);
166
    cv::phase(F0Secondary[2], -F0Secondary[3], up0Secondary);
167
    cv::Mat up0Equivalent = up0Primary - up0Secondary;
167
    cv::Mat up0Equivalent = up0Primary - up0Secondary;
168
    up0Equivalent = cvtools::modulo(up0Equivalent, 2*pi);
168
    up0Equivalent = cvtools::modulo(up0Equivalent, 2.0*pi);
169
    cv::Mat up0 = unwrapWithCue(up0Primary, up0Equivalent, (float)screenCols/periodPrimary);
169
    cv::Mat up0 = unwrapWithCue(up0Primary, up0Equivalent, (float)screenCols/periodPrimary);
170
    up0 *= screenCols/(2*pi);
170
    up0 *= screenCols/(2.0*pi);
-
 
171
    cv::Mat amplitude0;
-
 
172
    cv::magnitude(F0Primary[2], -F0Primary[3], amplitude0);
171
 
173
 
172
    // Decode camera1
174
    // Decode camera1
173
    std::vector<cv::Mat> frames1Primary(frames1Gray.begin()+2, frames1Gray.begin()+2+nStepsPrimary);
175
    std::vector<cv::Mat> frames1Primary(frames1Gray.begin()+2, frames1Gray.begin()+2+nStepsPrimary);
174
    std::vector<cv::Mat> frames1Secondary(frames1Gray.begin()+2+nStepsPrimary, frames1Gray.end());
176
    std::vector<cv::Mat> frames1Secondary(frames1Gray.begin()+2+nStepsPrimary, frames1Gray.end());
175
    std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
177
    std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
Line 178... Line 180...
178
    //cv::Mat up1Secondary = getPhase(frames1Secondary[0], frames1Secondary[1], frames1Secondary[2]);
180
    //cv::Mat up1Secondary = getPhase(frames1Secondary[0], frames1Secondary[1], frames1Secondary[2]);
179
    std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
181
    std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
180
    cv::Mat up1Secondary;
182
    cv::Mat up1Secondary;
181
    cv::phase(F1Secondary[2], -F1Secondary[3], up1Secondary);
183
    cv::phase(F1Secondary[2], -F1Secondary[3], up1Secondary);
182
    cv::Mat up1Equivalent = up1Primary - up1Secondary;
184
    cv::Mat up1Equivalent = up1Primary - up1Secondary;
183
    up1Equivalent = cvtools::modulo(up1Equivalent, 2*pi);
185
    up1Equivalent = cvtools::modulo(up1Equivalent, 2.0*pi);
184
    cv::Mat up1 = unwrapWithCue(up1Primary, up1Equivalent, (float)screenCols/periodPrimary);
186
    cv::Mat up1 = unwrapWithCue(up1Primary, up1Equivalent, (float)screenCols/periodPrimary);
185
    up1 *= screenCols/(2*pi);
187
    up1 *= screenCols/(2.0*pi);
-
 
188
    cv::Mat amplitude1;
-
 
189
    cv::magnitude(F1Primary[2], -F1Primary[3], amplitude1);
186
 
190
 
-
 
191
cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
-
 
192
cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
-
 
193
cvtools::writeMat(up0Equivalent, "up0Equivalent.mat", "up0Equivalent");
-
 
194
cvtools::writeMat(up0, "up0.mat", "up0");
-
 
195
cvtools::writeMat(amplitude0, "amplitude0.mat", "amplitude0");
187
 
196
 
188
    // Rectifying homographies (rotation+projections)
197
    // Rectifying homographies (rotation+projections)
189
    cv::Size frameSize(frameCols, frameRows);
198
    cv::Size frameSize(frameCols, frameRows);
190
    cv::Mat R, T;
199
    cv::Mat R, T;
191
    // stereoRectify segfaults unless R is double precision
200
    // stereoRectify segfaults unless R is double precision
Line 202... Line 211...
202
    // Phase remaps
211
    // Phase remaps
203
    cv::Mat up0Rect, up1Rect;
212
    cv::Mat up0Rect, up1Rect;
204
    cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_LINEAR);
213
    cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_LINEAR);
205
    cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_LINEAR);
214
    cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_LINEAR);
206
 
215
 
-
 
216
    // amplitude remaps
-
 
217
    cv::Mat amplitude0Rect, amplitude1Rect;
-
 
218
    cv::remap(amplitude0, amplitude0Rect, map0X, map0Y, CV_INTER_LINEAR);
-
 
219
    cv::remap(amplitude1, amplitude1Rect, map1X, map1Y, CV_INTER_LINEAR);
-
 
220
 
207
//cvtools::writeMat(up0Rect, "up0Rect.mat", "up0Rect");
221
//cvtools::writeMat(up0Rect, "up0Rect.mat", "up0Rect");
208
//cvtools::writeMat(up1Rect, "up1Rect.mat", "up1Rect");
222
//cvtools::writeMat(up1Rect, "up1Rect.mat", "up1Rect");
209
 
223
 
210
    // color debayer and remap
224
    // color debayer and remap
211
    cv::Mat color0Rect, color1Rect;
225
    cv::Mat color0Rect, color1Rect;
Line 237... Line 251...
237
    cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
251
    cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
238
    occlusion0Rect = (occlusion0Rect > 25) & (occlusion0Rect < 250);
252
    occlusion0Rect = (occlusion0Rect > 25) & (occlusion0Rect < 250);
239
    cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
253
    cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
240
    occlusion1Rect = (occlusion1Rect > 25) & (occlusion1Rect < 250);
254
    occlusion1Rect = (occlusion1Rect > 25) & (occlusion1Rect < 250);
241
 
255
 
-
 
256
    // Threshold on energy at primary frequency
-
 
257
    occlusion0Rect = occlusion0Rect & (amplitude0Rect > 5.0*nStepsPrimary);
-
 
258
    occlusion1Rect = occlusion1Rect & (amplitude1Rect > 5.0*nStepsPrimary);
-
 
259
 
242
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
260
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
243
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
261
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
244
 
262
 
245
    // Erode occlusion masks
263
    // Erode occlusion masks
246
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
264
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
247
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
265
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
248
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
266
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
249
 
267
 
250
    // Threshold on gradient of phase
268
    // Threshold on gradient of phase
251
    cv::Mat edges0;
269
    cv::Mat edges0;
252
    cv::Sobel(up0Rect, edges0, -1, 1, 0, 5);
270
    cv::Sobel(up0Rect, edges0, -1, 1, 1, 5);
253
    occlusion0Rect = occlusion0Rect & (abs(edges0) < 150);
271
    occlusion0Rect = occlusion0Rect & (abs(edges0) < 150);
254
 
272
 
255
    cv::Mat edges1;
273
    cv::Mat edges1;
256
    cv::Sobel(up1Rect, edges1, -1, 1, 0, 5);
274
    cv::Sobel(up1Rect, edges1, -1, 1, 1, 5);
257
    occlusion1Rect = occlusion1Rect & (abs(edges1) < 150);
275
    occlusion1Rect = occlusion1Rect & (abs(edges1) < 150);
258
 
276
 
259
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
277
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
260
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
278
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
261
 
279