Subversion Repositories seema-scanner

Rev

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

Rev 143 Rev 148
1
#include "AlgorithmPhaseShiftTwoFreq.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 = 16; // number of shifts/steps in primary
10
static unsigned int nStepsPrimary = 16; // number of shifts/steps in primary
11
static unsigned int nStepsSecondary = 8; // number of shifts/steps in secondary
11
static unsigned int nStepsSecondary = 8; // number of shifts/steps in secondary
12
static float periodPrimary = 48; // primary period
12
static float periodPrimary = 48; // 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);
18
    //phaseVector.setTo(0);
18
    //phaseVector.setTo(0);
19
 
19
 
20
    const float pi = M_PI;
20
    const float pi = M_PI;
21
 
21
 
22
    // Loop through vector
22
    // Loop through vector
23
    for(int i=0; i<phaseVector.rows; i++){
23
    for(int i=0; i<phaseVector.rows; i++){
24
        // Amplitude of channels
24
        // Amplitude of channels
25
        float amp = 0.5*(1+cos(2*pi*i/pitch - phase));
25
        float amp = 0.5*(1+cos(2*pi*i/pitch - phase));
26
        phaseVector.at<cv::Vec3b>(i, 0) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
26
        phaseVector.at<cv::Vec3b>(i, 0) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
27
    }
27
    }
28
 
28
 
29
    return phaseVector;
29
    return phaseVector;
30
}
30
}
31
 
31
 
32
AlgorithmPhaseShiftTwoFreq::AlgorithmPhaseShiftTwoFreq(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
38
    float pSecondary = (screenCols*periodPrimary)/(screenCols-periodPrimary);
38
    float pSecondary = (screenCols*periodPrimary)/(screenCols-periodPrimary);
39
 
39
 
40
    // all on pattern
40
    // all on pattern
41
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
41
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
42
    patterns.push_back(allOn);
42
    patterns.push_back(allOn);
43
 
43
 
44
    // all off pattern
44
    // all off pattern
45
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
45
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
46
    patterns.push_back(allOff);
46
    patterns.push_back(allOff);
47
 
47
 
48
    // Precompute encoded patterns
48
    // Precompute encoded patterns
49
    const float pi = M_PI;
49
    const float pi = M_PI;
50
 
50
 
51
    // Primary encoding patterns
51
    // Primary encoding patterns
52
    for(unsigned int i=0; i<nStepsPrimary; i++){
52
    for(unsigned int i=0; i<nStepsPrimary; i++){
53
        float phase = 2.0*pi/nStepsPrimary * i;
53
        float phase = 2.0*pi/nStepsPrimary * i;
54
        float pitch = periodPrimary;
54
        float pitch = periodPrimary;
55
        cv::Mat patternI(1,1,CV_8U);
55
        cv::Mat patternI(1,1,CV_8U);
56
        patternI = computePhaseVector(screenCols, phase, pitch);
56
        patternI = computePhaseVector(screenCols, phase, pitch);
57
        patterns.push_back(patternI.t());
57
        patterns.push_back(patternI.t());
58
    }
58
    }
59
 
59
 
60
    // Secondary encoding patterns
60
    // Secondary encoding patterns
61
    for(unsigned int i=0; i<nStepsSecondary; i++){
61
    for(unsigned int i=0; i<nStepsSecondary; i++){
62
        float phase = 2.0*pi/nStepsSecondary * i;
62
        float phase = 2.0*pi/nStepsSecondary * i;
63
        float pitch = pSecondary;
63
        float pitch = pSecondary;
64
        cv::Mat patternI(1,1,CV_8U);
64
        cv::Mat patternI(1,1,CV_8U);
65
        patternI = computePhaseVector(screenCols, phase, pitch);
65
        patternI = computePhaseVector(screenCols, phase, pitch);
66
        patterns.push_back(patternI.t());
66
        patterns.push_back(patternI.t());
67
    }
67
    }
68
 
68
 
69
 
69
 
70
}
70
}
71
 
71
 
72
cv::Mat AlgorithmPhaseShiftTwoFreq::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
static 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
 
84
    cv::Mat phase;
84
    cv::Mat phase;
85
 
85
 
86
    // One call approach
86
    // One call approach
87
    cv::phase(2.0*I1_-I3_-I2_, sqrt(3.0)*(I2_-I3_), phase);
87
    cv::phase(2.0*I1_-I3_-I2_, sqrt(3.0)*(I2_-I3_), phase);
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
static 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.0*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
 
104
    // Add to phase
104
    // Add to phase
105
    cv::Mat upUnwrapped = up + P*2*pi;
105
    cv::Mat upUnwrapped = up + P*2*pi;
106
 
106
 
107
    // Scale to range [0; 2pi]
107
    // Scale to range [0; 2pi]
108
    upUnwrapped *= 1.0/nPhases;
108
    upUnwrapped *= 1.0/nPhases;
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
static 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());
120
 
120
 
121
    // DFT approach
121
    // DFT approach
122
    cv::Mat I;
122
    cv::Mat I;
123
    cv::merge(frames, I);
123
    cv::merge(frames, I);
124
    unsigned int w = I.cols;
124
    unsigned int w = I.cols;
125
    unsigned int h = I.rows;
125
    unsigned int h = I.rows;
126
    I = I.reshape(1, h*w);
126
    I = I.reshape(1, h*w);
127
    I.convertTo(I, CV_32F);
127
    I.convertTo(I, CV_32F);
128
    cv::Mat fI;
128
    cv::Mat fI;
129
    cv::dft(I, fI, cv::DFT_ROWS + cv::DFT_COMPLEX_OUTPUT);
129
    cv::dft(I, fI, cv::DFT_ROWS + cv::DFT_COMPLEX_OUTPUT);
130
    fI = fI.reshape(N*2, h);
130
    fI = fI.reshape(N*2, h);
131
 
131
 
132
    std::vector<cv::Mat> fIcomp;
132
    std::vector<cv::Mat> fIcomp;
133
    cv::split(fI, fIcomp);
133
    cv::split(fI, fIcomp);
134
 
134
 
135
    return fIcomp;
135
    return fIcomp;
136
 
136
 
137
}
137
}
138
 
138
 
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){
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);
145
 
145
 
146
    int frameRows = frames0[0].rows;
146
    int frameRows = frames0[0].rows;
147
    int frameCols = frames0[0].cols;
147
    int frameCols = frames0[0].cols;
148
 
148
 
149
    // Gray-scale everything
149
    // Gray-scale everything
150
    std::vector<cv::Mat> frames0Gray(N);
150
    std::vector<cv::Mat> frames0Gray(N);
151
    std::vector<cv::Mat> frames1Gray(N);
151
    std::vector<cv::Mat> frames1Gray(N);
152
    for(int i=0; i<N; i++){
152
    for(int i=0; i<N; i++){
153
        cv::cvtColor(frames0[i], frames0Gray[i], CV_BayerBG2GRAY);
153
        cv::cvtColor(frames0[i], frames0Gray[i], CV_BayerBG2GRAY);
154
        cv::cvtColor(frames1[i], frames1Gray[i], CV_BayerBG2GRAY);
154
        cv::cvtColor(frames1[i], frames1Gray[i], CV_BayerBG2GRAY);
155
    }
155
    }
156
 
156
 
157
    // Decode camera0
157
    // Decode camera0
158
    std::vector<cv::Mat> frames0Primary(frames0Gray.begin()+2, frames0Gray.begin()+2+nStepsPrimary);
158
    std::vector<cv::Mat> frames0Primary(frames0Gray.begin()+2, frames0Gray.begin()+2+nStepsPrimary);
159
    std::vector<cv::Mat> frames0Secondary(frames0Gray.begin()+2+nStepsPrimary, frames0Gray.end());
159
    std::vector<cv::Mat> frames0Secondary(frames0Gray.begin()+2+nStepsPrimary, frames0Gray.end());
160
    std::vector<cv::Mat> F0Primary = getDFTComponents(frames0Primary);
160
    std::vector<cv::Mat> F0Primary = getDFTComponents(frames0Primary);
161
    cv::Mat up0Primary;
161
    cv::Mat up0Primary;
162
    cv::phase(F0Primary[2], -F0Primary[3], up0Primary);
162
    cv::phase(F0Primary[2], -F0Primary[3], up0Primary);
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.0*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.0*pi);
170
    up0 *= screenCols/(2.0*pi);
171
    cv::Mat amplitude0;
171
    cv::Mat amplitude0;
172
    cv::magnitude(F0Primary[2], -F0Primary[3], amplitude0);
172
    cv::magnitude(F0Primary[2], -F0Primary[3], amplitude0);
173
 
173
 
174
    // Decode camera1
174
    // Decode camera1
175
    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);
176
    std::vector<cv::Mat> frames1Secondary(frames1Gray.begin()+2+nStepsPrimary, frames1Gray.end());
176
    std::vector<cv::Mat> frames1Secondary(frames1Gray.begin()+2+nStepsPrimary, frames1Gray.end());
177
    std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
177
    std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
178
    cv::Mat up1Primary;
178
    cv::Mat up1Primary;
179
    cv::phase(F1Primary[2], -F1Primary[3], up1Primary);
179
    cv::phase(F1Primary[2], -F1Primary[3], up1Primary);
180
    //cv::Mat up1Secondary = getPhase(frames1Secondary[0], frames1Secondary[1], frames1Secondary[2]);
180
    //cv::Mat up1Secondary = getPhase(frames1Secondary[0], frames1Secondary[1], frames1Secondary[2]);
181
    std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
181
    std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
182
    cv::Mat up1Secondary;
182
    cv::Mat up1Secondary;
183
    cv::phase(F1Secondary[2], -F1Secondary[3], up1Secondary);
183
    cv::phase(F1Secondary[2], -F1Secondary[3], up1Secondary);
184
    cv::Mat up1Equivalent = up1Primary - up1Secondary;
184
    cv::Mat up1Equivalent = up1Primary - up1Secondary;
185
    up1Equivalent = cvtools::modulo(up1Equivalent, 2.0*pi);
185
    up1Equivalent = cvtools::modulo(up1Equivalent, 2.0*pi);
186
    cv::Mat up1 = unwrapWithCue(up1Primary, up1Equivalent, (float)screenCols/periodPrimary);
186
    cv::Mat up1 = unwrapWithCue(up1Primary, up1Equivalent, (float)screenCols/periodPrimary);
187
    up1 *= screenCols/(2.0*pi);
187
    up1 *= screenCols/(2.0*pi);
188
    cv::Mat amplitude1;
188
    cv::Mat amplitude1;
189
    cv::magnitude(F1Primary[2], -F1Primary[3], amplitude1);
189
    cv::magnitude(F1Primary[2], -F1Primary[3], amplitude1);
190
 
190
 
191
cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
191
//cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
192
cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
192
//cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
193
cvtools::writeMat(up0Equivalent, "up0Equivalent.mat", "up0Equivalent");
193
//cvtools::writeMat(up0Equivalent, "up0Equivalent.mat", "up0Equivalent");
194
cvtools::writeMat(up0, "up0.mat", "up0");
194
//cvtools::writeMat(up0, "up0.mat", "up0");
195
cvtools::writeMat(amplitude0, "amplitude0.mat", "amplitude0");
195
//cvtools::writeMat(amplitude0, "amplitude0.mat", "amplitude0");
196
 
196
 
197
    // Rectifying homographies (rotation+projections)
197
    // Rectifying homographies (rotation+projections)
198
    cv::Size frameSize(frameCols, frameRows);
198
    cv::Size frameSize(frameCols, frameRows);
199
    cv::Mat R, T;
199
    cv::Mat R, T;
200
    // stereoRectify segfaults unless R is double precision
200
    // stereoRectify segfaults unless R is double precision
201
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
201
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
202
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
202
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
203
    cv::Mat R0, R1, P0, P1, QRect;
203
    cv::Mat R0, R1, P0, P1, QRect;
204
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
204
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
205
 
205
 
206
    // Interpolation maps (lens distortion and rectification)
206
    // Interpolation maps (lens distortion and rectification)
207
    cv::Mat map0X, map0Y, map1X, map1Y;
207
    cv::Mat map0X, map0Y, map1X, map1Y;
208
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
208
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
209
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
209
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
210
 
210
 
211
    // Phase remaps
211
    // Phase remaps
212
    cv::Mat up0Rect, up1Rect;
212
    cv::Mat up0Rect, up1Rect;
213
    cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_LINEAR);
213
    cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_LINEAR);
214
    cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_LINEAR);
214
    cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_LINEAR);
215
 
215
 
216
    // amplitude remaps
216
    // amplitude remaps
217
    cv::Mat amplitude0Rect, amplitude1Rect;
217
    cv::Mat amplitude0Rect, amplitude1Rect;
218
    cv::remap(amplitude0, amplitude0Rect, map0X, map0Y, CV_INTER_LINEAR);
218
    cv::remap(amplitude0, amplitude0Rect, map0X, map0Y, CV_INTER_LINEAR);
219
    cv::remap(amplitude1, amplitude1Rect, map1X, map1Y, CV_INTER_LINEAR);
219
    cv::remap(amplitude1, amplitude1Rect, map1X, map1Y, CV_INTER_LINEAR);
220
 
220
 
221
//cvtools::writeMat(up0Rect, "up0Rect.mat", "up0Rect");
221
//cvtools::writeMat(up0Rect, "up0Rect.mat", "up0Rect");
222
//cvtools::writeMat(up1Rect, "up1Rect.mat", "up1Rect");
222
//cvtools::writeMat(up1Rect, "up1Rect.mat", "up1Rect");
223
 
223
 
224
    // color debayer and remap
224
    // color debayer and remap
225
    cv::Mat color0Rect, color1Rect;
225
    cv::Mat color0Rect, color1Rect;
226
//    frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
226
//    frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
227
    cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
227
    cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
228
    cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
228
    cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
229
 
229
 
230
//    frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
230
//    frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
231
    cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
231
    cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
232
    cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
232
    cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
233
 
233
 
234
//cvtools::writeMat(frames0Rect[18], "frames0Rect_18.mat", "frames0Rect_18");
234
//cvtools::writeMat(frames0Rect[18], "frames0Rect_18.mat", "frames0Rect_18");
235
//cvtools::writeMat(frames0Rect[19], "frames0Rect_19.mat", "frames0Rect_19");
235
//cvtools::writeMat(frames0Rect[19], "frames0Rect_19.mat", "frames0Rect_19");
236
 
236
 
237
//cvtools::writeMat(color0Rect, "color0Rect.mat", "color0Rect");
237
//cvtools::writeMat(color0Rect, "color0Rect.mat", "color0Rect");
238
//cvtools::writeMat(color1Rect, "color1Rect.mat", "color1Rect");
238
//cvtools::writeMat(color1Rect, "color1Rect.mat", "color1Rect");
239
 
239
 
240
    // On/off remaps
240
    // On/off remaps
241
    cv::Mat frames0OnRect, frames0OffRect;
241
    cv::Mat frames0OnRect, frames0OffRect;
242
    cv::remap(frames0Gray[0], frames0OnRect, map0X, map0Y, CV_INTER_LINEAR);
242
    cv::remap(frames0Gray[0], frames0OnRect, map0X, map0Y, CV_INTER_LINEAR);
243
    cv::remap(frames0Gray[1], frames0OffRect, map0X, map0Y, CV_INTER_LINEAR);
243
    cv::remap(frames0Gray[1], frames0OffRect, map0X, map0Y, CV_INTER_LINEAR);
244
 
244
 
245
    cv::Mat frames1OnRect, frames1OffRect;
245
    cv::Mat frames1OnRect, frames1OffRect;
246
    cv::remap(frames1Gray[0], frames1OnRect, map1X, map1Y, CV_INTER_LINEAR);
246
    cv::remap(frames1Gray[0], frames1OnRect, map1X, map1Y, CV_INTER_LINEAR);
247
    cv::remap(frames1Gray[1], frames1OffRect, map1X, map1Y, CV_INTER_LINEAR);
247
    cv::remap(frames1Gray[1], frames1OffRect, map1X, map1Y, CV_INTER_LINEAR);
248
 
248
 
249
    // Occlusion masks
249
    // Occlusion masks
250
    cv::Mat occlusion0Rect, occlusion1Rect;
250
    cv::Mat occlusion0Rect, occlusion1Rect;
251
    cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
251
    cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
252
    occlusion0Rect = (occlusion0Rect > 25) & (occlusion0Rect < 250);
252
    occlusion0Rect = (occlusion0Rect > 25) & (occlusion0Rect < 250);
253
    cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
253
    cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
254
    occlusion1Rect = (occlusion1Rect > 25) & (occlusion1Rect < 250);
254
    occlusion1Rect = (occlusion1Rect > 25) & (occlusion1Rect < 250);
255
 
255
 
256
    // Threshold on energy at primary frequency
256
    // Threshold on energy at primary frequency
257
    occlusion0Rect = occlusion0Rect & (amplitude0Rect > 5.0*nStepsPrimary);
257
    occlusion0Rect = occlusion0Rect & (amplitude0Rect > 5.0*nStepsPrimary);
258
    occlusion1Rect = occlusion1Rect & (amplitude1Rect > 5.0*nStepsPrimary);
258
    occlusion1Rect = occlusion1Rect & (amplitude1Rect > 5.0*nStepsPrimary);
259
 
259
 
260
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
260
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
261
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
261
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
262
 
262
 
263
    // Erode occlusion masks
263
    // Erode occlusion masks
264
    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));
265
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
265
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
266
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
266
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
267
 
267
 
268
    // Threshold on gradient of phase
268
    // Threshold on gradient of phase
269
    cv::Mat edges0;
269
    cv::Mat edges0;
270
    cv::Sobel(up0Rect, edges0, -1, 1, 1, 5);
270
    cv::Sobel(up0Rect, edges0, -1, 1, 1, 5);
271
    occlusion0Rect = occlusion0Rect & (abs(edges0) < 150);
271
    occlusion0Rect = occlusion0Rect & (abs(edges0) < 150);
272
 
272
 
273
    cv::Mat edges1;
273
    cv::Mat edges1;
274
    cv::Sobel(up1Rect, edges1, -1, 1, 1, 5);
274
    cv::Sobel(up1Rect, edges1, -1, 1, 1, 5);
275
    occlusion1Rect = occlusion1Rect & (abs(edges1) < 150);
275
    occlusion1Rect = occlusion1Rect & (abs(edges1) < 150);
276
 
276
 
277
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
277
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
278
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
278
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
279
 
279
 
280
    // Match phase maps
280
    // Match phase maps
281
    int frameRectRows = map0X.rows;
281
    int frameRectRows = map0X.rows;
282
    int frameRectCols = map0X.cols;
282
    int frameRectCols = map0X.cols;
283
 
283
 
284
    // camera0 against camera1
284
    // camera0 against camera1
285
    std::vector<cv::Vec2f> q0Rect, q1Rect;
285
    std::vector<cv::Vec2f> q0Rect, q1Rect;
286
    for(int row=0; row<frameRectRows; row++){
286
    for(int row=0; row<frameRectRows; row++){
287
        for(int col=0; col<frameRectCols; col++){
287
        for(int col=0; col<frameRectCols; col++){
288
 
288
 
289
            if(!occlusion0Rect.at<char>(row,col))
289
            if(!occlusion0Rect.at<char>(row,col))
290
                continue;
290
                continue;
291
 
291
 
292
            float up0i = up0Rect.at<float>(row,col);
292
            float up0i = up0Rect.at<float>(row,col);
293
            for(int col1=0; col1<up1Rect.cols-1; col1++){
293
            for(int col1=0; col1<up1Rect.cols-1; col1++){
294
 
294
 
295
                if(!occlusion1Rect.at<char>(row,col1) || !occlusion1Rect.at<char>(row,col1+1))
295
                if(!occlusion1Rect.at<char>(row,col1) || !occlusion1Rect.at<char>(row,col1+1))
296
                    continue;
296
                    continue;
297
 
297
 
298
                float up1Left = up1Rect.at<float>(row,col1);
298
                float up1Left = up1Rect.at<float>(row,col1);
299
                float up1Right = up1Rect.at<float>(row,col1+1);
299
                float up1Right = up1Rect.at<float>(row,col1+1);
300
 
300
 
301
                if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 1) && (up1Right-up0i < 1)){
301
                if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 1) && (up1Right-up0i < 1)){
302
 
302
 
303
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
303
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
304
 
304
 
305
                    q0Rect.push_back(cv::Point2f(col, row));
305
                    q0Rect.push_back(cv::Point2f(col, row));
306
                    q1Rect.push_back(cv::Point2f(col1i, row));
306
                    q1Rect.push_back(cv::Point2f(col1i, row));
307
 
307
 
308
                    break;
308
                    break;
309
                }
309
                }
310
            }
310
            }
311
        }
311
        }
312
    }
312
    }
313
 
313
 
314
//    // camera1 against camera0
314
//    // camera1 against camera0
315
//    for(int row=0; row<frameRectRows; row++){
315
//    for(int row=0; row<frameRectRows; row++){
316
//        for(int col=0; col<frameRectCols; col++){
316
//        for(int col=0; col<frameRectCols; col++){
317
 
317
 
318
//            if(!occlusion1Rect.at<char>(row,col))
318
//            if(!occlusion1Rect.at<char>(row,col))
319
//                continue;
319
//                continue;
320
 
320
 
321
//            float up1i = up1Rect.at<float>(row,col);
321
//            float up1i = up1Rect.at<float>(row,col);
322
//            for(int col0=0; col0<up0Rect.cols-1; col0++){
322
//            for(int col0=0; col0<up0Rect.cols-1; col0++){
323
 
323
 
324
//                if(!occlusion0Rect.at<char>(row,col0) || !occlusion0Rect.at<char>(row,col0+1))
324
//                if(!occlusion0Rect.at<char>(row,col0) || !occlusion0Rect.at<char>(row,col0+1))
325
//                    continue;
325
//                    continue;
326
 
326
 
327
//                float up0Left = up0Rect.at<float>(row,col0);
327
//                float up0Left = up0Rect.at<float>(row,col0);
328
//                float up0Right = up0Rect.at<float>(row,col0+1);
328
//                float up0Right = up0Rect.at<float>(row,col0+1);
329
 
329
 
330
//                if((up0Left <= up1i) && (up1i <= up0Right) && (up1i-up0Left < 1) && (up0Right-up1i < 1)){
330
//                if((up0Left <= up1i) && (up1i <= up0Right) && (up1i-up0Left < 1) && (up0Right-up1i < 1)){
331
 
331
 
332
//                    float col0i = col0 + (up1i-up0Left)/(up0Right-up0Left);
332
//                    float col0i = col0 + (up1i-up0Left)/(up0Right-up0Left);
333
 
333
 
334
//                    q1Rect.push_back(cv::Point2f(col, row));
334
//                    q1Rect.push_back(cv::Point2f(col, row));
335
//                    q0Rect.push_back(cv::Point2f(col0i, row));
335
//                    q0Rect.push_back(cv::Point2f(col0i, row));
336
 
336
 
337
//                    break;
337
//                    break;
338
//                }
338
//                }
339
//            }
339
//            }
340
//        }
340
//        }
341
//    }
341
//    }
342
 
342
 
343
    int nMatches = q0Rect.size();
343
    int nMatches = q0Rect.size();
344
 
344
 
345
    if(nMatches < 1){
345
    if(nMatches < 1){
346
        Q.resize(0);
346
        Q.resize(0);
347
        color.resize(0);
347
        color.resize(0);
348
 
348
 
349
        return;
349
        return;
350
    }
350
    }
351
 
351
 
352
    // Retrieve color information
352
    // Retrieve color information
353
    color.resize(nMatches);
353
    color.resize(nMatches);
354
    for(int i=0; i<nMatches; i++){
354
    for(int i=0; i<nMatches; i++){
355
 
355
 
356
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
356
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
357
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
357
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
358
 
358
 
359
        color[i] = 0.5*c0 + 0.5*c1;
359
        color[i] = 0.5*c0 + 0.5*c1;
360
    }
360
    }
361
 
361
 
362
    // Triangulate points
362
    // Triangulate points
363
    cv::Mat QMatHomogenous, QMat;
363
    cv::Mat QMatHomogenous, QMat;
364
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
364
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
365
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
365
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
366
 
366
 
367
    // Undo rectification
367
    // Undo rectification
368
    cv::Mat R0Inv;
368
    cv::Mat R0Inv;
369
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
369
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
370
    QMat = R0Inv*QMat;
370
    QMat = R0Inv*QMat;
371
 
371
 
372
    cvtools::matToPoints3f(QMat, Q);
372
    cvtools::matToPoints3f(QMat, Q);
373
 
373
 
374
}
374
}
375
 
375