Subversion Repositories seema-scanner

Rev

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

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