Subversion Repositories seema-scanner

Rev

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

Rev 192 Rev 193
1
//
1
//
2
// Embedded Phase Shifting
2
// Embedded Phase Shifting
3
//
3
//
4
// This implementation follows "Moreno, Son, Taubin: Embedded Phase Shifting: Robust Phase Shifting with Embedded Signals, CVPR 2015"
4
// This implementation follows "Moreno, Son, Taubin: Embedded Phase Shifting: Robust Phase Shifting with Embedded Signals, CVPR 2015"
5
//
5
//
6
//
6
//
7
 
7
 
8
#include "AlgorithmPhaseShiftEmbedded.h"
8
#include "AlgorithmPhaseShiftEmbedded.h"
9
#include <math.h>
9
#include <math.h>
10
 
10
 
11
#include "cvtools.h"
11
#include "cvtools.h"
12
#include "algorithmtools.h"
12
#include "algorithmtools.h"
13
 
13
 
14
// Number of frequencies
14
// Number of frequencies
15
static const int M = 3;
15
static const int M = 4;
16
 
16
 
17
// Embedded frequencies
17
// Embedded periods (product of these must be greater than screenCols)
18
static const float Fm[M] = {1.0/16, 1.0/128, 1.0/1024};
18
static const float Tm[M] = {16, 8, 8, 8};
19
 
19
 
20
// Number of patterns at each frequency
20
// Number of patterns at each frequency
21
static const int Nm[M] = {3, 3, 2};
21
static const int Nm[M] = {3, 3, 3, 3};
22
 
22
 
23
 
23
 
24
AlgorithmPhaseShiftEmbedded::AlgorithmPhaseShiftEmbedded(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
24
AlgorithmPhaseShiftEmbedded::AlgorithmPhaseShiftEmbedded(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
25
 
25
 
26
    // Set N
26
    // Set N
27
    N = 2;
27
    N = 2;
28
    for(int m=0; m<M; m++)
28
    for(int m=0; m<M; m++)
29
        N += Nm[m];
29
        N += Nm[m];
30
 
30
 
31
    // all on pattern
31
    // all on pattern
32
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
32
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
33
    patterns.push_back(allOn);
33
    patterns.push_back(allOn);
34
 
34
 
35
    // all off pattern
35
    // all off pattern
36
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
36
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
37
    patterns.push_back(allOff);
37
    patterns.push_back(allOff);
38
 
38
 
39
    // Precompute encoded patterns
39
    // Precompute encoded patterns
40
    const float pi = M_PI;
40
    const float pi = M_PI;
41
 
41
 
-
 
42
    // Compute embedded frequencies
-
 
43
    float Fm[M];
-
 
44
    for(int m=0; m<M; m++){
-
 
45
        Fm[m] = 1.0;
-
 
46
        for(int i=0; i<=m; i++)
-
 
47
            Fm[m] *= 1.0/Tm[i];
-
 
48
    }
-
 
49
 
-
 
50
    // Compute pattern frequencies
-
 
51
    float fm[M];
-
 
52
    for(int m=0; m<M; m++)
-
 
53
        fm[m] = Fm[0];
-
 
54
    for(int m=1; m<M; m++)
-
 
55
        fm[m] += Fm[m];
-
 
56
 
-
 
57
    for(int m=0; m<M; m++)
-
 
58
        std::cout << fm[m] << std::endl;
-
 
59
 
42
    // Encoding patterns
60
    // Encoding patterns
43
    for(int m=0; m<M; m++){
61
    for(int m=0; m<M; m++){
44
        int nSteps = Nm[m];
62
        int nSteps = Nm[m];
45
        float frequency = Fm[m];
63
        float frequency = fm[m];
46
        for(unsigned int i=0; i<nSteps; i++){
64
        for(unsigned int i=0; i<nSteps; i++){
47
            float phase = 2.0*pi/nSteps * i;
65
            float phase = 2.0*pi/std::max(nSteps, 3) * i;
48
            float pitch = 1.0/frequency;
66
            float pitch = 1.0/frequency;
49
            cv::Mat patternI(1,1,CV_8U);
67
            cv::Mat patternI(1,1,CV_8U);
50
            patternI = computePhaseVector(screenCols, phase, pitch);
68
            patternI = computePhaseVector(screenCols, phase, pitch);
51
            patterns.push_back(patternI.t());
69
            patterns.push_back(patternI.t());
52
        }
70
        }
53
    }
71
    }
54
 
72
 
55
}
73
}
56
 
74
 
57
cv::Mat AlgorithmPhaseShiftEmbedded::getEncodingPattern(unsigned int depth){
75
cv::Mat AlgorithmPhaseShiftEmbedded::getEncodingPattern(unsigned int depth){
58
    return patterns[depth];
76
    return patterns[depth];
59
}
77
}
60
 
78
 
61
static void decodeEmbeddedPS(const std::vector<cv::Mat> &frames, cv::Mat &up, cv::Mat &upRange){
79
static void decodeEmbeddedPS(const std::vector<cv::Mat> &frames, cv::Mat &up, cv::Mat &upRange, float screenCols){
62
 
80
 
63
    const int N = frames.size();
81
    const int N = frames.size();
64
 
82
 
65
    // Construct shift matrix
83
    // Construct shift matrix
66
    cv::Mat A(N, 1 + 2*N, CV_32F);
84
    cv::Mat A(N, 1 + 2*M, CV_32F);
-
 
85
    A.col(0).setTo(1.0);
67
 
86
 
68
    int rowBegin = 0;
87
    int rowBegin = 0;
69
    for(int m=0; m<M; m++){
88
    for(int m=0; m<M; m++){
70
 
89
 
71
        int nSteps = Nm[m];
90
        int nSteps = Nm[m];
72
 
91
 
73
        cv::Mat Am(nSteps, 2, CV_32F);
92
        cv::Mat Am(nSteps, 2, CV_32F);
74
 
93
 
75
        for(unsigned int i=0; i<nSteps; i++){
94
        for(unsigned int i=0; i<nSteps; i++){
76
            float phase = 2.0*CV_PI/nSteps * i;
95
            float phase = 2.0*CV_PI/std::max(nSteps, 3) * i;
77
 
96
 
78
            Am.at<float>(i, 0) = std::cos(phase);
97
            Am.at<float>(i, 0) = std::cos(phase);
79
            Am.at<float>(i, 1) = -std::sin(phase);
98
            Am.at<float>(i, 1) = -std::sin(phase);
80
        }
99
        }
81
 
100
 
82
        // Copy into the A matrix
101
        // Copy into the A matrix
83
        Am.copyTo(A.rowRange(rowBegin, rowBegin+nSteps).colRange(1+2*m, 1+2*(m+1)));
102
        Am.copyTo(A.rowRange(rowBegin, rowBegin+nSteps).colRange(1+2*m, 1+2*(m+1)));
84
        rowBegin += nSteps;
103
        rowBegin += nSteps;
85
    }
104
    }
-
 
105
    //std::cout << A << std::endl << std::endl;
86
 
106
 
87
    int frameRows = frames[0].rows;
107
    int frameRows = frames[0].rows;
88
    int frameCols = frames[0].cols;
108
    int frameCols = frames[0].cols;
89
 
109
 
90
    // DC-offset
110
    // DC-offset
91
    cv::Mat O(frameRows, frameCols, CV_32F);
111
    cv::Mat O(frameRows, frameCols, CV_32F);
92
 
112
 
93
    // Relative phase maps
113
    // Relative phase maps
94
    std::vector<cv::Mat> phim;
114
    std::vector<cv::Mat> phim;
95
    for(int i=0; i<N; i++)
115
    for(int i=0; i<N; i++)
96
        phim.push_back(cv::Mat(frameRows, frameCols, CV_32F));
116
        phim.push_back(cv::Mat(frameRows, frameCols, CV_32F));
97
 
117
 
98
    // Solve for relative phase values
118
    // Solve for relative phase values
99
    for(int row=0; row<frameRows; row++){
119
    for(int row=0; row<frameRows; row++){
100
        for(int col=0; col<frameCols; col++){
120
        for(int col=0; col<frameCols; col++){
101
 
121
 
102
            // Measurement vector
122
            // Measurement vector
103
            cv::Mat r(N, 1, CV_32F);
123
            cv::Mat r(N, 1, CV_32F);
104
            for(int i=0; i<N; i++)
124
            for(int i=0; i<N; i++)
105
                r.at<float>(i) = frames[i].at<float>(row, col);
125
                r.at<float>(i) = frames[i].at<uchar>(row, col);
106
 
126
 
107
            // Solve
127
            // Solve
108
            cv::Mat u; //[o, a cos1, a sin1, a cos2, a sin2, ...]
128
            cv::Mat u; //[o, a cos1, a sin1, a cos2, a sin2, ...]
109
            cv::solve(A, r, u);
129
            cv::solve(A, r, u, cv::DECOMP_SVD);
110
 
130
 
111
            for(int m=0; m<M; m++)
131
            for(int m=0; m<M; m++)
112
                phim[m].at<float>(row, col) = std::atan2(u.at<float>(m*2+1), u.at<float>(m*2+2));
132
                phim[m].at<float>(row, col) = std::atan2(u.at<float>(m*2+1), u.at<float>(m*2+2));
113
 
133
 
114
            O.at<float>(row, col) = u.at<float>(0);
134
            O.at<float>(row, col) = u.at<float>(0);
115
        }
135
        }
116
 
136
 
117
    }
137
    }
118
 
138
 
-
 
139
    #if 0
-
 
140
        for(int i=0; i<N; i++)
-
 
141
            cvtools::writeMat(frames[i], QString("frames_%1.mat").arg(i).toStdString().c_str());
-
 
142
        cvtools::writeMat(O, "O.mat");
-
 
143
        for(int m=0; m<M; m++)
-
 
144
            cvtools::writeMat(phim[m], QString("phim_%1.mat").arg(m).toStdString().c_str());
-
 
145
    #endif
-
 
146
 
119
    // Determine phase cues
147
    // Determine phase cue sequence
120
    std::vector<cv::Mat> Phim(M);
148
    std::vector<cv::Mat> Phim(M);
121
    Phim[0] = phim[0];
149
    Phim[0] = phim[0];
122
 
-
 
123
    for(int i=1; i<M; i++)
150
    for(int m=1; m<M; m++){
124
        cv::subtract(phim[i], phim[0], Phim[i]);
151
        cv::subtract(phim[m], phim[0], Phim[m]);
-
 
152
        Phim[m] = cvtools::modulo(Phim[m], 2.0*CV_PI);
-
 
153
    }
125
 
154
 
126
    // Note: Phim[1] is the cue of highest quality
155
    // Note: Phim[1] is the cue of highest quality
127
 
156
 
-
 
157
    #if 0
128
    std::vector<cv::Mat> upm;
158
        for(int m=0; m<M; m++)
-
 
159
            cvtools::writeMat(Phim[m], QString("Phim_%1.mat").arg(m).toStdString().c_str());
-
 
160
    #endif
129
 
161
 
-
 
162
    // Compute embedded frequencies
-
 
163
    float Fm[M];
130
    for(int m=0; m<M; m++)
164
    for(int m=0; m<M; m++){
-
 
165
        Fm[m] = 1.0;
-
 
166
        for(int i=0; i<=m; i++)
131
        upm[m] = unwrapWithCue(phim[m], Phim[1], 1.0/Fm[1]);
167
            Fm[m] *= 1.0/Tm[i];
-
 
168
    }
132
 
169
 
-
 
170
    // Unwrap phase cue sequence
-
 
171
    cv::Mat upCue = Phim[M-1];
-
 
172
    for(int m=M-2; m>0; m--){
-
 
173
        upCue = unwrapWithCue(Phim[m], upCue, screenCols*Fm[m]);
-
 
174
        #if 1
-
 
175
                cvtools::writeMat(upCue, "upCue.mat", "upCue");
-
 
176
        #endif
-
 
177
    }
-
 
178
 
-
 
179
    // Unwrap high frequency patterns
-
 
180
    std::vector<cv::Mat> upm(M);
-
 
181
    for(int m=0; m<M; m++){
-
 
182
        upm[m] = unwrapWithCue(phim[m], upCue, 1.0/Fm[1]);
-
 
183
    }
-
 
184
 
-
 
185
    #if 1
-
 
186
        for(int m=0; m<M; m++)
-
 
187
            cvtools::writeMat(upm[m], QString("upm_%1.mat").arg(m).toStdString().c_str());
-
 
188
    #endif
133
 
189
 
134
    // Determine range of phases (for outlier detection)
190
    // Determine range of phases (for outlier detection)
135
    cv::Mat upMin = upm[0];
191
    cv::Mat upMin = upm[0];
136
    cv::Mat upMax = upm[0];
192
    cv::Mat upMax = upm[0];
137
    for(int m=1; m<M; m++){
193
    for(int m=1; m<M; m++){
138
        upMin = cv::min(upMin, upm[m]);
194
        upMin = cv::min(upMin, upm[m]);
139
        upMax = cv::max(upMax, upm[m]);
195
        upMax = cv::max(upMax, upm[m]);
140
    }
196
    }
141
    upRange = upMax-upMin;
197
    upRange = upMax-upMin;
142
 
198
 
143
    // Return average of phase maps
199
    // Return average of phase maps
144
    up = upm[0];
200
    up = upm[0];
145
    for(int m=1; m<M; m++)
201
    for(int m=1; m<M; m++)
146
        up += upm[m];
202
        up += upm[m];
147
    up /= M;
203
    up /= M;
148
}
204
}
149
 
205
 
150
void AlgorithmPhaseShiftEmbedded::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){
206
void AlgorithmPhaseShiftEmbedded::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){
151
 
207
 
152
    assert(frames0.size() == N);
208
    assert(frames0.size() == N);
153
    assert(frames1.size() == N);
209
    assert(frames1.size() == N);
154
 
210
 
155
    int frameRows = frames0[0].rows;
211
    int frameRows = frames0[0].rows;
156
    int frameCols = frames0[0].cols;
212
    int frameCols = frames0[0].cols;
157
 
213
 
158
    // Rectifying homographies (rotation+projections)
214
    // Rectifying homographies (rotation+projections)
159
    cv::Size frameSize(frameCols, frameRows);
215
    cv::Size frameSize(frameCols, frameRows);
160
    cv::Mat R, T;
216
    cv::Mat R, T;
161
    // stereoRectify segfaults unless R is double precision
217
    // stereoRectify segfaults unless R is double precision
162
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
218
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
163
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
219
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
164
    cv::Mat R0, R1, P0, P1, QRect;
220
    cv::Mat R0, R1, P0, P1, QRect;
165
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
221
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
166
 
222
 
167
    // Interpolation maps (lens distortion and rectification)
223
    // Interpolation maps (lens distortion and rectification)
168
    cv::Mat map0X, map0Y, map1X, map1Y;
224
    cv::Mat map0X, map0Y, map1X, map1Y;
169
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
225
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
170
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
226
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
171
 
227
 
172
    int frameRectRows = map0X.rows;
228
    int frameRectRows = map0X.rows;
173
    int frameRectCols = map0X.cols;
229
    int frameRectCols = map0X.cols;
174
 
230
 
175
    // Gray-scale and remap
231
    // Gray-scale and remap
176
    std::vector<cv::Mat> frames0Rect(N);
232
    std::vector<cv::Mat> frames0Rect(N);
177
    std::vector<cv::Mat> frames1Rect(N);
233
    std::vector<cv::Mat> frames1Rect(N);
178
    for(unsigned int i=0; i<N; i++){
234
    for(unsigned int i=0; i<N; i++){
179
        cv::Mat temp;
235
        cv::Mat temp;
180
        cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
236
        cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
181
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
237
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
182
        cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
238
        cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
183
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
239
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
184
    }
240
    }
185
 
241
 
186
    // Decode camera 0
242
    // Decode camera 0
187
    std::vector<cv::Mat> frames0Patterns(frames0Rect.begin()+2, frames0Rect.end());
243
    std::vector<cv::Mat> frames0Patterns(frames0Rect.begin()+2, frames0Rect.end());
188
 
244
 
189
    cv::Mat up0, up0Range;
245
    cv::Mat up0, up0Range;
190
    decodeEmbeddedPS(frames0Patterns, up0, up0Range);
246
    decodeEmbeddedPS(frames0Patterns, up0, up0Range, screenCols);
191
    up0 *= screenCols;
247
    up0 *= screenCols;
192
 
248
 
193
    #ifdef QT_DEBUG
249
    #ifdef QT_DEBUG
194
        cvtools::writeMat(up0, "up0.mat", "up0");
250
        cvtools::writeMat(up0, "up0.mat", "up0");
195
        cvtools::writeMat(up0Range, "up0Range.mat", "up0Range");
251
        cvtools::writeMat(up0Range, "up0Range.mat", "up0Range");
196
    #endif
252
    #endif
197
 
253
 
198
    // Decode camera 1
254
    // Decode camera 1
199
    std::vector<cv::Mat> frames1Patterns(frames1Rect.begin()+2, frames1Rect.end());
255
    std::vector<cv::Mat> frames1Patterns(frames1Rect.begin()+2, frames1Rect.end());
200
 
256
 
201
    cv::Mat up1, up1Range;
257
    cv::Mat up1, up1Range;
202
    decodeEmbeddedPS(frames1Patterns, up1, up1Range);
258
    decodeEmbeddedPS(frames1Patterns, up1, up1Range, screenCols);
203
    up1 *= screenCols;
259
    up1 *= screenCols;
204
 
260
 
205
    #ifdef QT_DEBUG
261
    #ifdef QT_DEBUG
206
        cvtools::writeMat(up1, "up1.mat", "up1");
262
        cvtools::writeMat(up1, "up1.mat", "up1");
207
    #endif
263
    #endif
208
 
264
 
209
    // Color debayer and remap
265
    // Color debayer and remap
210
    cv::Mat color0, color1;
266
    cv::Mat color0, color1;
211
    cv::cvtColor(frames0[0], color0, CV_BayerBG2RGB);
267
    cv::cvtColor(frames0[0], color0, CV_BayerBG2RGB);
212
    cv::remap(color0, color0, map0X, map0Y, CV_INTER_LINEAR);
268
    cv::remap(color0, color0, map0X, map0Y, CV_INTER_LINEAR);
213
 
269
 
214
    cv::cvtColor(frames1[0], color1, CV_BayerBG2RGB);
270
    cv::cvtColor(frames1[0], color1, CV_BayerBG2RGB);
215
    cv::remap(color1, color1, map1X, map1Y, CV_INTER_LINEAR);
271
    cv::remap(color1, color1, map1X, map1Y, CV_INTER_LINEAR);
216
 
272
 
217
    #ifdef QT_DEBUG
273
    #ifdef QT_DEBUG
218
        cvtools::writeMat(color0, "color0.mat", "color0");
274
        cvtools::writeMat(color0, "color0.mat", "color0");
219
        cvtools::writeMat(color1, "color1.mat", "color1");
275
        cvtools::writeMat(color1, "color1.mat", "color1");
220
    #endif
276
    #endif
221
 
277
 
222
    // Occlusion masks
278
    // Occlusion masks
223
    cv::Mat occlusion0, occlusion1;
279
    cv::Mat occlusion0, occlusion1;
224
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0);
280
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0);
225
    occlusion0 = (occlusion0 > 25) & (occlusion0 < 250);
281
    occlusion0 = (occlusion0 > 25) & (occlusion0 < 250);
226
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1);
282
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1);
227
    occlusion1 = (occlusion1 > 25) & (occlusion1 < 250);
283
    occlusion1 = (occlusion1 > 25) & (occlusion1 < 250);
228
 
284
 
229
//    // Threshold on energy at primary frequency
285
//    // Threshold on energy at primary frequency
230
//    occlusion0 = occlusion0 & (amplitude0 > 5.0*nStepsPrimary);
286
//    occlusion0 = occlusion0 & (amplitude0 > 5.0*nStepsPrimary);
231
//    occlusion1 = occlusion1 & (amplitude1 > 5.0*nStepsPrimary);
287
//    occlusion1 = occlusion1 & (amplitude1 > 5.0*nStepsPrimary);
232
 
288
 
233
//    // Erode occlusion masks
289
//    // Erode occlusion masks
234
//    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
290
//    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
235
//    cv::erode(occlusion0, occlusion0, strel);
291
//    cv::erode(occlusion0, occlusion0, strel);
236
//    cv::erode(occlusion1, occlusion1, strel);
292
//    cv::erode(occlusion1, occlusion1, strel);
237
 
293
 
238
    // Threshold on gradient of phase
294
    // Threshold on gradient of phase
239
    cv::Mat edges0;
295
    cv::Mat edges0;
240
    cv::Sobel(up0, edges0, -1, 1, 1, 5);
296
    cv::Sobel(up0, edges0, -1, 1, 1, 5);
241
    occlusion0 = occlusion0 & (abs(edges0) < 150);
297
    occlusion0 = occlusion0 & (abs(edges0) < 150);
242
 
298
 
243
    cv::Mat edges1;
299
    cv::Mat edges1;
244
    cv::Sobel(up1, edges1, -1, 1, 1, 5);
300
    cv::Sobel(up1, edges1, -1, 1, 1, 5);
245
    occlusion1 = occlusion1 & (abs(edges1) < 150);
301
    occlusion1 = occlusion1 & (abs(edges1) < 150);
246
 
302
 
247
    #ifdef QT_DEBUG
303
    #ifdef QT_DEBUG
248
        cvtools::writeMat(occlusion0, "occlusion0.mat", "occlusion0");
304
        cvtools::writeMat(occlusion0, "occlusion0.mat", "occlusion0");
249
        cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
305
        cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
250
    #endif
306
    #endif
251
 
307
 
252
    // Match phase maps
308
    // Match phase maps
253
 
309
 
254
    // camera0 against camera1
310
    // camera0 against camera1
255
    std::vector<cv::Vec2f> q0, q1;
311
    std::vector<cv::Vec2f> q0, q1;
256
    for(int row=0; row<frameRectRows; row++){
312
    for(int row=0; row<frameRectRows; row++){
257
        for(int col=0; col<frameRectCols; col++){
313
        for(int col=0; col<frameRectCols; col++){
258
 
314
 
259
            if(!occlusion0.at<char>(row,col))
315
            if(!occlusion0.at<char>(row,col))
260
                continue;
316
                continue;
261
 
317
 
262
            float up0i = up0.at<float>(row,col);
318
            float up0i = up0.at<float>(row,col);
263
            for(int col1=0; col1<up1.cols-1; col1++){
319
            for(int col1=0; col1<up1.cols-1; col1++){
264
 
320
 
265
                if(!occlusion1.at<char>(row,col1) || !occlusion1.at<char>(row,col1+1))
321
                if(!occlusion1.at<char>(row,col1) || !occlusion1.at<char>(row,col1+1))
266
                    continue;
322
                    continue;
267
 
323
 
268
                float up1Left = up1.at<float>(row,col1);
324
                float up1Left = up1.at<float>(row,col1);
269
                float up1Right = up1.at<float>(row,col1+1);
325
                float up1Right = up1.at<float>(row,col1+1);
270
 
326
 
271
                if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 1.0) && (up1Right-up0i < 1.0) && (up1Right-up1Left > 0.1)){
327
                if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 1.0) && (up1Right-up0i < 1.0) && (up1Right-up1Left > 0.1)){
272
 
328
 
273
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
329
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
274
 
330
 
275
                    q0.push_back(cv::Point2f(col, row));
331
                    q0.push_back(cv::Point2f(col, row));
276
                    q1.push_back(cv::Point2f(col1i, row));
332
                    q1.push_back(cv::Point2f(col1i, row));
277
 
333
 
278
                    break;
334
                    break;
279
                }
335
                }
280
            }
336
            }
281
        }
337
        }
282
    }
338
    }
283
 
339
 
284
 
340
 
285
    int nMatches = q0.size();
341
    int nMatches = q0.size();
286
 
342
 
287
    if(nMatches < 1){
343
    if(nMatches < 1){
288
        Q.resize(0);
344
        Q.resize(0);
289
        color.resize(0);
345
        color.resize(0);
290
 
346
 
291
        return;
347
        return;
292
    }
348
    }
293
 
349
 
294
    // Retrieve color information
350
    // Retrieve color information
295
    color.resize(nMatches);
351
    color.resize(nMatches);
296
    for(int i=0; i<nMatches; i++){
352
    for(int i=0; i<nMatches; i++){
297
 
353
 
298
        cv::Vec3b c0 = color0.at<cv::Vec3b>(q0[i][1], q0[i][0]);
354
        cv::Vec3b c0 = color0.at<cv::Vec3b>(q0[i][1], q0[i][0]);
299
        cv::Vec3b c1 = color1.at<cv::Vec3b>(q1[i][1], q1[i][0]);
355
        cv::Vec3b c1 = color1.at<cv::Vec3b>(q1[i][1], q1[i][0]);
300
 
356
 
301
        color[i] = 0.5*c0 + 0.5*c1;
357
        color[i] = 0.5*c0 + 0.5*c1;
302
    }
358
    }
303
 
359
 
304
    // Triangulate points
360
    // Triangulate points
305
    cv::Mat QMatHomogenous, QMat;
361
    cv::Mat QMatHomogenous, QMat;
306
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
362
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
307
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
363
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
308
 
364
 
309
    // Undo rectification
365
    // Undo rectification
310
    cv::Mat R0Inv;
366
    cv::Mat R0Inv;
311
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
367
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
312
    QMat = R0Inv*QMat;
368
    QMat = R0Inv*QMat;
313
 
369
 
314
    cvtools::matToPoints3f(QMat, Q);
370
    cvtools::matToPoints3f(QMat, Q);
315
 
371
 
316
}
372
}
317
 
373