Subversion Repositories seema-scanner

Rev

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

Rev 128 Rev 129
1
#include "AlgorithmPhaseShiftThreeFreq.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 = 16; // number of shifts/steps in primary
10
static unsigned int nStepsPrimary = 16; // number of shifts/steps in primary
11
static unsigned int nStepsSecondary = 6; // number of shifts/steps in secondary
11
static unsigned int nStepsSecondary = 16; // number of shifts/steps in secondary
12
static unsigned int nStepsTertiary = 6; // number of shifts/steps in secondary
12
static unsigned int nStepsTertiary = 16; // number of shifts/steps in tertiary
13
static float periodPrimary = 32; // primary period
13
static float periodPrimary = 24; // primary period
-
 
14
static float periodSecondary = 30; // primary period
14
 
15
 
15
// Algorithm
16
// Algorithm
16
static cv::Mat computePhaseVector(unsigned int length, float phase, float pitch){
17
static cv::Mat computePhaseVector(unsigned int length, float phase, float pitch){
17
 
18
 
18
    cv::Mat phaseVector(length, 1, CV_8UC3);
19
    cv::Mat phaseVector(length, 1, CV_8UC3);
19
    //phaseVector.setTo(0);
20
    //phaseVector.setTo(0);
20
 
21
 
21
    const float pi = M_PI;
22
    const float pi = M_PI;
22
 
23
 
23
    // Loop through vector
24
    // Loop through vector
24
    for(int i=0; i<phaseVector.rows; i++){
25
    for(int i=0; i<phaseVector.rows; i++){
25
        // Amplitude of channels
26
        // Amplitude of channels
26
        float amp = 0.5*(1+cos(2*pi*i/pitch - phase));
27
        float amp = 0.5*(1+cos(2*pi*i/pitch - phase));
27
        phaseVector.at<cv::Vec3b>(i, 0) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
28
        phaseVector.at<cv::Vec3b>(i, 0) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
28
    }
29
    }
29
 
30
 
30
    return phaseVector;
31
    return phaseVector;
31
}
32
}
32
 
33
 
33
AlgorithmPhaseShiftThreeFreq::AlgorithmPhaseShiftThreeFreq(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
34
AlgorithmPhaseShiftThreeFreq::AlgorithmPhaseShiftThreeFreq(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
34
 
35
 
35
    // Set N
36
    // Set N
36
    N = 2+nStepsPrimary+nStepsSecondary+nStepsTertiary;
37
    N = 2+nStepsPrimary+nStepsSecondary+nStepsTertiary;
37
 
38
 
38
    // Determine the secondary (wider) period
39
    // Determine the tertiary period to fulfill the heterodyne condition
39
    float pSecondary = (screenCols*periodPrimary)/(screenCols-periodPrimary);
40
    float periodTertiary = (screenCols*periodPrimary*periodSecondary)/(periodPrimary*periodSecondary+2*screenCols*periodPrimary-screenCols*periodSecondary);
40
 
41
 
41
    // all on pattern
42
    // all on pattern
42
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
43
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
43
    patterns.push_back(allOn);
44
    patterns.push_back(allOn);
44
 
45
 
45
    // all off pattern
46
    // all off pattern
46
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
47
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
47
    patterns.push_back(allOff);
48
    patterns.push_back(allOff);
48
 
49
 
49
    // Precompute encoded patterns
50
    // Precompute encoded patterns
50
    const float pi = M_PI;
51
    const float pi = M_PI;
51
 
52
 
52
    // Primary encoding patterns
53
    // Primary encoding patterns
53
    for(unsigned int i=0; i<nStepsPrimary; i++){
54
    for(unsigned int i=0; i<nStepsPrimary; i++){
54
        float phase = 2.0*pi/nStepsPrimary * i;
55
        float phase = 2.0*pi/nStepsPrimary * i;
55
        float pitch = periodPrimary;
56
        float pitch = periodPrimary;
56
        cv::Mat patternI(1,1,CV_8U);
57
        cv::Mat patternI(1,1,CV_8U);
57
        patternI = computePhaseVector(screenCols, phase, pitch);
58
        patternI = computePhaseVector(screenCols, phase, pitch);
58
        patterns.push_back(patternI.t());
59
        patterns.push_back(patternI.t());
59
    }
60
    }
60
 
61
 
61
    // Secondary encoding patterns
62
    // Secondary encoding patterns
62
    for(unsigned int i=0; i<nStepsSecondary; i++){
63
    for(unsigned int i=0; i<nStepsSecondary; i++){
63
        float phase = 2.0*pi/nStepsSecondary * i;
64
        float phase = 2.0*pi/nStepsSecondary * i;
64
        float pitch = pSecondary;
65
        float pitch = periodSecondary;
65
        cv::Mat patternI(1,1,CV_8U);
66
        cv::Mat patternI(1,1,CV_8U);
66
        patternI = computePhaseVector(screenCols, phase, pitch);
67
        patternI = computePhaseVector(screenCols, phase, pitch);
67
        patterns.push_back(patternI.t());
68
        patterns.push_back(patternI.t());
68
    }
69
    }
69
    // Tertiary encoding patterns
70
    // Tertiary encoding patterns
70
    for(unsigned int i=0; i<nStepsTertiary; i++){
71
    for(unsigned int i=0; i<nStepsTertiary; i++){
71
        float phase = 2.0*pi/nStepsTertiary * i;
72
        float phase = 2.0*pi/nStepsTertiary * i;
72
        float pitch = pTertiary;
73
        float pitch = periodTertiary;
73
        cv::Mat patternI(1,1,CV_8U);
74
        cv::Mat patternI(1,1,CV_8U);
74
        patternI = computePhaseVector(screenCols, phase, pitch);
75
        patternI = computePhaseVector(screenCols, phase, pitch);
75
        patterns.push_back(patternI.t());
76
        patterns.push_back(patternI.t());
76
    }
77
    }
77
 
78
 
78
}
79
}
79
 
80
 
80
cv::Mat AlgorithmPhaseShiftThreeFreq::getEncodingPattern(unsigned int depth){
81
cv::Mat AlgorithmPhaseShiftThreeFreq::getEncodingPattern(unsigned int depth){
81
    return patterns[depth];
82
    return patterns[depth];
82
}
83
}
83
 
84
 
84
 
85
 
85
// Absolute phase from 3 frames
86
// Absolute phase from 3 frames
86
static cv::Mat getPhase(const cv::Mat I1, const cv::Mat I2, const cv::Mat I3){
87
static cv::Mat getPhase(const cv::Mat I1, const cv::Mat I2, const cv::Mat I3){
87
 
88
 
88
    cv::Mat_<float> I1_(I1);
89
    cv::Mat_<float> I1_(I1);
89
    cv::Mat_<float> I2_(I2);
90
    cv::Mat_<float> I2_(I2);
90
    cv::Mat_<float> I3_(I3);
91
    cv::Mat_<float> I3_(I3);
91
 
92
 
92
    cv::Mat phase;
93
    cv::Mat phase;
93
 
94
 
94
    // One call approach
95
    // One call approach
95
    cv::phase(2.0*I1_-I3_-I2_, sqrt(3.0)*(I2_-I3_), phase);
96
    cv::phase(2.0*I1_-I3_-I2_, sqrt(3.0)*(I2_-I3_), phase);
96
    return phase;
97
    return phase;
97
 
98
 
98
}
99
}
99
 
100
 
100
// Phase unwrapping by means of a phase cue
101
// Phase unwrapping by means of a phase cue
101
static cv::Mat unwrapWithCue(const cv::Mat up, const cv::Mat upCue, float nPhases){
102
static cv::Mat unwrapWithCue(const cv::Mat up, const cv::Mat upCue, float nPhases){
102
 
103
 
103
    const float pi = M_PI;
104
    const float pi = M_PI;
104
 
105
 
105
    // Determine number of jumps
106
    // Determine number of jumps
106
    cv::Mat P = (upCue*nPhases-up)/(2.0*pi);
107
    cv::Mat P = (upCue*nPhases-up)/(2.0*pi);
107
 
108
 
108
    // Round to integers
109
    // Round to integers
109
    P.convertTo(P, CV_8U);
110
    P.convertTo(P, CV_8U);
110
    P.convertTo(P, CV_32F);
111
    P.convertTo(P, CV_32F);
111
 
112
 
112
    // Add to phase
113
    // Add to phase
113
    cv::Mat upUnwrapped = up + P*2*pi;
114
    cv::Mat upUnwrapped = up + P*2*pi;
114
 
115
 
115
    // Scale to range [0; 2pi]
116
    // Scale to range [0; 2pi]
116
    upUnwrapped *= 1.0/nPhases;
117
    upUnwrapped *= 1.0/nPhases;
117
 
118
 
118
    return upUnwrapped;
119
    return upUnwrapped;
119
}
120
}
120
 
121
 
121
// Absolute phase and magnitude from N frames
122
// Absolute phase and magnitude from N frames
122
static std::vector<cv::Mat> getDFTComponents(const std::vector<cv::Mat> frames){
123
static std::vector<cv::Mat> getDFTComponents(const std::vector<cv::Mat> frames){
123
 
124
 
124
    unsigned int N = frames.size();
125
    unsigned int N = frames.size();
125
 
126
 
126
//    std::vector<cv::Mat> framesReverse = frames;
127
//    std::vector<cv::Mat> framesReverse = frames;
127
//    std::reverse(framesReverse.begin(), framesReverse.end());
128
//    std::reverse(framesReverse.begin(), framesReverse.end());
128
 
129
 
129
    // DFT approach
130
    // DFT approach
130
    cv::Mat I;
131
    cv::Mat I;
131
    cv::merge(frames, I);
132
    cv::merge(frames, I);
132
    unsigned int w = I.cols;
133
    unsigned int w = I.cols;
133
    unsigned int h = I.rows;
134
    unsigned int h = I.rows;
134
    I = I.reshape(1, h*w);
135
    I = I.reshape(1, h*w);
135
    I.convertTo(I, CV_32F);
136
    I.convertTo(I, CV_32F);
136
    cv::Mat fI;
137
    cv::Mat fI;
137
    cv::dft(I, fI, cv::DFT_ROWS + cv::DFT_COMPLEX_OUTPUT);
138
    cv::dft(I, fI, cv::DFT_ROWS + cv::DFT_COMPLEX_OUTPUT);
138
    fI = fI.reshape(N*2, h);
139
    fI = fI.reshape(N*2, h);
139
 
140
 
140
    std::vector<cv::Mat> fIcomp;
141
    std::vector<cv::Mat> fIcomp;
141
    cv::split(fI, fIcomp);
142
    cv::split(fI, fIcomp);
142
 
143
 
143
    return fIcomp;
144
    return fIcomp;
144
 
145
 
145
}
146
}
146
 
147
 
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){
148
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){
148
 
149
 
149
    const float pi = M_PI;
150
    const float pi = M_PI;
150
 
151
 
151
    assert(frames0.size() == N);
152
    assert(frames0.size() == N);
152
    assert(frames1.size() == N);
153
    assert(frames1.size() == N);
153
 
154
 
154
    int frameRows = frames0[0].rows;
155
    int frameRows = frames0[0].rows;
155
    int frameCols = frames0[0].cols;
156
    int frameCols = frames0[0].cols;
156
 
157
 
157
    // Gray-scale everything
158
    // Gray-scale everything
158
    std::vector<cv::Mat> frames0Gray(N);
159
    std::vector<cv::Mat> frames0Gray(N);
159
    std::vector<cv::Mat> frames1Gray(N);
160
    std::vector<cv::Mat> frames1Gray(N);
160
    for(int i=0; i<N; i++){
161
    for(int i=0; i<N; i++){
161
        cv::cvtColor(frames0[i], frames0Gray[i], CV_BayerBG2GRAY);
162
        cv::cvtColor(frames0[i], frames0Gray[i], CV_BayerBG2GRAY);
162
        cv::cvtColor(frames1[i], frames1Gray[i], CV_BayerBG2GRAY);
163
        cv::cvtColor(frames1[i], frames1Gray[i], CV_BayerBG2GRAY);
163
    }
164
    }
164
 
165
 
165
    // Decode camera0
166
    // Decode camera0
166
    std::vector<cv::Mat> frames0Primary(frames0Gray.begin()+2, frames0Gray.begin()+2+nStepsPrimary);
167
    std::vector<cv::Mat> frames0Primary(frames0Gray.begin()+2, frames0Gray.begin()+2+nStepsPrimary);
167
    std::vector<cv::Mat> frames0Secondary(frames0Gray.begin()+2+nStepsPrimary, frames0Gray.end()-nStepsTertiary);
168
    std::vector<cv::Mat> frames0Secondary(frames0Gray.begin()+2+nStepsPrimary, frames0Gray.end()-nStepsTertiary);
168
    std::vector<cv::Mat> frames0Tertiary(frames0Gray.end()-nStepsTertiary, frames0Gray.end());
169
    std::vector<cv::Mat> frames0Tertiary(frames0Gray.end()-nStepsTertiary, frames0Gray.end());
169
    std::vector<cv::Mat> F0Primary = getDFTComponents(frames0Primary);
-
 
170
 
170
 
-
 
171
    std::vector<cv::Mat> F0Primary = getDFTComponents(frames0Primary);
171
    cv::Mat up0Primary;
172
    cv::Mat up0Primary;
172
    cv::phase(F0Primary[2], -F0Primary[3], up0Primary);
173
    cv::phase(F0Primary[2], -F0Primary[3], up0Primary);
173
    std::vector<cv::Mat> F0Secondary = getDFTComponents(frames0Secondary);
174
    std::vector<cv::Mat> F0Secondary = getDFTComponents(frames0Secondary);
174
    cv::Mat up0Secondary;
175
    cv::Mat up0Secondary;
175
    cv::phase(F0Secondary[2], -F0Secondary[3], up0Secondary);
176
    cv::phase(F0Secondary[2], -F0Secondary[3], up0Secondary);
176
    std::vector<cv::Mat> F0Tertiary = getDFTComponents(frames0Tertiary);
177
    std::vector<cv::Mat> F0Tertiary = getDFTComponents(frames0Tertiary);
177
    cv::Mat up0Tertiary;
178
    cv::Mat up0Tertiary;
178
    cv::phase(F0Tertiary[2], -F0Tertiary[3], up0Tertiary);
179
    cv::phase(F0Tertiary[2], -F0Tertiary[3], up0Tertiary);
179
 
180
 
180
    cv::Mat up0EquivalentPS = up0Primary - up0Secondary;
181
    cv::Mat up0EquivalentPS = up0Primary - up0Secondary;
181
    up0EquivalentPS = cvtools::modulo(up0EquivalentPS, 2.0*pi);
182
    up0EquivalentPS = cvtools::modulo(up0EquivalentPS, 2.0*pi);
182
 
183
 
183
    cv::Mat up0EquivalentST = up0Secondary - up0Tertiary;
184
    cv::Mat up0EquivalentST = up0Secondary - up0Tertiary;
184
    up0EquivalentST = cvtools::modulo(up0EquivalentST, 2.0*pi);
185
    up0EquivalentST = cvtools::modulo(up0EquivalentST, 2.0*pi);
185
 
186
 
186
    cv::Mat up0Equivalent = up0EquivalentPS - up0EquivalentST;
187
    cv::Mat up0Equivalent = up0EquivalentPS - up0EquivalentST;
187
    up0Equivalent = cvtools::modulo(up0Equivalent, 2.0*pi);
188
    up0Equivalent = cvtools::modulo(up0Equivalent, 2.0*pi);
188
 
189
 
-
 
190
cv::Mat up0Equivalent2 = unwrapWithCue(up0EquivalentPS, up0Equivalent, (float)screenCols/periodPrimary);
-
 
191
 
189
    cv::Mat up0 = unwrapWithCue(up0Primary, up0Equivalent, (float)screenCols/periodPrimary);
192
    cv::Mat up0 = unwrapWithCue(up0Primary, up0Equivalent2, (float)screenCols/periodPrimary);
190
    up0 *= screenCols/(2.0*pi);
193
    up0 *= screenCols/(2.0*pi);
191
    cv::Mat amplitude0;
194
    cv::Mat amplitude0;
192
    cv::magnitude(F0Primary[2], -F0Primary[3], amplitude0);
195
    cv::magnitude(F0Primary[2], -F0Primary[3], amplitude0);
193
 
196
 
194
    // Decode camera1
197
    // Decode camera1
195
    std::vector<cv::Mat> frames1Primary(frames1Gray.begin()+2, frames1Gray.begin()+2+nStepsPrimary);
198
    std::vector<cv::Mat> frames1Primary(frames1Gray.begin()+2, frames1Gray.begin()+2+nStepsPrimary);
196
    std::vector<cv::Mat> frames1Secondary(frames1Gray.begin()+2+nStepsPrimary, frames1Gray.end());
199
    std::vector<cv::Mat> frames1Secondary(frames1Gray.begin()+2+nStepsPrimary, frames1Gray.end()-nStepsTertiary);
-
 
200
    std::vector<cv::Mat> frames1Tertiary(frames1Gray.end()-nStepsTertiary, frames1Gray.end());
-
 
201
 
197
    std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
202
    std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
198
    cv::Mat up1Primary;
203
    cv::Mat up1Primary;
199
    cv::phase(F1Primary[2], -F1Primary[3], up1Primary);
204
    cv::phase(F1Primary[2], -F1Primary[3], up1Primary);
200
    std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
205
    std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
201
    cv::Mat up1Secondary;
206
    cv::Mat up1Secondary;
202
    cv::phase(F1Secondary[2], -F1Secondary[3], up1Secondary);
207
    cv::phase(F1Secondary[2], -F1Secondary[3], up1Secondary);
203
    std::vector<cv::Mat> F1Tertiary = getDFTComponents(frames1Tertiary);
208
    std::vector<cv::Mat> F1Tertiary = getDFTComponents(frames1Tertiary);
204
    cv::Mat up1Tertiary;
209
    cv::Mat up1Tertiary;
205
    cv::phase(F1Tertiary[2], -F1Tertiary[3], up1Tertiary);
210
    cv::phase(F1Tertiary[2], -F1Tertiary[3], up1Tertiary);
206
 
211
 
207
    cv::Mat up1EquivalentPS = up1Primary - up1Secondary;
212
    cv::Mat up1EquivalentPS = up1Primary - up1Secondary;
208
    up1EquivalentPS = cvtools::modulo(up1EquivalentPS, 2.0*pi);
213
    up1EquivalentPS = cvtools::modulo(up1EquivalentPS, 2.0*pi);
209
 
214
 
210
    cv::Mat up1EquivalentST = up1Secondary - up1Tertiary;
215
    cv::Mat up1EquivalentST = up1Secondary - up1Tertiary;
211
    up1EquivalentST = cvtools::modulo(up1EquivalentST, 2.0*pi);
216
    up1EquivalentST = cvtools::modulo(up1EquivalentST, 2.0*pi);
212
 
217
 
213
    cv::Mat up1Equivalent = up1EquivalentPS - up1EquivalentST;
218
    cv::Mat up1Equivalent = up1EquivalentPS - up1EquivalentST;
214
    up1Equivalent = cvtools::modulo(up1Equivalent, 2.0*pi);
219
    up1Equivalent = cvtools::modulo(up1Equivalent, 2.0*pi);
215
 
220
 
216
    cv::Mat up1 = unwrapWithCue(up1Primary, up1Equivalent, (float)screenCols/periodPrimary);
221
    cv::Mat up1 = unwrapWithCue(up1Primary, up1Equivalent, (float)screenCols/periodPrimary);
217
    up1 *= screenCols/(2.0*pi);
222
    up1 *= screenCols/(2.0*pi);
218
    cv::Mat amplitude1;
223
    cv::Mat amplitude1;
219
    cv::magnitude(F1Primary[2], -F1Primary[3], amplitude1);
224
    cv::magnitude(F1Primary[2], -F1Primary[3], amplitude1);
220
 
225
 
221
cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
226
//cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
222
cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
227
//cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
-
 
228
//cvtools::writeMat(up0Tertiary, "up0Tertiary.mat", "up0Tertiary");
223
cvtools::writeMat(up0Equivalent, "up0Equivalent.mat", "up0Equivalent");
229
//cvtools::writeMat(up0Equivalent, "up0Equivalent.mat", "up0Equivalent");
-
 
230
cvtools::writeMat(up0Equivalent2, "up0Equivalent2.mat", "up0Equivalent2");
-
 
231
//cvtools::writeMat(up0EquivalentPS, "up0EquivalentPS.mat", "up0EquivalentPS");
-
 
232
//cvtools::writeMat(up0EquivalentST, "up0EquivalentST.mat", "up0EquivalentST");
224
cvtools::writeMat(up0, "up0.mat", "up0");
233
//cvtools::writeMat(up0, "up0.mat", "up0");
-
 
234
//cvtools::writeMat(up1, "up1.mat", "up1");
225
cvtools::writeMat(amplitude0, "amplitude0.mat", "amplitude0");
235
//cvtools::writeMat(amplitude0, "amplitude0.mat", "amplitude0");
226
 
236
 
227
    // Rectifying homographies (rotation+projections)
237
    // Rectifying homographies (rotation+projections)
228
    cv::Size frameSize(frameCols, frameRows);
238
    cv::Size frameSize(frameCols, frameRows);
229
    cv::Mat R, T;
239
    cv::Mat R, T;
230
    // stereoRectify segfaults unless R is double precision
240
    // stereoRectify segfaults unless R is double precision
231
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
241
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
232
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
242
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
233
    cv::Mat R0, R1, P0, P1, QRect;
243
    cv::Mat R0, R1, P0, P1, QRect;
234
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
244
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
235
 
245
 
236
    // Interpolation maps (lens distortion and rectification)
246
    // Interpolation maps (lens distortion and rectification)
237
    cv::Mat map0X, map0Y, map1X, map1Y;
247
    cv::Mat map0X, map0Y, map1X, map1Y;
238
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
248
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
239
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
249
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
240
 
250
 
241
    // Phase remaps
251
    // Phase remaps
242
    cv::Mat up0Rect, up1Rect;
252
    cv::Mat up0Rect, up1Rect;
243
    cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_LINEAR);
253
    cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_LINEAR);
244
    cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_LINEAR);
254
    cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_LINEAR);
245
 
255
 
246
    // amplitude remaps
256
    // amplitude remaps
247
    cv::Mat amplitude0Rect, amplitude1Rect;
257
    cv::Mat amplitude0Rect, amplitude1Rect;
248
    cv::remap(amplitude0, amplitude0Rect, map0X, map0Y, CV_INTER_LINEAR);
258
    cv::remap(amplitude0, amplitude0Rect, map0X, map0Y, CV_INTER_LINEAR);
249
    cv::remap(amplitude1, amplitude1Rect, map1X, map1Y, CV_INTER_LINEAR);
259
    cv::remap(amplitude1, amplitude1Rect, map1X, map1Y, CV_INTER_LINEAR);
250
 
260
 
251
//cvtools::writeMat(up0Rect, "up0Rect.mat", "up0Rect");
261
//cvtools::writeMat(up0Rect, "up0Rect.mat", "up0Rect");
252
//cvtools::writeMat(up1Rect, "up1Rect.mat", "up1Rect");
262
//cvtools::writeMat(up1Rect, "up1Rect.mat", "up1Rect");
253
 
263
 
254
    // color debayer and remap
264
    // color debayer and remap
255
    cv::Mat color0Rect, color1Rect;
265
    cv::Mat color0Rect, color1Rect;
256
//    frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
266
//    frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
257
    cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
267
    cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
258
    cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
268
    cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
259
 
269
 
260
//    frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
270
//    frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
261
    cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
271
    cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
262
    cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
272
    cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
263
 
273
 
264
//cvtools::writeMat(frames0Rect[18], "frames0Rect_18.mat", "frames0Rect_18");
274
//cvtools::writeMat(frames0Rect[18], "frames0Rect_18.mat", "frames0Rect_18");
265
//cvtools::writeMat(frames0Rect[19], "frames0Rect_19.mat", "frames0Rect_19");
275
//cvtools::writeMat(frames0Rect[19], "frames0Rect_19.mat", "frames0Rect_19");
266
 
276
 
267
//cvtools::writeMat(color0Rect, "color0Rect.mat", "color0Rect");
277
//cvtools::writeMat(color0Rect, "color0Rect.mat", "color0Rect");
268
//cvtools::writeMat(color1Rect, "color1Rect.mat", "color1Rect");
278
//cvtools::writeMat(color1Rect, "color1Rect.mat", "color1Rect");
269
 
279
 
270
    // On/off remaps
280
    // On/off remaps
271
    cv::Mat frames0OnRect, frames0OffRect;
281
    cv::Mat frames0OnRect, frames0OffRect;
272
    cv::remap(frames0Gray[0], frames0OnRect, map0X, map0Y, CV_INTER_LINEAR);
282
    cv::remap(frames0Gray[0], frames0OnRect, map0X, map0Y, CV_INTER_LINEAR);
273
    cv::remap(frames0Gray[1], frames0OffRect, map0X, map0Y, CV_INTER_LINEAR);
283
    cv::remap(frames0Gray[1], frames0OffRect, map0X, map0Y, CV_INTER_LINEAR);
274
 
284
 
275
    cv::Mat frames1OnRect, frames1OffRect;
285
    cv::Mat frames1OnRect, frames1OffRect;
276
    cv::remap(frames1Gray[0], frames1OnRect, map1X, map1Y, CV_INTER_LINEAR);
286
    cv::remap(frames1Gray[0], frames1OnRect, map1X, map1Y, CV_INTER_LINEAR);
277
    cv::remap(frames1Gray[1], frames1OffRect, map1X, map1Y, CV_INTER_LINEAR);
287
    cv::remap(frames1Gray[1], frames1OffRect, map1X, map1Y, CV_INTER_LINEAR);
278
 
288
 
279
    // Occlusion masks
289
    // Occlusion masks
280
    cv::Mat occlusion0Rect, occlusion1Rect;
290
    cv::Mat occlusion0Rect, occlusion1Rect;
281
    cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
291
    cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
282
    occlusion0Rect = (occlusion0Rect > 25) & (occlusion0Rect < 250);
292
    occlusion0Rect = (occlusion0Rect > 25) & (occlusion0Rect < 250);
283
    cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
293
    cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
284
    occlusion1Rect = (occlusion1Rect > 25) & (occlusion1Rect < 250);
294
    occlusion1Rect = (occlusion1Rect > 25) & (occlusion1Rect < 250);
285
 
295
 
286
    // Threshold on energy at primary frequency
296
    // Threshold on energy at primary frequency
287
    occlusion0Rect = occlusion0Rect & (amplitude0Rect > 5.0*nStepsPrimary);
297
    occlusion0Rect = occlusion0Rect & (amplitude0Rect > 5.0*nStepsPrimary);
288
    occlusion1Rect = occlusion1Rect & (amplitude1Rect > 5.0*nStepsPrimary);
298
    occlusion1Rect = occlusion1Rect & (amplitude1Rect > 5.0*nStepsPrimary);
289
 
299
 
290
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
300
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
291
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
301
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
292
 
302
 
293
    // Erode occlusion masks
303
    // Erode occlusion masks
294
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
304
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
295
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
305
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
296
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
306
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
297
 
307
 
298
    // Threshold on gradient of phase
308
    // Threshold on gradient of phase
299
    cv::Mat edges0;
309
    cv::Mat edges0;
300
    cv::Sobel(up0Rect, edges0, -1, 1, 1, 5);
310
    cv::Sobel(up0Rect, edges0, -1, 1, 1, 5);
301
    occlusion0Rect = occlusion0Rect & (abs(edges0) < 150);
311
    occlusion0Rect = occlusion0Rect & (abs(edges0) < 150);
302
 
312
 
303
    cv::Mat edges1;
313
    cv::Mat edges1;
304
    cv::Sobel(up1Rect, edges1, -1, 1, 1, 5);
314
    cv::Sobel(up1Rect, edges1, -1, 1, 1, 5);
305
    occlusion1Rect = occlusion1Rect & (abs(edges1) < 150);
315
    occlusion1Rect = occlusion1Rect & (abs(edges1) < 150);
306
 
316
 
307
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
317
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
308
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
318
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
309
 
319
 
310
    // Match phase maps
320
    // Match phase maps
311
    int frameRectRows = map0X.rows;
321
    int frameRectRows = map0X.rows;
312
    int frameRectCols = map0X.cols;
322
    int frameRectCols = map0X.cols;
313
 
323
 
314
    // camera0 against camera1
324
    // camera0 against camera1
315
    std::vector<cv::Vec2f> q0Rect, q1Rect;
325
    std::vector<cv::Vec2f> q0Rect, q1Rect;
316
    for(int row=0; row<frameRectRows; row++){
326
    for(int row=0; row<frameRectRows; row++){
317
        for(int col=0; col<frameRectCols; col++){
327
        for(int col=0; col<frameRectCols; col++){
318
 
328
 
319
            if(!occlusion0Rect.at<char>(row,col))
329
            if(!occlusion0Rect.at<char>(row,col))
320
                continue;
330
                continue;
321
 
331
 
322
            float up0i = up0Rect.at<float>(row,col);
332
            float up0i = up0Rect.at<float>(row,col);
323
            for(int col1=0; col1<up1Rect.cols-1; col1++){
333
            for(int col1=0; col1<up1Rect.cols-1; col1++){
324
 
334
 
325
                if(!occlusion1Rect.at<char>(row,col1) || !occlusion1Rect.at<char>(row,col1+1))
335
                if(!occlusion1Rect.at<char>(row,col1) || !occlusion1Rect.at<char>(row,col1+1))
326
                    continue;
336
                    continue;
327
 
337
 
328
                float up1Left = up1Rect.at<float>(row,col1);
338
                float up1Left = up1Rect.at<float>(row,col1);
329
                float up1Right = up1Rect.at<float>(row,col1+1);
339
                float up1Right = up1Rect.at<float>(row,col1+1);
330
 
340
 
331
                if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 0.5) && (up1Right-up0i < 0.5)){
341
                if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 0.5) && (up1Right-up0i < 0.5)){
332
 
342
 
333
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
343
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
334
 
344
 
335
                    q0Rect.push_back(cv::Point2f(col, row));
345
                    q0Rect.push_back(cv::Point2f(col, row));
336
                    q1Rect.push_back(cv::Point2f(col1i, row));
346
                    q1Rect.push_back(cv::Point2f(col1i, row));
337
 
347
 
338
                    break;
348
                    break;
339
                }
349
                }
340
            }
350
            }
341
        }
351
        }
342
    }
352
    }
343
 
353
 
344
//    // camera1 against camera0
354
//    // camera1 against camera0
345
//    for(int row=0; row<frameRectRows; row++){
355
//    for(int row=0; row<frameRectRows; row++){
346
//        for(int col=0; col<frameRectCols; col++){
356
//        for(int col=0; col<frameRectCols; col++){
347
 
357
 
348
//            if(!occlusion1Rect.at<char>(row,col))
358
//            if(!occlusion1Rect.at<char>(row,col))
349
//                continue;
359
//                continue;
350
 
360
 
351
//            float up1i = up1Rect.at<float>(row,col);
361
//            float up1i = up1Rect.at<float>(row,col);
352
//            for(int col0=0; col0<up0Rect.cols-1; col0++){
362
//            for(int col0=0; col0<up0Rect.cols-1; col0++){
353
 
363
 
354
//                if(!occlusion0Rect.at<char>(row,col0) || !occlusion0Rect.at<char>(row,col0+1))
364
//                if(!occlusion0Rect.at<char>(row,col0) || !occlusion0Rect.at<char>(row,col0+1))
355
//                    continue;
365
//                    continue;
356
 
366
 
357
//                float up0Left = up0Rect.at<float>(row,col0);
367
//                float up0Left = up0Rect.at<float>(row,col0);
358
//                float up0Right = up0Rect.at<float>(row,col0+1);
368
//                float up0Right = up0Rect.at<float>(row,col0+1);
359
 
369
 
360
//                if((up0Left <= up1i) && (up1i <= up0Right) && (up1i-up0Left < 1) && (up0Right-up1i < 1)){
370
//                if((up0Left <= up1i) && (up1i <= up0Right) && (up1i-up0Left < 1) && (up0Right-up1i < 1)){
361
 
371
 
362
//                    float col0i = col0 + (up1i-up0Left)/(up0Right-up0Left);
372
//                    float col0i = col0 + (up1i-up0Left)/(up0Right-up0Left);
363
 
373
 
364
//                    q1Rect.push_back(cv::Point2f(col, row));
374
//                    q1Rect.push_back(cv::Point2f(col, row));
365
//                    q0Rect.push_back(cv::Point2f(col0i, row));
375
//                    q0Rect.push_back(cv::Point2f(col0i, row));
366
 
376
 
367
//                    break;
377
//                    break;
368
//                }
378
//                }
369
//            }
379
//            }
370
//        }
380
//        }
371
//    }
381
//    }
372
 
382
 
373
    int nMatches = q0Rect.size();
383
    int nMatches = q0Rect.size();
374
 
384
 
375
    if(nMatches < 1){
385
    if(nMatches < 1){
376
        Q.resize(0);
386
        Q.resize(0);
377
        color.resize(0);
387
        color.resize(0);
378
 
388
 
379
        return;
389
        return;
380
    }
390
    }
381
 
391
 
382
    // Retrieve color information
392
    // Retrieve color information
383
    color.resize(nMatches);
393
    color.resize(nMatches);
384
    for(int i=0; i<nMatches; i++){
394
    for(int i=0; i<nMatches; i++){
385
 
395
 
386
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
396
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
387
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
397
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
388
 
398
 
389
        color[i] = 0.5*c0 + 0.5*c1;
399
        color[i] = 0.5*c0 + 0.5*c1;
390
    }
400
    }
391
 
401
 
392
    // Triangulate points
402
    // Triangulate points
393
    cv::Mat QMatHomogenous, QMat;
403
    cv::Mat QMatHomogenous, QMat;
394
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
404
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
395
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
405
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
396
 
406
 
397
    // Undo rectification
407
    // Undo rectification
398
    cv::Mat R0Inv;
408
    cv::Mat R0Inv;
399
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
409
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
400
    QMat = R0Inv*QMat;
410
    QMat = R0Inv*QMat;
401
 
411
 
402
    cvtools::matToPoints3f(QMat, Q);
412
    cvtools::matToPoints3f(QMat, Q);
403
 
413
 
404
}
414
}
405
 
415