Subversion Repositories seema-scanner

Rev

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

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