Subversion Repositories seema-scanner

Rev

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

Rev 234 Rev 235
1
//
1
//
2
// Two Frequency Phase Shifting using the Heterodyne Principle
2
// Two Frequency Phase Shifting using the Heterodyne Principle
3
//
3
//
4
// This implementation follows "Reich, Ritter, Thesing, White light heterodyne
4
// This implementation follows "Reich, Ritter, Thesing, White light heterodyne
5
// principle for 3D-measurement", SPIE (1997).
5
// principle for 3D-measurement", SPIE (1997).
6
//
6
//
7
// Different from the paper, it uses only two different frequencies.
7
// Different from the paper, it uses only two different frequencies.
8
//
8
//
9
// The number of periods in the primary frequency can be chosen freely, but
9
// The number of periods in the primary frequency can be chosen freely, but
10
// small changes can have a considerable impact on quality. The number of
10
// small changes can have a considerable impact on quality. The number of
11
// phase shifts can be chosen freely (min. 3), and higher values reduce the
11
// phase shifts can be chosen freely (min. 3), and higher values reduce the
12
// effects of image noise.They also allow us to filter bad points based on
12
// effects of image noise.They also allow us to filter bad points based on
13
// energy at non-primary frequencies.
13
// energy at non-primary frequencies.
14
//
14
//
15
 
15
 
16
#include "AlgorithmPhaseShiftTwoFreq.h"
16
#include "AlgorithmPhaseShiftTwoFreq.h"
17
#include <math.h>
17
#include <math.h>
18
 
18
 
19
#include "cvtools.h"
19
#include "cvtools.h"
20
#include "algorithmtools.h"
20
#include "algorithmtools.h"
21
 
21
 
22
#include <omp.h>
22
#include <omp.h>
23
 
23
 
24
static unsigned int nStepsPrimary = 16; // number of shifts/steps in primary
24
static unsigned int nStepsPrimary = 16; // number of shifts/steps in primary
25
static unsigned int nStepsSecondary = 8; // number of shifts/steps in secondary
25
static unsigned int nStepsSecondary = 8; // number of shifts/steps in secondary
26
static float nPeriodsPrimary = 40; // primary period
26
static float nPeriodsPrimary = 40; // primary period
27
 
27
 
28
AlgorithmPhaseShiftTwoFreq::AlgorithmPhaseShiftTwoFreq(unsigned int _screenCols,
28
AlgorithmPhaseShiftTwoFreq::AlgorithmPhaseShiftTwoFreq(unsigned int _screenCols,
29
                                                       unsigned int _screenRows)
29
                                                       unsigned int _screenRows)
30
    : Algorithm(_screenCols, _screenRows) {
30
    : Algorithm(_screenCols, _screenRows) {
31
 
31
 
32
    // Set N
32
    // Set N
33
    N = 2+nStepsPrimary+nStepsSecondary;
33
    N = 2+nStepsPrimary+nStepsSecondary;
34
 
34
 
35
    // Determine the secondary (wider) period to fulfill the heterodyne condition
35
    // Determine the secondary (wider) period to fulfill the heterodyne condition
36
    float nPeriodsSecondary = nPeriodsPrimary + 1;
36
    float nPeriodsSecondary = nPeriodsPrimary + 1;
37
 
37
 
38
    // all on pattern
38
    // all on pattern
39
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
39
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
40
    patterns.push_back(allOn);
40
    patterns.push_back(allOn);
41
 
41
 
42
    // all off pattern
42
    // all off pattern
43
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
43
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
44
    patterns.push_back(allOff);
44
    patterns.push_back(allOff);
45
 
45
 
46
    // Primary encoding patterns
46
    // Primary encoding patterns
47
    for(unsigned int i=0; i<nStepsPrimary; i++){
47
    for(unsigned int i=0; i<nStepsPrimary; i++){
48
        float phase = 2.0*CV_PI/nStepsPrimary * i;
48
        float phase = 2.0*CV_PI/nStepsPrimary * i;
49
        float pitch = screenCols/nPeriodsPrimary;
49
        float pitch = screenCols/nPeriodsPrimary;
50
        cv::Mat patternI(1,1,CV_8U);
50
        cv::Mat patternI(1,1,CV_8U);
51
        patternI = computePhaseVector(screenCols, phase, pitch);
51
        patternI = computePhaseVector(screenCols, phase, pitch);
52
        patterns.push_back(patternI.t());
52
        patterns.push_back(patternI.t());
53
    }
53
    }
54
 
54
 
55
    // Secondary encoding patterns
55
    // Secondary encoding patterns
56
    for(unsigned int i=0; i<nStepsSecondary; i++){
56
    for(unsigned int i=0; i<nStepsSecondary; i++){
57
        float phase = 2.0*CV_PI/nStepsSecondary * i;
57
        float phase = 2.0*CV_PI/nStepsSecondary * i;
58
        float pitch = screenCols/nPeriodsSecondary;
58
        float pitch = screenCols/nPeriodsSecondary;
59
        cv::Mat patternI(1,1,CV_8U);
59
        cv::Mat patternI(1,1,CV_8U);
60
        patternI = computePhaseVector(screenCols, phase, pitch);
60
        patternI = computePhaseVector(screenCols, phase, pitch);
61
        patterns.push_back(patternI.t());
61
        patterns.push_back(patternI.t());
62
    }
62
    }
63
 
63
 
64
 
64
 
65
}
65
}
66
 
66
 
67
cv::Mat AlgorithmPhaseShiftTwoFreq::getEncodingPattern(unsigned int depth){
67
cv::Mat AlgorithmPhaseShiftTwoFreq::getEncodingPattern(unsigned int depth){
68
    return patterns[depth];
68
    return patterns[depth];
69
}
69
}
70
 
70
 
71
 
71
 
72
struct StereoRectifyier {
72
struct StereoRectifyier {
73
    cv::Mat map0X, map0Y, map1X, map1Y;
73
    cv::Mat map0X, map0Y, map1X, map1Y;
74
    cv::Mat R0, R1, P0, P1, QRect;
74
    cv::Mat R0, R1, P0, P1, QRect;
75
};
75
};
76
static void getStereoRectifyier(const SMCalibrationParameters &calibration,
76
static void getStereoRectifyier(const SMCalibrationParameters &calibration,
77
                                const cv::Size& frameSize,
77
                                const cv::Size& frameSize,
78
                                StereoRectifyier& stereoRect);
78
                                StereoRectifyier& stereoRect);
79
static void determineAmplitudePhaseEnergy(std::vector<cv::Mat>& frames,
79
static void determineAmplitudePhaseEnergy(std::vector<cv::Mat>& frames,
80
                                    cv::Mat& amplitude,
80
                                    cv::Mat& amplitude,
81
                                    cv::Mat& phase,
81
                                    cv::Mat& phase,
82
                                    cv::Mat& energy);
82
                                    cv::Mat& energy);
83
static void collectPhases(const cv::Mat& phasePrimary,
83
static void collectPhases(const cv::Mat& phasePrimary,
84
                          const cv::Mat& phaseSecondary,
84
                          const cv::Mat& phaseSecondary,
85
                          cv::Mat& phase);
85
                          cv::Mat& phase);
86
static void matchPhaseMaps(const cv::Mat& occlusion0, const cv::Mat& occlusion1,
86
static void matchPhaseMaps(const cv::Mat& occlusion0, const cv::Mat& occlusion1,
87
                           const cv::Mat& phase0, const cv::Mat& phase1,
87
                           const cv::Mat& phase0, const cv::Mat& phase1,
88
                           std::vector<cv::Vec2f>& q0, std::vector<cv::Vec2f>& q1);
88
                           std::vector<cv::Vec2f>& q0, std::vector<cv::Vec2f>& q1);
89
static void triangulate(const StereoRectifyier& stereoRect,
89
static void triangulate(const StereoRectifyier& stereoRect,
90
                        const std::vector<cv::Vec2f>& q0,
90
                        const std::vector<cv::Vec2f>& q0,
91
                        const std::vector<cv::Vec2f>& q1,
91
                        const std::vector<cv::Vec2f>& q1,
92
                        std::vector<cv::Point3f>& Q);
92
                        std::vector<cv::Point3f>& Q);
93
 
93
 
94
 
94
 
95
void AlgorithmPhaseShiftTwoFreq::
95
void AlgorithmPhaseShiftTwoFreq::
96
    get3DPoints(const SMCalibrationParameters & calibration,
96
    get3DPoints(const SMCalibrationParameters & calibration,
97
                const std::vector<cv::Mat>& frames0,
97
                const std::vector<cv::Mat>& frames0,
98
                const std::vector<cv::Mat>& frames1,
98
                const std::vector<cv::Mat>& frames1,
99
                std::vector<cv::Point3f>& Q,
99
                std::vector<cv::Point3f>& Q,
100
                std::vector<cv::Vec3b>& color){
100
                std::vector<cv::Vec3b>& color){
101
 
101
 
102
    assert(frames0.size() == N);
102
    assert(frames0.size() == N);
103
    assert(frames1.size() == N);
103
    assert(frames1.size() == N);
104
 
104
 
105
    StereoRectifyier stereoRect;
105
    StereoRectifyier stereoRect;
106
    getStereoRectifyier(calibration,
106
    getStereoRectifyier(calibration,
107
                        cv::Size(frames0[0].cols, frames0[0].rows),
107
                        cv::Size(frames0[0].cols, frames0[0].rows),
108
                        stereoRect);
108
                        stereoRect);
109
 
109
 
110
    // // Erode occlusion masks
110
    // // Erode occlusion masks
111
    // cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
111
    // cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
112
 
112
 
113
    cv::Mat up0, up1;
113
    cv::Mat up0, up1;
114
    cv::Mat occlusion0, occlusion1;
114
    cv::Mat occlusion0, occlusion1;
115
    cv::Mat color0, color1;
115
    cv::Mat color0, color1;
116
 
116
 
117
    #pragma omp parallel sections
117
    #pragma omp parallel sections
118
    {
118
    {
119
        #pragma omp section
119
        #pragma omp section
120
        {
120
        {
121
 
121
 
122
            // Gray-scale and remap/rectify
122
            // Gray-scale and remap/rectify
123
            std::vector<cv::Mat> frames0Rect(N);
123
            std::vector<cv::Mat> frames0Rect(N);
124
 
124
 
125
            for(unsigned int i=0; i<N; i++){
125
            for(unsigned int i=0; i<N; i++){
126
                cv::Mat temp;
126
                cv::Mat temp;
127
                cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
127
                cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
128
                cv::remap(temp, frames0Rect[i],
128
                cv::remap(temp, frames0Rect[i],
129
                          stereoRect.map0X, stereoRect.map0Y,
129
                          stereoRect.map0X, stereoRect.map0Y,
130
                          CV_INTER_LINEAR);
130
                          CV_INTER_LINEAR);
131
            }
131
            }
132
 
132
 
133
            cv::cvtColor(frames0[0], color0, CV_BayerBG2RGB);
133
            cv::cvtColor(frames0[0], color0, CV_BayerBG2RGB);
134
            cv::remap(color0, color0,
134
            cv::remap(color0, color0,
135
                      stereoRect.map1X, stereoRect.map1Y,
135
                      stereoRect.map0X, stereoRect.map0Y,
136
                      CV_INTER_LINEAR);
136
                      CV_INTER_LINEAR);
137
 
137
 
138
            // Occlusion masks
138
            // Occlusion masks
139
            cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0);
139
            cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0);
140
            occlusion0 = (occlusion0 > 25) & (occlusion0 < 250);
140
            occlusion0 = (occlusion0 > 25) & (occlusion0 < 250);
141
 
141
 
142
            // Decode camera0
142
            // Decode camera0
143
            std::vector<cv::Mat> frames0Primary(frames0Rect.begin()+2,
143
            std::vector<cv::Mat> frames0Primary(frames0Rect.begin()+2,
144
                                                frames0Rect.begin()+2+nStepsPrimary);
144
                                                frames0Rect.begin()+2+nStepsPrimary);
145
            std::vector<cv::Mat> frames0Secondary(frames0Rect.begin()+2+nStepsPrimary,
145
            std::vector<cv::Mat> frames0Secondary(frames0Rect.begin()+2+nStepsPrimary,
146
                                                  frames0Rect.end());
146
                                                  frames0Rect.end());
147
 
147
 
148
            frames0Rect.clear();
148
            frames0Rect.clear();
149
 
149
 
150
            cv::Mat amplitude0Primary, amplitude0Secondary;
150
            cv::Mat amplitude0Primary, amplitude0Secondary;
151
            cv::Mat up0Primary, up0Secondary;
151
            cv::Mat up0Primary, up0Secondary;
152
            cv::Mat energy0Primary, energy0Secondary;
152
            cv::Mat energy0Primary, energy0Secondary;
153
            determineAmplitudePhaseEnergy(frames0Primary,
153
            determineAmplitudePhaseEnergy(frames0Primary,
154
                                          amplitude0Primary,
154
                                          amplitude0Primary,
155
                                          up0Primary,
155
                                          up0Primary,
156
                                          energy0Primary);
156
                                          energy0Primary);
157
            determineAmplitudePhaseEnergy(frames0Secondary,
157
            determineAmplitudePhaseEnergy(frames0Secondary,
158
                                          amplitude0Secondary,
158
                                          amplitude0Secondary,
159
                                          up0Secondary,
159
                                          up0Secondary,
160
                                          energy0Secondary);
160
                                          energy0Secondary);
161
 
161
 
162
            collectPhases(up0Primary, up0Secondary, up0);
162
            collectPhases(up0Primary, up0Secondary, up0);
163
 
163
 
164
            // Threshold on energy at primary frequency
164
            // Threshold on energy at primary frequency
165
            occlusion0 = occlusion0 & (amplitude0Primary > 5.0*nStepsPrimary);
165
            occlusion0 = occlusion0 & (amplitude0Primary > 5.0*nStepsPrimary);
166
            // Threshold on energy ratios
166
            // Threshold on energy ratios
167
            occlusion0 = occlusion0 & (amplitude0Primary > 0.25*energy0Primary);
167
            occlusion0 = occlusion0 & (amplitude0Primary > 0.25*energy0Primary);
168
            occlusion0 = occlusion0 & (amplitude0Secondary > 0.25*energy0Secondary);
168
            occlusion0 = occlusion0 & (amplitude0Secondary > 0.25*energy0Secondary);
169
 
169
 
170
            // // Erode occlusion masks
170
            // // Erode occlusion masks
171
            // cv::erode(occlusion0, occlusion0, strel);
171
            // cv::erode(occlusion0, occlusion0, strel);
172
 
172
 
173
            // Threshold on gradient of phase
173
            // Threshold on gradient of phase
174
            cv::Mat edges0;
174
            cv::Mat edges0;
175
            cv::Sobel(up0, edges0, -1, 1, 1, 5);
175
            cv::Sobel(up0, edges0, -1, 1, 1, 5);
176
            occlusion0 = occlusion0 & (abs(edges0) < 10);
176
            occlusion0 = occlusion0 & (abs(edges0) < 10);
177
 
177
 
178
            #ifdef SM_DEBUG
178
            #ifdef SM_DEBUG
179
                cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
179
                cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
180
                cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
180
                cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
181
                cvtools::writeMat(up0, "up0.mat", "up0");
181
                cvtools::writeMat(up0, "up0.mat", "up0");
182
                cvtools::writeMat(amplitude0Primary,
182
                cvtools::writeMat(amplitude0Primary,
183
                                  "amplitude0Primary.mat", "amplitude0Primary");
183
                                  "amplitude0Primary.mat", "amplitude0Primary");
184
                cvtools::writeMat(amplitude0Secondary,
184
                cvtools::writeMat(amplitude0Secondary,
185
                                  "amplitude0Secondary.mat", "amplitude0Secondary");
185
                                  "amplitude0Secondary.mat", "amplitude0Secondary");
186
                cvtools::writeMat(energy0Primary,
186
                cvtools::writeMat(energy0Primary,
187
                                  "energy0Primary.mat", "energy0Primary");
187
                                  "energy0Primary.mat", "energy0Primary");
188
                cvtools::writeMat(energy0Secondary,
188
                cvtools::writeMat(energy0Secondary,
189
                                  "energy0Secondary.mat", "energy0Secondary");
189
                                  "energy0Secondary.mat", "energy0Secondary");
190
                cvtools::writeMat(edges0, "edges0.mat", "edges0");
190
                cvtools::writeMat(edges0, "edges0.mat", "edges0");
191
                cvtools::writeMat(occlusion0, "occlusion0.mat", "occlusion0");
191
                cvtools::writeMat(occlusion0, "occlusion0.mat", "occlusion0");
192
                cvtools::writeMat(color0, "color0.mat", "color0");
192
                cvtools::writeMat(color0, "color0.mat", "color0");
193
            #endif
193
            #endif
194
 
194
 
195
        }
195
        }
196
        #pragma omp section
196
        #pragma omp section
197
        {
197
        {
198
 
198
 
199
            // Gray-scale and remap
199
            // Gray-scale and remap
200
            std::vector<cv::Mat> frames1Rect(N);
200
            std::vector<cv::Mat> frames1Rect(N);
201
 
201
 
202
            for(unsigned int i=0; i<N; i++){
202
            for(unsigned int i=0; i<N; i++){
203
                cv::Mat temp;
203
                cv::Mat temp;
204
                cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
204
                cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
205
                cv::remap(temp, frames1Rect[i],
205
                cv::remap(temp, frames1Rect[i],
206
                          stereoRect.map1X, stereoRect.map1Y,
206
                          stereoRect.map1X, stereoRect.map1Y,
207
                          CV_INTER_LINEAR);
207
                          CV_INTER_LINEAR);
208
            }
208
            }
209
 
209
 
210
            cv::cvtColor(frames1[0], color1, CV_BayerBG2RGB);
210
            cv::cvtColor(frames1[0], color1, CV_BayerBG2RGB);
211
            cv::remap(color1, color1,
211
            cv::remap(color1, color1,
212
                      stereoRect.map1X, stereoRect.map1Y,
212
                      stereoRect.map1X, stereoRect.map1Y,
213
                      CV_INTER_LINEAR);
213
                      CV_INTER_LINEAR);
214
 
214
 
215
            // Occlusion masks
215
            // Occlusion masks
216
            cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1);
216
            cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1);
217
            occlusion1 = (occlusion1 > 25) & (occlusion1 < 250);
217
            occlusion1 = (occlusion1 > 25) & (occlusion1 < 250);
218
 
218
 
219
            // Decode camera1
219
            // Decode camera1
220
            std::vector<cv::Mat> frames1Primary(frames1Rect.begin()+2,
220
            std::vector<cv::Mat> frames1Primary(frames1Rect.begin()+2,
221
                                                frames1Rect.begin()+2+nStepsPrimary);
221
                                                frames1Rect.begin()+2+nStepsPrimary);
222
            std::vector<cv::Mat> frames1Secondary(frames1Rect.begin()+2+nStepsPrimary,
222
            std::vector<cv::Mat> frames1Secondary(frames1Rect.begin()+2+nStepsPrimary,
223
                                                  frames1Rect.end());
223
                                                  frames1Rect.end());
224
 
224
 
225
            frames1Rect.clear();
225
            frames1Rect.clear();
226
 
226
 
227
            cv::Mat amplitude1Primary, amplitude1Secondary;
227
            cv::Mat amplitude1Primary, amplitude1Secondary;
228
            cv::Mat up1Primary, up1Secondary;
228
            cv::Mat up1Primary, up1Secondary;
229
            cv::Mat energy1Primary, energy1Secondary;
229
            cv::Mat energy1Primary, energy1Secondary;
230
            determineAmplitudePhaseEnergy(frames1Primary,
230
            determineAmplitudePhaseEnergy(frames1Primary,
231
                                          amplitude1Primary,
231
                                          amplitude1Primary,
232
                                          up1Primary,
232
                                          up1Primary,
233
                                          energy1Primary);
233
                                          energy1Primary);
234
            determineAmplitudePhaseEnergy(frames1Secondary,
234
            determineAmplitudePhaseEnergy(frames1Secondary,
235
                                          amplitude1Secondary,
235
                                          amplitude1Secondary,
236
                                          up1Secondary,
236
                                          up1Secondary,
237
                                          energy1Secondary);
237
                                          energy1Secondary);
238
 
238
 
239
            collectPhases(up1Primary, up1Secondary, up1);
239
            collectPhases(up1Primary, up1Secondary, up1);
240
 
240
 
241
            // Threshold on energy at primary frequency
241
            // Threshold on energy at primary frequency
242
            occlusion1 = occlusion1 & (amplitude1Primary > 5.0*nStepsPrimary);
242
            occlusion1 = occlusion1 & (amplitude1Primary > 5.0*nStepsPrimary);
243
            // Threshold on energy ratios
243
            // Threshold on energy ratios
244
            occlusion1 = occlusion1 & (amplitude1Primary > 0.25*energy1Primary);
244
            occlusion1 = occlusion1 & (amplitude1Primary > 0.25*energy1Primary);
245
            occlusion1 = occlusion1 & (amplitude1Secondary > 0.25*energy1Secondary);
245
            occlusion1 = occlusion1 & (amplitude1Secondary > 0.25*energy1Secondary);
246
 
246
 
247
            // // Erode occlusion masks
247
            // // Erode occlusion masks
248
            // cv::erode(occlusion1, occlusion1, strel);
248
            // cv::erode(occlusion1, occlusion1, strel);
249
 
249
 
250
 
250
 
251
            // Threshold on gradient of phase
251
            // Threshold on gradient of phase
252
            cv::Mat edges1;
252
            cv::Mat edges1;
253
            cv::Sobel(up1, edges1, -1, 1, 1, 5);
253
            cv::Sobel(up1, edges1, -1, 1, 1, 5);
254
            occlusion1 = occlusion1 & (abs(edges1) < 10);
254
            occlusion1 = occlusion1 & (abs(edges1) < 10);
255
 
255
 
256
            #ifdef SM_DEBUG
256
            #ifdef SM_DEBUG
257
                cvtools::writeMat(up1Primary, "up1Primary.mat", "up1Primary");
257
                cvtools::writeMat(up1Primary, "up1Primary.mat", "up1Primary");
258
                cvtools::writeMat(up1Secondary, "up1Secondary.mat", "up1Secondary");
258
                cvtools::writeMat(up1Secondary, "up1Secondary.mat", "up1Secondary");
259
                cvtools::writeMat(up1, "up1.mat", "up1");
259
                cvtools::writeMat(up1, "up1.mat", "up1");
260
                cvtools::writeMat(amplitude1Primary,
260
                cvtools::writeMat(amplitude1Primary,
261
                                  "amplitude1Primary.mat", "amplitude1Primary");
261
                                  "amplitude1Primary.mat", "amplitude1Primary");
262
                cvtools::writeMat(amplitude1Secondary,
262
                cvtools::writeMat(amplitude1Secondary,
263
                                  "amplitude1Secondary.mat", "amplitude1Secondary");
263
                                  "amplitude1Secondary.mat", "amplitude1Secondary");
264
                cvtools::writeMat(energy1Primary,
264
                cvtools::writeMat(energy1Primary,
265
                                  "energy1Primary.mat", "energy1Primary");
265
                                  "energy1Primary.mat", "energy1Primary");
266
                cvtools::writeMat(energy1Secondary,
266
                cvtools::writeMat(energy1Secondary,
267
                                  "energy1Secondary.mat", "energy1Secondary");
267
                                  "energy1Secondary.mat", "energy1Secondary");
268
                cvtools::writeMat(edges1, "edges1.mat", "edges1");
268
                cvtools::writeMat(edges1, "edges1.mat", "edges1");
269
                cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
269
                cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
270
                cvtools::writeMat(color1, "color1.mat", "color1");
270
                cvtools::writeMat(color1, "color1.mat", "color1");
271
            #endif
271
            #endif
272
 
272
 
273
        }
273
        }
274
    }
274
    }
275
 
275
 
276
 
276
 
277
    // Match phase maps
277
    // Match phase maps
278
 
278
 
279
    // camera0 against camera1
279
    // camera0 against camera1
280
    std::vector<cv::Vec2f> q0, q1;
280
    std::vector<cv::Vec2f> q0, q1;
281
    matchPhaseMaps(occlusion0, occlusion1, up0, up1, q0, q1);
281
    matchPhaseMaps(occlusion0, occlusion1, up0, up1, q0, q1);
282
 
282
 
283
    size_t nMatches = q0.size();
283
    size_t nMatches = q0.size();
284
 
284
 
285
    if(nMatches < 1){
285
    if(nMatches < 1){
286
        Q.resize(0);
286
        Q.resize(0);
287
        color.resize(0);
287
        color.resize(0);
288
 
288
 
289
        return;
289
        return;
290
    }
290
    }
291
    else {
291
    else {
292
        // Retrieve color information
292
        // Retrieve color information
293
        color.resize(nMatches);
293
        color.resize(nMatches);
294
        for(unsigned int i=0; i<nMatches; i++){
294
        for(unsigned int i=0; i<nMatches; i++){
295
 
295
 
296
            cv::Vec3b c0 = color0.at<cv::Vec3b>(int(q0[i][1]), int(q0[i][0]));
296
            cv::Vec3b c0 = color0.at<cv::Vec3b>(int(q0[i][1]), int(q0[i][0]));
297
            cv::Vec3b c1 = color1.at<cv::Vec3b>(int(q1[i][1]), int(q1[i][0]));
297
            cv::Vec3b c1 = color1.at<cv::Vec3b>(int(q1[i][1]), int(q1[i][0]));
298
 
298
 
299
            color[i] = 0.5*c0 + 0.5*c1;
299
            color[i] = 0.5*c0 + 0.5*c1;
300
        }
300
        }
301
    }
301
    }
302
 
302
 
303
    // Triangulate points
303
    // Triangulate points
304
    triangulate(stereoRect, q0, q1, Q);
304
    triangulate(stereoRect, q0, q1, Q);
305
 
305
 
306
}
306
}
307
 
307
 
308
void getStereoRectifyier(const SMCalibrationParameters &calibration,
308
void getStereoRectifyier(const SMCalibrationParameters &calibration,
309
                         const cv::Size& frameSize,
309
                         const cv::Size& frameSize,
310
                         StereoRectifyier& stereoRect){
310
                         StereoRectifyier& stereoRect){
311
 
311
 
312
    // cv::stereoRectify segfaults unless R is double precision
312
    // cv::stereoRectify segfaults unless R is double precision
313
    cv::Mat R, T;
313
    cv::Mat R, T;
314
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
314
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
315
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
315
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
316
 
316
 
317
    cv::stereoRectify(calibration.K0, calibration.k0,
317
    cv::stereoRectify(calibration.K0, calibration.k0,
318
                      calibration.K1, calibration.k1,
318
                      calibration.K1, calibration.k1,
319
                      frameSize, R, T,
319
                      frameSize, R, T,
320
                      stereoRect.R0, stereoRect.R1,
320
                      stereoRect.R0, stereoRect.R1,
321
                      stereoRect.P0, stereoRect.P1,
321
                      stereoRect.P0, stereoRect.P1,
322
                      stereoRect.QRect, 0);
322
                      stereoRect.QRect, 0);
323
 
323
 
324
    // Interpolation maps (lens distortion and rectification)
324
    // Interpolation maps (lens distortion and rectification)
325
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0,
325
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0,
326
                                stereoRect.R0, stereoRect.P0,
326
                                stereoRect.R0, stereoRect.P0,
327
                                frameSize, CV_32F,
327
                                frameSize, CV_32F,
328
                                stereoRect.map0X, stereoRect.map0Y);
328
                                stereoRect.map0X, stereoRect.map0Y);
329
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1,
329
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1,
330
                                stereoRect.R1, stereoRect.P1,
330
                                stereoRect.R1, stereoRect.P1,
331
                                frameSize, CV_32F,
331
                                frameSize, CV_32F,
332
                                stereoRect.map1X, stereoRect.map1Y);
332
                                stereoRect.map1X, stereoRect.map1Y);
333
}
333
}
334
 
334
 
335
void determineAmplitudePhaseEnergy(std::vector<cv::Mat>& frames,
335
void determineAmplitudePhaseEnergy(std::vector<cv::Mat>& frames,
336
                                   cv::Mat& amplitude,
336
                                   cv::Mat& amplitude,
337
                                   cv::Mat& phase,
337
                                   cv::Mat& phase,
338
                                   cv::Mat& energy) {
338
                                   cv::Mat& energy) {
339
 
339
 
340
    std::vector<cv::Mat> fourier = getDFTComponents(frames);
340
    std::vector<cv::Mat> fourier = getDFTComponents(frames);
341
 
341
 
342
    cv::phase(fourier[2], -fourier[3], phase);
342
    cv::phase(fourier[2], -fourier[3], phase);
343
 
343
 
344
    // Signal energy at unit frequency
344
    // Signal energy at unit frequency
345
    cv::magnitude(fourier[2], -fourier[3], amplitude);
345
    cv::magnitude(fourier[2], -fourier[3], amplitude);
346
 
346
 
347
    // Collected signal energy at higher frequencies
347
    // Collected signal energy at higher frequencies
348
    energy = cv::Mat(phase.rows, phase.cols, CV_32F, cv::Scalar(0.0));
348
    energy = cv::Mat(phase.rows, phase.cols, CV_32F, cv::Scalar(0.0));
349
 
349
 
350
    for(unsigned int i=0; i<frames.size()-1; i++){
350
    for(unsigned int i=0; i<frames.size()-1; i++){
351
        cv::Mat magnitude;
351
        cv::Mat magnitude;
352
        cv::magnitude(fourier[i*2 + 2], fourier[i*2 + 3], magnitude);
352
        cv::magnitude(fourier[i*2 + 2], fourier[i*2 + 3], magnitude);
353
        cv::add(energy, magnitude, energy, cv::noArray(), CV_32F);
353
        cv::add(energy, magnitude, energy, cv::noArray(), CV_32F);
354
    }
354
    }
355
 
355
 
356
    frames.clear();
356
    frames.clear();
357
}
357
}
358
 
358
 
359
void collectPhases(const cv::Mat& phasePrimary,
359
void collectPhases(const cv::Mat& phasePrimary,
360
                   const cv::Mat& phaseSecondary,
360
                   const cv::Mat& phaseSecondary,
361
                   cv::Mat& phase) {
361
                   cv::Mat& phase) {
362
    cv::Mat phaseEquivalent = phaseSecondary - phasePrimary;
362
    cv::Mat phaseEquivalent = phaseSecondary - phasePrimary;
363
    phaseEquivalent = cvtools::modulo(phaseEquivalent, 2.0*CV_PI);
363
    phaseEquivalent = cvtools::modulo(phaseEquivalent, 2.0*CV_PI);
364
    phase = unwrapWithCue(phasePrimary, phaseEquivalent, nPeriodsPrimary);
364
    phase = unwrapWithCue(phasePrimary, phaseEquivalent, nPeriodsPrimary);
365
    phase *= phasePrimary.cols/(2.0*CV_PI);
365
    phase *= phasePrimary.cols/(2.0*CV_PI);
366
}
366
}
367
 
367
 
368
 
368
 
369
void matchPhaseMaps(const cv::Mat& occlusion0, const cv::Mat& occlusion1,
369
void matchPhaseMaps(const cv::Mat& occlusion0, const cv::Mat& occlusion1,
370
                    const cv::Mat& phase0, const cv::Mat& phase1,
370
                    const cv::Mat& phase0, const cv::Mat& phase1,
371
                    std::vector<cv::Vec2f>& q0, std::vector<cv::Vec2f>& q1) {
371
                    std::vector<cv::Vec2f>& q0, std::vector<cv::Vec2f>& q1) {
372
 
372
 
373
    #pragma omp parallel for
373
    #pragma omp parallel for
374
    for(int row=0; row<occlusion0.rows; row++){
374
    for(int row=0; row<occlusion0.rows; row++){
375
        for(int col=0; col<occlusion0.cols; col++){
375
        for(int col=0; col<occlusion0.cols; col++){
376
 
376
 
377
            if(!occlusion0.at<char>(row,col))
377
            if(!occlusion0.at<char>(row,col))
378
                continue;
378
                continue;
379
 
379
 
380
            float phase0i = phase0.at<float>(row,col);
380
            float phase0i = phase0.at<float>(row,col);
381
            for(int col1=0; col1<phase1.cols-1; col1++){
381
            for(int col1=0; col1<phase1.cols-1; col1++){
382
 
382
 
383
                if(!occlusion1.at<char>(row,col1) || !occlusion1.at<char>(row,col1+1))
383
                if(!occlusion1.at<char>(row,col1) || !occlusion1.at<char>(row,col1+1))
384
                    continue;
384
                    continue;
385
 
385
 
386
                float phase1Left = phase1.at<float>(row,col1);
386
                float phase1Left = phase1.at<float>(row,col1);
387
                float phase1Right = phase1.at<float>(row,col1+1);
387
                float phase1Right = phase1.at<float>(row,col1+1);
388
 
388
 
389
                bool match = (phase1Left <= phase0i)
389
                bool match = (phase1Left <= phase0i)
390
                              && (phase0i <= phase1Right)
390
                              && (phase0i <= phase1Right)
391
                              && (phase0i-phase1Left < 1.0)
391
                              && (phase0i-phase1Left < 1.0)
392
                              && (phase1Right-phase0i < 1.0)
392
                              && (phase1Right-phase0i < 1.0)
393
                              && (phase1Right-phase1Left > 0.1);
393
                              && (phase1Right-phase1Left > 0.1);
394
 
394
 
395
                if(match){
395
                if(match){
396
 
396
 
397
                    float col1i = col1 + (phase0i-phase1Left)/(phase1Right-phase1Left);
397
                    float col1i = col1 + (phase0i-phase1Left)/(phase1Right-phase1Left);
398
 
398
 
399
                    #pragma omp critical
399
                    #pragma omp critical
400
                    {
400
                    {
401
                    q0.push_back(cv::Point2f(col, row));
401
                    q0.push_back(cv::Point2f(col, row));
402
                    q1.push_back(cv::Point2f(col1i, row));
402
                    q1.push_back(cv::Point2f(col1i, row));
403
                    }
403
                    }
404
                    break;
404
                    break;
405
                }
405
                }
406
            }
406
            }
407
        }
407
        }
408
    }
408
    }
409
 
409
 
410
}
410
}
411
 
411
 
412
void triangulate(const StereoRectifyier& stereoRect,
412
void triangulate(const StereoRectifyier& stereoRect,
413
                 const std::vector<cv::Vec2f>& q0,
413
                 const std::vector<cv::Vec2f>& q0,
414
                 const std::vector<cv::Vec2f>& q1,
414
                 const std::vector<cv::Vec2f>& q1,
415
                 std::vector<cv::Point3f>& Q) {
415
                 std::vector<cv::Point3f>& Q) {
416
 
416
 
417
    // cv::Mat QMatHomogenous, QMat;
417
    // cv::Mat QMatHomogenous, QMat;
418
    // cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
418
    // cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
419
    // cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
419
    // cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
420
 
420
 
421
    // // Undo rectification
421
    // // Undo rectification
422
    // cv::Mat R0Inv;
422
    // cv::Mat R0Inv;
423
    // cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
423
    // cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
424
    // QMat = R0Inv*QMat;
424
    // QMat = R0Inv*QMat;
425
 
425
 
426
    // cvtools::matToPoints3f(QMat, Q);
426
    // cvtools::matToPoints3f(QMat, Q);
427
 
427
 
428
 
428
 
429
    // Triangulate by means of disparity projection
429
    // Triangulate by means of disparity projection
430
    Q.resize(q0.size());
430
    Q.resize(q0.size());
431
    cv::Matx44f QRectx = cv::Matx44f(stereoRect.QRect);
431
    cv::Matx44f QRectx = cv::Matx44f(stereoRect.QRect);
432
    cv::Matx33f R0invx = cv::Matx33f(cv::Mat(stereoRect.R0.t()));
432
    cv::Matx33f R0invx = cv::Matx33f(cv::Mat(stereoRect.R0.t()));
433
 
433
 
434
    #pragma omp parallel for
434
    #pragma omp parallel for
435
    for(unsigned int i=0; i < q0.size(); i++){
435
    for(unsigned int i=0; i < q0.size(); i++){
436
        float disparity = q0[i][0] - q1[i][0];
436
        float disparity = q0[i][0] - q1[i][0];
437
        cv::Vec4f Qih = QRectx*cv::Vec4f(q0[i][0], q0[i][1], disparity, 1.0);
437
        cv::Vec4f Qih = QRectx*cv::Vec4f(q0[i][0], q0[i][1], disparity, 1.0);
438
        float winv = float(1.0) / Qih[3];
438
        float winv = float(1.0) / Qih[3];
439
        Q[i] = R0invx * cv::Point3f(Qih[0]*winv, Qih[1]*winv, Qih[2]*winv);
439
        Q[i] = R0invx * cv::Point3f(Qih[0]*winv, Qih[1]*winv, Qih[2]*winv);
440
    }
440
    }
441
}
441
}
442
 
442