Subversion Repositories seema-scanner

Rev

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

Rev 235 Rev 244
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
void getStereoRectifier(const SMCalibrationParameters &calibration,
77
                                const cv::Size& frameSize,
77
                         const cv::Size& frameSize,
78
                                StereoRectifyier& stereoRect);
78
                         StereoRectifyier& stereoRect){
-
 
79
 
-
 
80
    // cv::stereoRectify segfaults unless R is double precision
-
 
81
    cv::Mat R, T;
-
 
82
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
-
 
83
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
-
 
84
 
-
 
85
    cv::stereoRectify(calibration.K0, calibration.k0,
-
 
86
                      calibration.K1, calibration.k1,
-
 
87
                      frameSize, R, T,
-
 
88
                      stereoRect.R0, stereoRect.R1,
-
 
89
                      stereoRect.P0, stereoRect.P1,
-
 
90
                      stereoRect.QRect, 0);
-
 
91
 
-
 
92
    // Interpolation maps (lens distortion and rectification)
-
 
93
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0,
-
 
94
                                stereoRect.R0, stereoRect.P0,
-
 
95
                                frameSize, CV_32F,
-
 
96
                                stereoRect.map0X, stereoRect.map0Y);
-
 
97
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1,
-
 
98
                                stereoRect.R1, stereoRect.P1,
-
 
99
                                frameSize, CV_32F,
-
 
100
                                stereoRect.map1X, stereoRect.map1Y);
-
 
101
}
-
 
102
 
79
static void determineAmplitudePhaseEnergy(std::vector<cv::Mat>& frames,
103
void determineAmplitudePhaseEnergy(std::vector<cv::Mat>& frames,
80
                                    cv::Mat& amplitude,
104
                                   cv::Mat& amplitude,
81
                                    cv::Mat& phase,
105
                                   cv::Mat& phase,
82
                                    cv::Mat& energy);
106
                                   cv::Mat& energy) {
-
 
107
 
-
 
108
    std::vector<cv::Mat> fourier = getDFTComponents(frames);
-
 
109
 
-
 
110
    cv::phase(fourier[2], -fourier[3], phase);
-
 
111
 
-
 
112
    // Signal energy at unit frequency
-
 
113
    cv::magnitude(fourier[2], -fourier[3], amplitude);
-
 
114
 
-
 
115
    // Collected signal energy at higher frequencies
-
 
116
    energy = cv::Mat(phase.rows, phase.cols, CV_32F, cv::Scalar(0.0));
-
 
117
 
-
 
118
    for(unsigned int i=0; i<frames.size()-1; i++){
-
 
119
        cv::Mat magnitude;
-
 
120
        cv::magnitude(fourier[i*2 + 2], fourier[i*2 + 3], magnitude);
-
 
121
        cv::add(energy, magnitude, energy, cv::noArray(), CV_32F);
-
 
122
    }
-
 
123
 
-
 
124
    frames.clear();
-
 
125
}
-
 
126
 
83
static void collectPhases(const cv::Mat& phasePrimary,
127
void unWrapPhaseMap(const cv::Mat& phasePrimary,
84
                          const cv::Mat& phaseSecondary,
128
                   const cv::Mat& phaseSecondary,
85
                          cv::Mat& phase);
129
                   cv::Mat& phase) {
-
 
130
    cv::Mat phaseEquivalent = phaseSecondary - phasePrimary;
-
 
131
    phaseEquivalent = cvtools::modulo(phaseEquivalent, 2.0*CV_PI);
-
 
132
    phase = unwrapWithCue(phasePrimary, phaseEquivalent, nPeriodsPrimary);
-
 
133
    phase *= phasePrimary.cols/(2.0*CV_PI);
-
 
134
}
-
 
135
 
-
 
136
 
86
static void matchPhaseMaps(const cv::Mat& occlusion0, const cv::Mat& occlusion1,
137
void matchPhaseMaps(const cv::Mat& occlusion0, const cv::Mat& occlusion1,
87
                           const cv::Mat& phase0, const cv::Mat& phase1,
138
                    const cv::Mat& phase0, const cv::Mat& phase1,
88
                           std::vector<cv::Vec2f>& q0, std::vector<cv::Vec2f>& q1);
139
                    std::vector<cv::Vec2f>& q0, std::vector<cv::Vec2f>& q1) {
-
 
140
 
89
static void triangulate(const StereoRectifyier& stereoRect,
141
    #pragma omp parallel for
90
                        const std::vector<cv::Vec2f>& q0,
-
 
91
                        const std::vector<cv::Vec2f>& q1,
142
    for(int row=0; row<occlusion0.rows; row++){
92
                        std::vector<cv::Point3f>& Q);
143
        for(int col=0; col<occlusion0.cols; col++){
93
 
144
 
-
 
145
            if(!occlusion0.at<char>(row,col))
-
 
146
                continue;
-
 
147
 
-
 
148
            float phase0i = phase0.at<float>(row,col);
-
 
149
            for(int col1=0; col1<phase1.cols-1; col1++){
-
 
150
 
-
 
151
                if(!occlusion1.at<char>(row,col1) || !occlusion1.at<char>(row,col1+1))
-
 
152
                    continue;
-
 
153
 
-
 
154
                float phase1Left = phase1.at<float>(row,col1);
-
 
155
                float phase1Right = phase1.at<float>(row,col1+1);
-
 
156
 
-
 
157
                bool match = (phase1Left <= phase0i)
-
 
158
                              && (phase0i <= phase1Right)
-
 
159
                              && (phase0i-phase1Left < 1.0)
-
 
160
                              && (phase1Right-phase0i < 1.0)
-
 
161
                              && (phase1Right-phase1Left > 0.1);
-
 
162
 
-
 
163
                if(match){
-
 
164
 
-
 
165
                    float col1i = col1 + (phase0i-phase1Left)/(phase1Right-phase1Left);
-
 
166
 
-
 
167
                    #pragma omp critical
-
 
168
                    {
-
 
169
                    q0.push_back(cv::Point2f(col, row));
-
 
170
                    q1.push_back(cv::Point2f(col1i, row));
-
 
171
                    }
-
 
172
                    break;
-
 
173
                }
-
 
174
            }
-
 
175
        }
-
 
176
    }
-
 
177
 
-
 
178
}
-
 
179
 
-
 
180
void triangulate(const StereoRectifyier& stereoRect,
-
 
181
                 const std::vector<cv::Vec2f>& q0,
-
 
182
                 const std::vector<cv::Vec2f>& q1,
-
 
183
                 std::vector<cv::Point3f>& Q) {
-
 
184
 
-
 
185
    // cv::Mat QMatHomogenous, QMat;
-
 
186
    // cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
-
 
187
    // cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
-
 
188
 
-
 
189
    // // Undo rectification
-
 
190
    // cv::Mat R0Inv;
-
 
191
    // cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
-
 
192
    // QMat = R0Inv*QMat;
-
 
193
 
-
 
194
    // cvtools::matToPoints3f(QMat, Q);
-
 
195
 
-
 
196
 
-
 
197
    // Triangulate by means of disparity projection
-
 
198
    Q.resize(q0.size());
-
 
199
    cv::Matx44f QRectx = cv::Matx44f(stereoRect.QRect);
-
 
200
    cv::Matx33f R0invx = cv::Matx33f(cv::Mat(stereoRect.R0.t()));
-
 
201
 
-
 
202
    #pragma omp parallel for
-
 
203
    for(unsigned int i=0; i < q0.size(); i++){
-
 
204
        float disparity = q0[i][0] - q1[i][0];
-
 
205
        cv::Vec4f Qih = QRectx*cv::Vec4f(q0[i][0], q0[i][1], disparity, 1.0);
-
 
206
        float winv = float(1.0) / Qih[3];
-
 
207
        Q[i] = R0invx * cv::Point3f(Qih[0]*winv, Qih[1]*winv, Qih[2]*winv);
-
 
208
    }
-
 
209
}
94
 
210
 
95
void AlgorithmPhaseShiftTwoFreq::
211
void AlgorithmPhaseShiftTwoFreq::
96
    get3DPoints(const SMCalibrationParameters & calibration,
212
    get3DPoints(const SMCalibrationParameters & calibration,
97
                const std::vector<cv::Mat>& frames0,
213
                const std::vector<cv::Mat>& frames0,
98
                const std::vector<cv::Mat>& frames1,
214
                const std::vector<cv::Mat>& frames1,
99
                std::vector<cv::Point3f>& Q,
215
                std::vector<cv::Point3f>& Q,
100
                std::vector<cv::Vec3b>& color){
216
                std::vector<cv::Vec3b>& color){
101
 
217
 
102
    assert(frames0.size() == N);
218
    assert(frames0.size() == N);
103
    assert(frames1.size() == N);
219
    assert(frames1.size() == N);
104
 
220
 
105
    StereoRectifyier stereoRect;
221
    StereoRectifyier stereoRect;
106
    getStereoRectifyier(calibration,
222
    getStereoRectifier(calibration,
107
                        cv::Size(frames0[0].cols, frames0[0].rows),
223
                        cv::Size(frames0[0].cols, frames0[0].rows),
108
                        stereoRect);
224
                        stereoRect);
109
 
225
 
110
    // // Erode occlusion masks
226
    // // Erode occlusion masks
111
    // cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
227
    // cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
112
 
228
 
113
    cv::Mat up0, up1;
229
    cv::Mat up0, up1;
114
    cv::Mat occlusion0, occlusion1;
230
    cv::Mat occlusion0, occlusion1;
115
    cv::Mat color0, color1;
231
    cv::Mat color0, color1;
116
 
232
 
117
    #pragma omp parallel sections
233
    #pragma omp parallel sections
118
    {
234
    {
119
        #pragma omp section
235
        #pragma omp section
120
        {
236
        {
121
 
237
 
122
            // Gray-scale and remap/rectify
238
            // Gray-scale and remap/rectify
123
            std::vector<cv::Mat> frames0Rect(N);
239
            std::vector<cv::Mat> frames0Rect(N);
124
 
240
 
125
            for(unsigned int i=0; i<N; i++){
241
            for(unsigned int i=0; i<N; i++){
126
                cv::Mat temp;
242
                cv::Mat temp;
-
 
243
                if(frames1[i].depth() == CV_8U)
127
                cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
244
                    cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
-
 
245
                else
-
 
246
                    temp = frames1[i];
128
                cv::remap(temp, frames0Rect[i],
247
                cv::remap(temp, frames0Rect[i],
129
                          stereoRect.map0X, stereoRect.map0Y,
248
                          stereoRect.map0X, stereoRect.map0Y,
130
                          CV_INTER_LINEAR);
249
                          CV_INTER_LINEAR);
131
            }
250
            }
132
 
251
 
133
            cv::cvtColor(frames0[0], color0, CV_BayerBG2RGB);
252
            // If images are HDR (float), we need to convert to uchar
134
            cv::remap(color0, color0,
253
            frames0Rect[0].convertTo(color0, CV_8UC3);
135
                      stereoRect.map0X, stereoRect.map0Y,
-
 
136
                      CV_INTER_LINEAR);
-
 
137
 
254
 
138
            // Occlusion masks
255
            // Occlusion masks
139
            cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0);
256
            cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0);
140
            occlusion0 = (occlusion0 > 25) & (occlusion0 < 250);
257
            occlusion0 = (occlusion0 > 25) & (occlusion0 < 250);
141
 
258
 
142
            // Decode camera0
259
            // Decode camera0
143
            std::vector<cv::Mat> frames0Primary(frames0Rect.begin()+2,
260
            std::vector<cv::Mat> frames0Primary(frames0Rect.begin()+2,
144
                                                frames0Rect.begin()+2+nStepsPrimary);
261
                                                frames0Rect.begin()+2+nStepsPrimary);
145
            std::vector<cv::Mat> frames0Secondary(frames0Rect.begin()+2+nStepsPrimary,
262
            std::vector<cv::Mat> frames0Secondary(frames0Rect.begin()+2+nStepsPrimary,
146
                                                  frames0Rect.end());
263
                                                  frames0Rect.end());
147
 
264
 
148
            frames0Rect.clear();
265
            frames0Rect.clear();
149
 
266
 
150
            cv::Mat amplitude0Primary, amplitude0Secondary;
267
            cv::Mat amplitude0Primary, amplitude0Secondary;
151
            cv::Mat up0Primary, up0Secondary;
268
            cv::Mat up0Primary, up0Secondary;
152
            cv::Mat energy0Primary, energy0Secondary;
269
            cv::Mat energy0Primary, energy0Secondary;
153
            determineAmplitudePhaseEnergy(frames0Primary,
270
            determineAmplitudePhaseEnergy(frames0Primary,
154
                                          amplitude0Primary,
271
                                          amplitude0Primary,
155
                                          up0Primary,
272
                                          up0Primary,
156
                                          energy0Primary);
273
                                          energy0Primary);
157
            determineAmplitudePhaseEnergy(frames0Secondary,
274
            determineAmplitudePhaseEnergy(frames0Secondary,
158
                                          amplitude0Secondary,
275
                                          amplitude0Secondary,
159
                                          up0Secondary,
276
                                          up0Secondary,
160
                                          energy0Secondary);
277
                                          energy0Secondary);
161
 
278
 
162
            collectPhases(up0Primary, up0Secondary, up0);
279
            unWrapPhaseMap(up0Primary, up0Secondary, up0);
163
 
280
 
164
            // Threshold on energy at primary frequency
281
            // Threshold on energy at primary frequency
165
            occlusion0 = occlusion0 & (amplitude0Primary > 5.0*nStepsPrimary);
282
            occlusion0 = occlusion0 & (amplitude0Primary > 5.0*nStepsPrimary);
166
            // Threshold on energy ratios
283
            // Threshold on energy ratios
167
            occlusion0 = occlusion0 & (amplitude0Primary > 0.25*energy0Primary);
284
            occlusion0 = occlusion0 & (amplitude0Primary > 0.25*energy0Primary);
168
            occlusion0 = occlusion0 & (amplitude0Secondary > 0.25*energy0Secondary);
285
            occlusion0 = occlusion0 & (amplitude0Secondary > 0.25*energy0Secondary);
169
 
286
 
170
            // // Erode occlusion masks
287
            // // Erode occlusion masks
171
            // cv::erode(occlusion0, occlusion0, strel);
288
            // cv::erode(occlusion0, occlusion0, strel);
172
 
289
 
173
            // Threshold on gradient of phase
290
            // Threshold on vertical gradient of phase
174
            cv::Mat edges0;
291
            cv::Mat edges0;
175
            cv::Sobel(up0, edges0, -1, 1, 1, 5);
292
            cv::Sobel(up0, edges0, -1, 1, 1, 5);
176
            occlusion0 = occlusion0 & (abs(edges0) < 10);
293
            occlusion0 = occlusion0 & (abs(edges0) < 10);
177
 
294
 
178
            #ifdef SM_DEBUG
295
            #ifdef SM_DEBUG
179
                cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
296
                cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
180
                cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
297
                cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
181
                cvtools::writeMat(up0, "up0.mat", "up0");
298
                cvtools::writeMat(up0, "up0.mat", "up0");
182
                cvtools::writeMat(amplitude0Primary,
299
                cvtools::writeMat(amplitude0Primary,
183
                                  "amplitude0Primary.mat", "amplitude0Primary");
300
                                  "amplitude0Primary.mat", "amplitude0Primary");
184
                cvtools::writeMat(amplitude0Secondary,
301
                cvtools::writeMat(amplitude0Secondary,
185
                                  "amplitude0Secondary.mat", "amplitude0Secondary");
302
                                  "amplitude0Secondary.mat", "amplitude0Secondary");
186
                cvtools::writeMat(energy0Primary,
303
                cvtools::writeMat(energy0Primary,
187
                                  "energy0Primary.mat", "energy0Primary");
304
                                  "energy0Primary.mat", "energy0Primary");
188
                cvtools::writeMat(energy0Secondary,
305
                cvtools::writeMat(energy0Secondary,
189
                                  "energy0Secondary.mat", "energy0Secondary");
306
                                  "energy0Secondary.mat", "energy0Secondary");
190
                cvtools::writeMat(edges0, "edges0.mat", "edges0");
307
                cvtools::writeMat(edges0, "edges0.mat", "edges0");
191
                cvtools::writeMat(occlusion0, "occlusion0.mat", "occlusion0");
308
                cvtools::writeMat(occlusion0, "occlusion0.mat", "occlusion0");
192
                cvtools::writeMat(color0, "color0.mat", "color0");
309
                cvtools::writeMat(color0, "color0.mat", "color0");
193
            #endif
310
            #endif
194
 
311
 
195
        }
312
        }
196
        #pragma omp section
313
        #pragma omp section
197
        {
314
        {
198
 
315
 
199
            // Gray-scale and remap
316
            // Gray-scale and remap
200
            std::vector<cv::Mat> frames1Rect(N);
317
            std::vector<cv::Mat> frames1Rect(N);
201
 
318
 
202
            for(unsigned int i=0; i<N; i++){
319
            for(unsigned int i=0; i<N; i++){
203
                cv::Mat temp;
320
                cv::Mat temp;
-
 
321
                if(frames1[i].depth() == CV_8U)
204
                cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
322
                    cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
-
 
323
                else
-
 
324
                    temp = frames1[i];
205
                cv::remap(temp, frames1Rect[i],
325
                cv::remap(temp, frames1Rect[i],
206
                          stereoRect.map1X, stereoRect.map1Y,
326
                          stereoRect.map1X, stereoRect.map1Y,
207
                          CV_INTER_LINEAR);
327
                          CV_INTER_LINEAR);
208
            }
328
            }
209
 
329
 
210
            cv::cvtColor(frames1[0], color1, CV_BayerBG2RGB);
330
            // If images are HDR (float), we need to convert to uchar
211
            cv::remap(color1, color1,
331
            frames1Rect[0].convertTo(color1, CV_8UC3);
212
                      stereoRect.map1X, stereoRect.map1Y,
-
 
213
                      CV_INTER_LINEAR);
-
 
214
 
332
 
215
            // Occlusion masks
333
            // Occlusion masks
216
            cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1);
334
            cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1);
217
            occlusion1 = (occlusion1 > 25) & (occlusion1 < 250);
335
            occlusion1 = (occlusion1 > 25) & (occlusion1 < 250);
218
 
336
 
219
            // Decode camera1
337
            // Decode camera1
220
            std::vector<cv::Mat> frames1Primary(frames1Rect.begin()+2,
338
            std::vector<cv::Mat> frames1Primary(frames1Rect.begin()+2,
221
                                                frames1Rect.begin()+2+nStepsPrimary);
339
                                                frames1Rect.begin()+2+nStepsPrimary);
222
            std::vector<cv::Mat> frames1Secondary(frames1Rect.begin()+2+nStepsPrimary,
340
            std::vector<cv::Mat> frames1Secondary(frames1Rect.begin()+2+nStepsPrimary,
223
                                                  frames1Rect.end());
341
                                                  frames1Rect.end());
224
 
342
 
225
            frames1Rect.clear();
343
            frames1Rect.clear();
226
 
344
 
227
            cv::Mat amplitude1Primary, amplitude1Secondary;
345
            cv::Mat amplitude1Primary, amplitude1Secondary;
228
            cv::Mat up1Primary, up1Secondary;
346
            cv::Mat up1Primary, up1Secondary;
229
            cv::Mat energy1Primary, energy1Secondary;
347
            cv::Mat energy1Primary, energy1Secondary;
230
            determineAmplitudePhaseEnergy(frames1Primary,
348
            determineAmplitudePhaseEnergy(frames1Primary,
231
                                          amplitude1Primary,
349
                                          amplitude1Primary,
232
                                          up1Primary,
350
                                          up1Primary,
233
                                          energy1Primary);
351
                                          energy1Primary);
234
            determineAmplitudePhaseEnergy(frames1Secondary,
352
            determineAmplitudePhaseEnergy(frames1Secondary,
235
                                          amplitude1Secondary,
353
                                          amplitude1Secondary,
236
                                          up1Secondary,
354
                                          up1Secondary,
237
                                          energy1Secondary);
355
                                          energy1Secondary);
238
 
356
 
239
            collectPhases(up1Primary, up1Secondary, up1);
357
            unWrapPhaseMap(up1Primary, up1Secondary, up1);
240
 
358
 
241
            // Threshold on energy at primary frequency
359
            // Threshold on energy at primary frequency
242
            occlusion1 = occlusion1 & (amplitude1Primary > 5.0*nStepsPrimary);
360
            occlusion1 = occlusion1 & (amplitude1Primary > 5.0*nStepsPrimary);
243
            // Threshold on energy ratios
361
            // Threshold on energy ratios
244
            occlusion1 = occlusion1 & (amplitude1Primary > 0.25*energy1Primary);
362
            occlusion1 = occlusion1 & (amplitude1Primary > 0.25*energy1Primary);
245
            occlusion1 = occlusion1 & (amplitude1Secondary > 0.25*energy1Secondary);
363
            occlusion1 = occlusion1 & (amplitude1Secondary > 0.25*energy1Secondary);
246
 
364
 
247
            // // Erode occlusion masks
365
            // // Erode occlusion masks
248
            // cv::erode(occlusion1, occlusion1, strel);
366
            // cv::erode(occlusion1, occlusion1, strel);
249
 
367
 
250
 
368
 
251
            // Threshold on gradient of phase
369
            // Threshold on vertical gradient of phase
252
            cv::Mat edges1;
370
            cv::Mat edges1;
253
            cv::Sobel(up1, edges1, -1, 1, 1, 5);
371
            cv::Sobel(up1, edges1, -1, 1, 1, 5);
254
            occlusion1 = occlusion1 & (abs(edges1) < 10);
372
            occlusion1 = occlusion1 & (abs(edges1) < 10);
255
 
373
 
256
            #ifdef SM_DEBUG
374
            #ifdef SM_DEBUG
257
                cvtools::writeMat(up1Primary, "up1Primary.mat", "up1Primary");
375
                cvtools::writeMat(up1Primary, "up1Primary.mat", "up1Primary");
258
                cvtools::writeMat(up1Secondary, "up1Secondary.mat", "up1Secondary");
376
                cvtools::writeMat(up1Secondary, "up1Secondary.mat", "up1Secondary");
259
                cvtools::writeMat(up1, "up1.mat", "up1");
377
                cvtools::writeMat(up1, "up1.mat", "up1");
260
                cvtools::writeMat(amplitude1Primary,
378
                cvtools::writeMat(amplitude1Primary,
261
                                  "amplitude1Primary.mat", "amplitude1Primary");
379
                                  "amplitude1Primary.mat", "amplitude1Primary");
262
                cvtools::writeMat(amplitude1Secondary,
380
                cvtools::writeMat(amplitude1Secondary,
263
                                  "amplitude1Secondary.mat", "amplitude1Secondary");
381
                                  "amplitude1Secondary.mat", "amplitude1Secondary");
264
                cvtools::writeMat(energy1Primary,
382
                cvtools::writeMat(energy1Primary,
265
                                  "energy1Primary.mat", "energy1Primary");
383
                                  "energy1Primary.mat", "energy1Primary");
266
                cvtools::writeMat(energy1Secondary,
384
                cvtools::writeMat(energy1Secondary,
267
                                  "energy1Secondary.mat", "energy1Secondary");
385
                                  "energy1Secondary.mat", "energy1Secondary");
268
                cvtools::writeMat(edges1, "edges1.mat", "edges1");
386
                cvtools::writeMat(edges1, "edges1.mat", "edges1");
269
                cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
387
                cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
270
                cvtools::writeMat(color1, "color1.mat", "color1");
388
                cvtools::writeMat(color1, "color1.mat", "color1");
271
            #endif
389
            #endif
272
 
390
 
273
        }
391
        }
274
    }
392
    }
275
 
393
 
276
 
394
 
277
    // Match phase maps
395
    // Match phase maps
278
 
396
 
279
    // camera0 against camera1
397
    // camera0 against camera1
280
    std::vector<cv::Vec2f> q0, q1;
398
    std::vector<cv::Vec2f> q0, q1;
281
    matchPhaseMaps(occlusion0, occlusion1, up0, up1, q0, q1);
399
    matchPhaseMaps(occlusion0, occlusion1, up0, up1, q0, q1);
282
 
400
 
283
    size_t nMatches = q0.size();
401
    size_t nMatches = q0.size();
284
 
402
 
285
    if(nMatches < 1){
403
    if(nMatches < 1){
286
        Q.resize(0);
404
        Q.resize(0);
287
        color.resize(0);
405
        color.resize(0);
288
 
406
 
289
        return;
407
        return;
290
    }
408
    }
291
    else {
409
    else {
292
        // Retrieve color information
410
        // Retrieve color information
293
        color.resize(nMatches);
411
        color.resize(nMatches);
294
        for(unsigned int i=0; i<nMatches; i++){
412
        for(unsigned int i=0; i<nMatches; i++){
295
 
413
 
296
            cv::Vec3b c0 = color0.at<cv::Vec3b>(int(q0[i][1]), int(q0[i][0]));
414
            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]));
415
            cv::Vec3b c1 = color1.at<cv::Vec3b>(int(q1[i][1]), int(q1[i][0]));
298
 
416
 
299
            color[i] = 0.5*c0 + 0.5*c1;
417
            color[i] = 0.5*c0 + 0.5*c1;
300
        }
418
        }
301
    }
419
    }
302
 
420
 
303
    // Triangulate points
421
    // Triangulate points
304
    triangulate(stereoRect, q0, q1, Q);
422
    triangulate(stereoRect, q0, q1, Q);
305
 
423
 
306
}
424
}
307
 
425
 
308
void getStereoRectifyier(const SMCalibrationParameters &calibration,
-
 
309
                         const cv::Size& frameSize,
-
 
310
                         StereoRectifyier& stereoRect){
-
 
311
 
-
 
312
    // cv::stereoRectify segfaults unless R is double precision
-
 
313
    cv::Mat R, T;
-
 
314
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
-
 
315
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
-
 
316
 
-
 
317
    cv::stereoRectify(calibration.K0, calibration.k0,
-
 
318
                      calibration.K1, calibration.k1,
-
 
319
                      frameSize, R, T,
-
 
320
                      stereoRect.R0, stereoRect.R1,
-
 
321
                      stereoRect.P0, stereoRect.P1,
-
 
322
                      stereoRect.QRect, 0);
-
 
323
 
-
 
324
    // Interpolation maps (lens distortion and rectification)
-
 
325
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0,
-
 
326
                                stereoRect.R0, stereoRect.P0,
-
 
327
                                frameSize, CV_32F,
-
 
328
                                stereoRect.map0X, stereoRect.map0Y);
-
 
329
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1,
-
 
330
                                stereoRect.R1, stereoRect.P1,
-
 
331
                                frameSize, CV_32F,
-
 
332
                                stereoRect.map1X, stereoRect.map1Y);
-
 
333
}
-
 
334
 
-
 
335
void determineAmplitudePhaseEnergy(std::vector<cv::Mat>& frames,
-
 
336
                                   cv::Mat& amplitude,
-
 
337
                                   cv::Mat& phase,
-
 
338
                                   cv::Mat& energy) {
-
 
339
 
-
 
340
    std::vector<cv::Mat> fourier = getDFTComponents(frames);
-
 
341
 
-
 
342
    cv::phase(fourier[2], -fourier[3], phase);
-
 
343
 
-
 
344
    // Signal energy at unit frequency
-
 
345
    cv::magnitude(fourier[2], -fourier[3], amplitude);
-
 
346
 
-
 
347
    // Collected signal energy at higher frequencies
-
 
348
    energy = cv::Mat(phase.rows, phase.cols, CV_32F, cv::Scalar(0.0));
-
 
349
 
-
 
350
    for(unsigned int i=0; i<frames.size()-1; i++){
-
 
351
        cv::Mat magnitude;
-
 
352
        cv::magnitude(fourier[i*2 + 2], fourier[i*2 + 3], magnitude);
-
 
353
        cv::add(energy, magnitude, energy, cv::noArray(), CV_32F);
-
 
354
    }
-
 
355
 
-
 
356
    frames.clear();
-
 
357
}
-
 
358
 
-
 
359
void collectPhases(const cv::Mat& phasePrimary,
-
 
360
                   const cv::Mat& phaseSecondary,
-
 
361
                   cv::Mat& phase) {
-
 
362
    cv::Mat phaseEquivalent = phaseSecondary - phasePrimary;
-
 
363
    phaseEquivalent = cvtools::modulo(phaseEquivalent, 2.0*CV_PI);
-
 
364
    phase = unwrapWithCue(phasePrimary, phaseEquivalent, nPeriodsPrimary);
-
 
365
    phase *= phasePrimary.cols/(2.0*CV_PI);
-
 
366
}
-
 
367
 
-
 
368
 
-
 
369
void matchPhaseMaps(const cv::Mat& occlusion0, const cv::Mat& occlusion1,
-
 
370
                    const cv::Mat& phase0, const cv::Mat& phase1,
-
 
371
                    std::vector<cv::Vec2f>& q0, std::vector<cv::Vec2f>& q1) {
-
 
372
 
-
 
373
    #pragma omp parallel for
-
 
374
    for(int row=0; row<occlusion0.rows; row++){
-
 
375
        for(int col=0; col<occlusion0.cols; col++){
-
 
376
 
-
 
377
            if(!occlusion0.at<char>(row,col))
-
 
378
                continue;
-
 
379
 
-
 
380
            float phase0i = phase0.at<float>(row,col);
-
 
381
            for(int col1=0; col1<phase1.cols-1; col1++){
-
 
382
 
-
 
383
                if(!occlusion1.at<char>(row,col1) || !occlusion1.at<char>(row,col1+1))
-
 
384
                    continue;
-
 
385
 
-
 
386
                float phase1Left = phase1.at<float>(row,col1);
-
 
387
                float phase1Right = phase1.at<float>(row,col1+1);
-
 
388
 
-
 
389
                bool match = (phase1Left <= phase0i)
-
 
390
                              && (phase0i <= phase1Right)
-
 
391
                              && (phase0i-phase1Left < 1.0)
-
 
392
                              && (phase1Right-phase0i < 1.0)
-
 
393
                              && (phase1Right-phase1Left > 0.1);
-
 
394
 
-
 
395
                if(match){
-
 
396
 
-
 
397
                    float col1i = col1 + (phase0i-phase1Left)/(phase1Right-phase1Left);
-
 
398
 
-
 
399
                    #pragma omp critical
-
 
400
                    {
-
 
401
                    q0.push_back(cv::Point2f(col, row));
-
 
402
                    q1.push_back(cv::Point2f(col1i, row));
-
 
403
                    }
-
 
404
                    break;
-
 
405
                }
-
 
406
            }
-
 
407
        }
-
 
408
    }
-
 
409
 
-
 
410
}
-
 
411
 
-
 
412
void triangulate(const StereoRectifyier& stereoRect,
-
 
413
                 const std::vector<cv::Vec2f>& q0,
-
 
414
                 const std::vector<cv::Vec2f>& q1,
-
 
415
                 std::vector<cv::Point3f>& Q) {
-
 
416
 
-
 
417
    // cv::Mat QMatHomogenous, QMat;
-
 
418
    // cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
-
 
419
    // cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
-
 
420
 
-
 
421
    // // Undo rectification
-
 
422
    // cv::Mat R0Inv;
-
 
423
    // cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
-
 
424
    // QMat = R0Inv*QMat;
-
 
425
 
-
 
426
    // cvtools::matToPoints3f(QMat, Q);
-
 
427
 
-
 
428
 
-
 
429
    // Triangulate by means of disparity projection
-
 
430
    Q.resize(q0.size());
-
 
431
    cv::Matx44f QRectx = cv::Matx44f(stereoRect.QRect);
-
 
432
    cv::Matx33f R0invx = cv::Matx33f(cv::Mat(stereoRect.R0.t()));
-
 
433
 
-
 
434
    #pragma omp parallel for
-
 
435
    for(unsigned int i=0; i < q0.size(); i++){
-
 
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);
-
 
438
        float winv = float(1.0) / Qih[3];
-
 
439
        Q[i] = R0invx * cv::Point3f(Qih[0]*winv, Qih[1]*winv, Qih[2]*winv);
-
 
440
    }
-
 
441
}
-
 
442
 
426