Subversion Repositories seema-scanner

Rev

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

Rev 179 Rev 180
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
    // Rectifying homographies (rotation+projections)
149
    // Rectifying homographies (rotation+projections)
150
    cv::Size frameSize(frameCols, frameRows);
150
    cv::Size frameSize(frameCols, frameRows);
151
    cv::Mat R, T;
151
    cv::Mat R, T;
152
    // stereoRectify segfaults unless R is double precision
152
    // stereoRectify segfaults unless R is double precision
153
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
153
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
154
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
154
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
155
    cv::Mat R0, R1, P0, P1, QRect;
155
    cv::Mat R0, R1, P0, P1, QRect;
156
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
156
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
157
 
157
 
158
    // Interpolation maps (lens distortion and rectification)
158
    // Interpolation maps (lens distortion and rectification)
159
    cv::Mat map0X, map0Y, map1X, map1Y;
159
    cv::Mat map0X, map0Y, map1X, map1Y;
160
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
160
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
161
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
161
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
162
 
162
 
163
    // Gray-scale and remap
163
    // Gray-scale and remap
164
    std::vector<cv::Mat> frames0Rect(N);
164
    std::vector<cv::Mat> frames0Rect(N);
165
    std::vector<cv::Mat> frames1Rect(N);
165
    std::vector<cv::Mat> frames1Rect(N);
166
    for(unsigned int i=0; i<N; i++){
166
    for(unsigned int i=0; i<N; i++){
167
        cv::Mat temp;
167
        cv::Mat temp;
168
        cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
168
        cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
169
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
169
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
170
        cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
170
        cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
171
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
171
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
172
    }
172
    }
173
 
173
 
174
    // Decode camera0
174
    // Decode camera0
175
    std::vector<cv::Mat> frames0Primary(frames0Rect.begin()+2, frames0Rect.begin()+2+nStepsPrimary);
175
    std::vector<cv::Mat> frames0Primary(frames0Rect.begin()+2, frames0Rect.begin()+2+nStepsPrimary);
176
    std::vector<cv::Mat> frames0Secondary(frames0Rect.begin()+2+nStepsPrimary, frames0Rect.end());
176
    std::vector<cv::Mat> frames0Secondary(frames0Rect.begin()+2+nStepsPrimary, frames0Rect.end());
177
    std::vector<cv::Mat> F0Primary = getDFTComponents(frames0Primary);
177
    std::vector<cv::Mat> F0Primary = getDFTComponents(frames0Primary);
178
    cv::Mat up0Primary;
178
    cv::Mat up0Primary;
179
    cv::phase(F0Primary[2], -F0Primary[3], up0Primary);
179
    cv::phase(F0Primary[2], -F0Primary[3], up0Primary);
180
    //cv::Mat up0Secondary = getPhase(frames0Secondary[0], frames0Secondary[1], frames0Secondary[2]);
180
    //cv::Mat up0Secondary = getPhase(frames0Secondary[0], frames0Secondary[1], frames0Secondary[2]);
181
    std::vector<cv::Mat> F0Secondary = getDFTComponents(frames0Secondary);
181
    std::vector<cv::Mat> F0Secondary = getDFTComponents(frames0Secondary);
182
    cv::Mat up0Secondary;
182
    cv::Mat up0Secondary;
183
    cv::phase(F0Secondary[2], -F0Secondary[3], up0Secondary);
183
    cv::phase(F0Secondary[2], -F0Secondary[3], up0Secondary);
184
    cv::Mat up0Equivalent = up0Primary - up0Secondary;
184
    cv::Mat up0Equivalent = up0Primary - up0Secondary;
185
    up0Equivalent = cvtools::modulo(up0Equivalent, 2.0*pi);
185
    up0Equivalent = cvtools::modulo(up0Equivalent, 2.0*pi);
186
    cv::Mat up0 = unwrapWithCue(up0Primary, up0Equivalent, (float)screenCols/periodPrimary);
186
    cv::Mat up0 = unwrapWithCue(up0Primary, up0Equivalent, (float)screenCols/periodPrimary);
187
    up0 *= screenCols/(2.0*pi);
187
    up0 *= screenCols/(2.0*pi);
188
    cv::Mat amplitude0;
188
    cv::Mat amplitude0;
189
    cv::magnitude(F0Primary[2], -F0Primary[3], amplitude0);
189
    cv::magnitude(F0Primary[2], -F0Primary[3], amplitude0);
190
 
190
 
191
    // Decode camera1
191
    // Decode camera1
192
    std::vector<cv::Mat> frames1Primary(frames1Rect.begin()+2, frames1Rect.begin()+2+nStepsPrimary);
192
    std::vector<cv::Mat> frames1Primary(frames1Rect.begin()+2, frames1Rect.begin()+2+nStepsPrimary);
193
    std::vector<cv::Mat> frames1Secondary(frames1Rect.begin()+2+nStepsPrimary, frames1Rect.end());
193
    std::vector<cv::Mat> frames1Secondary(frames1Rect.begin()+2+nStepsPrimary, frames1Rect.end());
194
    std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
194
    std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
195
    cv::Mat up1Primary;
195
    cv::Mat up1Primary;
196
    cv::phase(F1Primary[2], -F1Primary[3], up1Primary);
196
    cv::phase(F1Primary[2], -F1Primary[3], up1Primary);
197
    //cv::Mat up1Secondary = getPhase(frames1Secondary[0], frames1Secondary[1], frames1Secondary[2]);
197
    //cv::Mat up1Secondary = getPhase(frames1Secondary[0], frames1Secondary[1], frames1Secondary[2]);
198
    std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
198
    std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
199
    cv::Mat up1Secondary;
199
    cv::Mat up1Secondary;
200
    cv::phase(F1Secondary[2], -F1Secondary[3], up1Secondary);
200
    cv::phase(F1Secondary[2], -F1Secondary[3], up1Secondary);
201
    cv::Mat up1Equivalent = up1Primary - up1Secondary;
201
    cv::Mat up1Equivalent = up1Primary - up1Secondary;
202
    up1Equivalent = cvtools::modulo(up1Equivalent, 2.0*pi);
202
    up1Equivalent = cvtools::modulo(up1Equivalent, 2.0*pi);
203
    cv::Mat up1 = unwrapWithCue(up1Primary, up1Equivalent, (float)screenCols/periodPrimary);
203
    cv::Mat up1 = unwrapWithCue(up1Primary, up1Equivalent, (float)screenCols/periodPrimary);
204
    up1 *= screenCols/(2.0*pi);
204
    up1 *= screenCols/(2.0*pi);
205
    cv::Mat amplitude1;
205
    cv::Mat amplitude1;
206
    cv::magnitude(F1Primary[2], -F1Primary[3], amplitude1);
206
    cv::magnitude(F1Primary[2], -F1Primary[3], amplitude1);
207
 
207
 
208
//cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
208
cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
209
//cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
209
cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
210
//cvtools::writeMat(up0Equivalent, "up0Equivalent.mat", "up0Equivalent");
210
cvtools::writeMat(up0Equivalent, "up0Equivalent.mat", "up0Equivalent");
211
//cvtools::writeMat(up0, "up0.mat", "up0");
211
cvtools::writeMat(up0, "up0.mat", "up0");
212
//cvtools::writeMat(amplitude0, "amplitude0.mat", "amplitude0");
212
cvtools::writeMat(amplitude0, "amplitude0.mat", "amplitude0");
213
 
213
 
214
    // color debayer and remap
214
    // color debayer and remap
215
    cv::Mat color0, color1;
215
    cv::Mat color0, color1;
216
//    frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
216
//    frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
217
    cv::cvtColor(frames0[0], color0, CV_BayerBG2RGB);
217
    cv::cvtColor(frames0[0], color0, CV_BayerBG2RGB);
218
    cv::remap(color0, color0, map0X, map0Y, CV_INTER_LINEAR);
218
    cv::remap(color0, color0, map0X, map0Y, CV_INTER_LINEAR);
219
 
219
 
220
//    frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
220
//    frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
221
    cv::cvtColor(frames1[0], color1, CV_BayerBG2RGB);
221
    cv::cvtColor(frames1[0], color1, CV_BayerBG2RGB);
222
    cv::remap(color1, color1, map1X, map1Y, CV_INTER_LINEAR);
222
    cv::remap(color1, color1, map1X, map1Y, CV_INTER_LINEAR);
223
 
223
 
224
//cvtools::writeMat(color0, "color0.mat", "color0");
224
//cvtools::writeMat(color0, "color0.mat", "color0");
225
//cvtools::writeMat(color1, "color1.mat", "color1");
225
//cvtools::writeMat(color1, "color1.mat", "color1");
226
 
226
 
227
    // Occlusion masks
227
    // Occlusion masks
228
    cv::Mat occlusion0, occlusion1;
228
    cv::Mat occlusion0, occlusion1;
229
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0);
229
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0);
230
    occlusion0 = (occlusion0 > 25) & (occlusion0 < 250);
230
    occlusion0 = (occlusion0 > 25) & (occlusion0 < 250);
231
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1);
231
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1);
232
    occlusion1 = (occlusion1 > 25) & (occlusion1 < 250);
232
    occlusion1 = (occlusion1 > 25) & (occlusion1 < 250);
233
 
233
 
234
    // Threshold on energy at primary frequency
234
    // Threshold on energy at primary frequency
235
    occlusion0 = occlusion0 & (amplitude0 > 5.0*nStepsPrimary);
235
    occlusion0 = occlusion0 & (amplitude0 > 5.0*nStepsPrimary);
236
    occlusion1 = occlusion1 & (amplitude1 > 5.0*nStepsPrimary);
236
    occlusion1 = occlusion1 & (amplitude1 > 5.0*nStepsPrimary);
237
 
237
 
238
//cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
238
//cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
239
//cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
239
//cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
240
 
240
 
241
    // Erode occlusion masks
241
    // Erode occlusion masks
242
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
242
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
243
    cv::erode(occlusion0, occlusion0, strel);
243
    cv::erode(occlusion0, occlusion0, strel);
244
    cv::erode(occlusion1, occlusion1, strel);
244
    cv::erode(occlusion1, occlusion1, strel);
245
 
245
 
246
    // Threshold on gradient of phase
246
    // Threshold on gradient of phase
247
    cv::Mat edges0;
247
    cv::Mat edges0;
248
    cv::Sobel(up0, edges0, -1, 1, 1, 5);
248
    cv::Sobel(up0, edges0, -1, 1, 1, 5);
249
    occlusion0 = occlusion0 & (abs(edges0) < 150);
249
    occlusion0 = occlusion0 & (abs(edges0) < 150);
250
 
250
 
251
    cv::Mat edges1;
251
    cv::Mat edges1;
252
    cv::Sobel(up1, edges1, -1, 1, 1, 5);
252
    cv::Sobel(up1, edges1, -1, 1, 1, 5);
253
    occlusion1 = occlusion1 & (abs(edges1) < 150);
253
    occlusion1 = occlusion1 & (abs(edges1) < 150);
254
 
254
 
255
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
255
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
256
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
256
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
257
 
257
 
258
    // Match phase maps
258
    // Match phase maps
259
    int frameRectRows = map0X.rows;
259
    int frameRectRows = map0X.rows;
260
    int frameRectCols = map0X.cols;
260
    int frameRectCols = map0X.cols;
261
 
261
 
262
    // camera0 against camera1
262
    // camera0 against camera1
263
    std::vector<cv::Vec2f> q0, q1;
263
    std::vector<cv::Vec2f> q0, q1;
264
    for(int row=0; row<frameRectRows; row++){
264
    for(int row=0; row<frameRectRows; row++){
265
        for(int col=0; col<frameRectCols; col++){
265
        for(int col=0; col<frameRectCols; col++){
266
 
266
 
267
            if(!occlusion0.at<char>(row,col))
267
            if(!occlusion0.at<char>(row,col))
268
                continue;
268
                continue;
269
 
269
 
270
            float up0i = up0.at<float>(row,col);
270
            float up0i = up0.at<float>(row,col);
271
            for(int col1=0; col1<up1.cols-1; col1++){
271
            for(int col1=0; col1<up1.cols-1; col1++){
272
 
272
 
273
                if(!occlusion1.at<char>(row,col1) || !occlusion1.at<char>(row,col1+1))
273
                if(!occlusion1.at<char>(row,col1) || !occlusion1.at<char>(row,col1+1))
274
                    continue;
274
                    continue;
275
 
275
 
276
                float up1Left = up1.at<float>(row,col1);
276
                float up1Left = up1.at<float>(row,col1);
277
                float up1Right = up1.at<float>(row,col1+1);
277
                float up1Right = up1.at<float>(row,col1+1);
278
 
278
 
279
                if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 1) && (up1Right-up0i < 1)){
279
                if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 1) && (up1Right-up0i < 1)){
280
 
280
 
281
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
281
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
282
 
282
 
283
                    q0.push_back(cv::Point2f(col, row));
283
                    q0.push_back(cv::Point2f(col, row));
284
                    q1.push_back(cv::Point2f(col1i, row));
284
                    q1.push_back(cv::Point2f(col1i, row));
285
 
285
 
286
                    break;
286
                    break;
287
                }
287
                }
288
            }
288
            }
289
        }
289
        }
290
    }
290
    }
291
 
291
 
292
 
292
 
293
    int nMatches = q0.size();
293
    int nMatches = q0.size();
294
 
294
 
295
    if(nMatches < 1){
295
    if(nMatches < 1){
296
        Q.resize(0);
296
        Q.resize(0);
297
        color.resize(0);
297
        color.resize(0);
298
 
298
 
299
        return;
299
        return;
300
    }
300
    }
301
 
301
 
302
    // Retrieve color information
302
    // Retrieve color information
303
    color.resize(nMatches);
303
    color.resize(nMatches);
304
    for(int i=0; i<nMatches; i++){
304
    for(int i=0; i<nMatches; i++){
305
 
305
 
306
        cv::Vec3b c0 = color0.at<cv::Vec3b>(q0[i][1], q0[i][0]);
306
        cv::Vec3b c0 = color0.at<cv::Vec3b>(q0[i][1], q0[i][0]);
307
        cv::Vec3b c1 = color1.at<cv::Vec3b>(q1[i][1], q1[i][0]);
307
        cv::Vec3b c1 = color1.at<cv::Vec3b>(q1[i][1], q1[i][0]);
308
 
308
 
309
        color[i] = 0.5*c0 + 0.5*c1;
309
        color[i] = 0.5*c0 + 0.5*c1;
310
    }
310
    }
311
 
311
 
312
    // Triangulate points
312
    // Triangulate points
313
    cv::Mat QMatHomogenous, QMat;
313
    cv::Mat QMatHomogenous, QMat;
314
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
314
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
315
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
315
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
316
 
316
 
317
    // Undo rectification
317
    // Undo rectification
318
    cv::Mat R0Inv;
318
    cv::Mat R0Inv;
319
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
319
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
320
    QMat = R0Inv*QMat;
320
    QMat = R0Inv*QMat;
321
 
321
 
322
    cvtools::matToPoints3f(QMat, Q);
322
    cvtools::matToPoints3f(QMat, Q);
323
 
323
 
324
}
324
}
325
 
325