Subversion Repositories seema-scanner

Rev

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

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