Subversion Repositories seema-scanner

Rev

Rev 251 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 251 Rev 257
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
 
110
    cv::phase(fourier[2], -fourier[3], phase);
111
    cv::phase(fourier[2], -fourier[3], phase);
111
 
112
 
112
    // Signal energy at unit frequency
113
    // Signal energy at unit frequency
113
    cv::magnitude(fourier[2], -fourier[3], amplitude);
114
    cv::magnitude(fourier[2], -fourier[3], amplitude);
114
 
115
 
115
    // Collected signal energy at higher frequencies
116
    // Collected signal energy at higher frequencies
116
    energy = cv::Mat(phase.rows, phase.cols, CV_32F, cv::Scalar(0.0));
117
    energy = cv::Mat(phase.rows, phase.cols, CV_32F, cv::Scalar(0.0));
117
 
118
 
118
    for(unsigned int i=0; i<frames.size()-1; i++){
119
    for(unsigned int i=0; i<frames.size()-1; i++){
119
        cv::Mat magnitude;
120
        cv::Mat magnitude;
120
        cv::magnitude(fourier[i*2 + 2], fourier[i*2 + 3], magnitude);
121
        cv::magnitude(fourier[i*2 + 2], fourier[i*2 + 3], magnitude);
121
        cv::add(energy, magnitude, energy, cv::noArray(), CV_32F);
122
        cv::add(energy, magnitude, energy, cv::noArray(), CV_32F);
122
    }
123
    }
123
 
124
 
124
    frames.clear();
125
    frames.clear();
125
}
126
}
126
 
127
 
127
void unWrapPhaseMap(const cv::Mat& phasePrimary,
128
void unWrapPhaseMap(const cv::Mat& phasePrimary,
128
                   const cv::Mat& phaseSecondary,
129
                   const cv::Mat& phaseSecondary,
129
                   cv::Mat& phase) {
130
                   cv::Mat& phase) {
130
    cv::Mat phaseEquivalent = phaseSecondary - phasePrimary;
131
    cv::Mat phaseEquivalent = phaseSecondary - phasePrimary;
131
    phaseEquivalent = cvtools::modulo(phaseEquivalent, 2.0*CV_PI);
132
    phaseEquivalent = cvtools::modulo(phaseEquivalent, 2.0*CV_PI);
132
    phase = unwrapWithCue(phasePrimary, phaseEquivalent, nPeriodsPrimary);
133
    phase = unwrapWithCue(phasePrimary, phaseEquivalent, nPeriodsPrimary);
133
    phase *= phasePrimary.cols/(2.0*CV_PI);
134
    phase *= phasePrimary.cols/(2.0*CV_PI);
134
}
135
}
135
 
136
 
136
 
137
 
137
void matchPhaseMaps(const cv::Mat& occlusion0, const cv::Mat& occlusion1,
138
void matchPhaseMaps(const cv::Mat& occlusion0, const cv::Mat& occlusion1,
138
                    const cv::Mat& phase0, const cv::Mat& phase1,
139
                    const cv::Mat& phase0, const cv::Mat& phase1,
139
                    std::vector<cv::Vec2f>& q0, std::vector<cv::Vec2f>& q1) {
140
                    std::vector<cv::Vec2f>& q0, std::vector<cv::Vec2f>& q1) {
140
 
141
 
141
    #pragma omp parallel for
142
    #pragma omp parallel for
142
    for(int row=0; row<occlusion0.rows; row++){
143
    for(int row=0; row<occlusion0.rows; row++){
143
        for(int col=0; col<occlusion0.cols; col++){
144
        for(int col=0; col<occlusion0.cols; col++){
144
 
145
 
145
            if(!occlusion0.at<char>(row,col))
146
            if(!occlusion0.at<char>(row,col))
146
                continue;
147
                continue;
147
 
148
 
148
            float phase0i = phase0.at<float>(row,col);
149
            float phase0i = phase0.at<float>(row,col);
149
            for(int col1=0; col1<phase1.cols-1; col1++){
150
            for(int col1=0; col1<phase1.cols-1; col1++){
150
 
151
 
151
                if(!occlusion1.at<char>(row,col1) || !occlusion1.at<char>(row,col1+1))
152
                if(!occlusion1.at<char>(row,col1) || !occlusion1.at<char>(row,col1+1))
152
                    continue;
153
                    continue;
153
 
154
 
154
                float phase1Left = phase1.at<float>(row,col1);
155
                float phase1Left = phase1.at<float>(row,col1);
155
                float phase1Right = phase1.at<float>(row,col1+1);
156
                float phase1Right = phase1.at<float>(row,col1+1);
156
 
157
 
157
                bool match = (phase1Left <= phase0i)
158
                bool match = (phase1Left <= phase0i)
158
                              && (phase0i <= phase1Right)
159
                              && (phase0i <= phase1Right)
159
                              && (phase0i-phase1Left < 1.0)
160
                              && (phase0i-phase1Left < 1.0)
160
                              && (phase1Right-phase0i < 1.0)
161
                              && (phase1Right-phase0i < 1.0)
161
                              && (phase1Right-phase1Left > 0.1);
162
                              && (phase1Right-phase1Left > 0.1);
162
 
163
 
163
                if(match){
164
                if(match){
164
 
165
 
165
                    float col1i = col1 + (phase0i-phase1Left)/(phase1Right-phase1Left);
166
                    float col1i = col1 + (phase0i-phase1Left)/(phase1Right-phase1Left);
166
 
167
 
167
                    #pragma omp critical
168
                    #pragma omp critical
168
                    {
169
                    {
169
                    q0.push_back(cv::Point2f(col, row));
170
                    q0.push_back(cv::Point2f(col, row));
170
                    q1.push_back(cv::Point2f(col1i, row));
171
                    q1.push_back(cv::Point2f(col1i, row));
171
                    }
172
                    }
172
                    break;
173
                    break;
173
                }
174
                }
174
            }
175
            }
175
        }
176
        }
176
    }
177
    }
177
 
178
 
178
}
179
}
179
 
180
 
180
void triangulate(const StereoRectifyier& stereoRect,
181
void triangulate(const StereoRectifyier& stereoRect,
181
                 const std::vector<cv::Vec2f>& q0,
182
                 const std::vector<cv::Vec2f>& q0,
182
                 const std::vector<cv::Vec2f>& q1,
183
                 const std::vector<cv::Vec2f>& q1,
183
                 std::vector<cv::Point3f>& Q) {
184
                 std::vector<cv::Point3f>& Q) {
184
 
185
 
185
    // cv::Mat QMatHomogenous, QMat;
186
    // cv::Mat QMatHomogenous, QMat;
186
    // cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
187
    // cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
187
    // cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
188
    // cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
188
 
189
 
189
    // // Undo rectification
190
    // // Undo rectification
190
    // cv::Mat R0Inv;
191
    // cv::Mat R0Inv;
191
    // cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
192
    // cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
192
    // QMat = R0Inv*QMat;
193
    // QMat = R0Inv*QMat;
193
 
194
 
194
    // cvtools::matToPoints3f(QMat, Q);
195
    // cvtools::matToPoints3f(QMat, Q);
195
 
196
 
196
 
197
 
197
    // Triangulate by means of disparity projection
198
    // Triangulate by means of disparity projection
198
    Q.resize(q0.size());
199
    Q.resize(q0.size());
199
    cv::Matx44f QRectx = cv::Matx44f(stereoRect.QRect);
200
    cv::Matx44f QRectx = cv::Matx44f(stereoRect.QRect);
200
    cv::Matx33f R0invx = cv::Matx33f(cv::Mat(stereoRect.R0.t()));
201
    cv::Matx33f R0invx = cv::Matx33f(cv::Mat(stereoRect.R0.t()));
201
 
202
 
202
    #pragma omp parallel for
203
    #pragma omp parallel for
203
    for(unsigned int i=0; i < q0.size(); i++){
204
    for(unsigned int i=0; i < q0.size(); i++){
204
        float disparity = q0[i][0] - q1[i][0];
205
        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
        cv::Vec4f Qih = QRectx*cv::Vec4f(q0[i][0], q0[i][1], disparity, 1.0);
206
        float winv = float(1.0) / Qih[3];
207
        float winv = float(1.0) / Qih[3];
207
        Q[i] = R0invx * cv::Point3f(Qih[0]*winv, Qih[1]*winv, Qih[2]*winv);
208
        Q[i] = R0invx * cv::Point3f(Qih[0]*winv, Qih[1]*winv, Qih[2]*winv);
208
    }
209
    }
209
}
210
}
210
 
211
 
211
void AlgorithmPhaseShiftTwoFreq::
212
void AlgorithmPhaseShiftTwoFreq::
212
    get3DPoints(const SMCalibrationParameters & calibration,
213
    get3DPoints(const SMCalibrationParameters & calibration,
213
                const std::vector<cv::Mat>& frames0,
214
                const std::vector<cv::Mat>& frames0,
214
                const std::vector<cv::Mat>& frames1,
215
                const std::vector<cv::Mat>& frames1,
215
                std::vector<cv::Point3f>& Q,
216
                std::vector<cv::Point3f>& Q,
216
                std::vector<cv::Vec3f>& color){
217
                std::vector<cv::Vec3f>& color){
217
 
218
 
218
    assert(frames0.size() == N);
219
    assert(frames0.size() == N);
219
    assert(frames1.size() == N);
220
    assert(frames1.size() == N);
220
 
221
 
221
    StereoRectifyier stereoRect;
222
    StereoRectifyier stereoRect;
222
    getStereoRectifier(calibration,
223
    getStereoRectifier(calibration,
223
                        cv::Size(frames0[0].cols, frames0[0].rows),
224
                        cv::Size(frames0[0].cols, frames0[0].rows),
224
                        stereoRect);
225
                        stereoRect);
225
 
226
 
226
    // // Erode occlusion masks
227
    // // Erode occlusion masks
227
    // cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
228
    // cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
228
 
229
 
229
    cv::Mat up0, up1;
230
    cv::Mat up0, up1;
230
    cv::Mat occlusion0, occlusion1;
231
    cv::Mat occlusion0, occlusion1;
231
    cv::Mat color0, color1;
232
    cv::Mat color0, color1;
232
 
233
 
233
    #pragma omp parallel sections
234
    #pragma omp parallel sections
234
    {
235
    {
235
        #pragma omp section
236
        #pragma omp section
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(frames0[i].depth() == CV_8U)
243
                if(frames0[i].depth() == CV_8U)
244
                    cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
244
                    cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
-
 
245
                else if(frames0[i].channels() == 3)
-
 
246
                    cv::cvtColor(frames0[i], temp, CV_RGB2GRAY);
245
                else
247
                else
246
                    temp = frames0[i];
248
                    temp = frames0[i];
-
 
249
 
247
                cv::remap(temp, frames0Rect[i],
250
                cv::remap(temp, frames0Rect[i],
248
                          stereoRect.map0X, stereoRect.map0Y,
251
                          stereoRect.map0X, stereoRect.map0Y,
249
                          CV_INTER_LINEAR);
252
                          CV_INTER_LINEAR);
250
            }
253
            }
251
 
254
 
-
 
255
            if(frames0[0].depth() == CV_8U) {
-
 
256
                cv::Mat temp;
-
 
257
                cv::cvtColor(frames0[0], temp, CV_BayerBG2RGB);
252
            // If images are HDR (float), we need to convert to uchar
258
                cv::remap(temp, color0, stereoRect.map0X, stereoRect.map0Y,
-
 
259
                          CV_INTER_LINEAR);
-
 
260
            }
-
 
261
            else if(frames0[0].channels() == 1) {
253
            frames0Rect[0].convertTo(color0, CV_32FC3);
262
                frames0Rect[0].convertTo(color0, CV_32FC3);
-
 
263
            }
-
 
264
            else {
-
 
265
                cv::remap(frames0[0], color0, stereoRect.map0X, stereoRect.map0Y,
-
 
266
                          CV_INTER_LINEAR);
-
 
267
            }
-
 
268
 
-
 
269
            cv::Mat tmp;
-
 
270
            color0.convertTo(tmp, CV_8U, 255.0);
-
 
271
            cv::imwrite("color0.jpg", tmp);
254
 
272
 
255
            // Occlusion masks
273
            // Occlusion masks
256
            cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0);
274
            cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0);
-
 
275
            if(frames0Rect[0].depth() == CV_8U)
257
            occlusion0 = (occlusion0 > 25) & (occlusion0 < 250);
276
                occlusion0 = (occlusion0 > 25) & (occlusion0 < 250);
-
 
277
            else
-
 
278
                occlusion0 = (occlusion0 > 25.0 / 255.0) & (occlusion0 < 250.0 / 255.0);
258
 
279
 
259
            // Decode camera0
280
            // Decode camera0
260
            std::vector<cv::Mat> frames0Primary(frames0Rect.begin()+2,
281
            std::vector<cv::Mat> frames0Primary(frames0Rect.begin()+2,
261
                                                frames0Rect.begin()+2+nStepsPrimary);
282
                                                frames0Rect.begin()+2+nStepsPrimary);
262
            std::vector<cv::Mat> frames0Secondary(frames0Rect.begin()+2+nStepsPrimary,
283
            std::vector<cv::Mat> frames0Secondary(frames0Rect.begin()+2+nStepsPrimary,
263
                                                  frames0Rect.end());
284
                                                  frames0Rect.end());
264
 
285
 
265
            frames0Rect.clear();
286
            frames0Rect.clear();
266
 
287
 
267
            cv::Mat amplitude0Primary, amplitude0Secondary;
288
            cv::Mat amplitude0Primary, amplitude0Secondary;
268
            cv::Mat up0Primary, up0Secondary;
289
            cv::Mat up0Primary, up0Secondary;
269
            cv::Mat energy0Primary, energy0Secondary;
290
            cv::Mat energy0Primary, energy0Secondary;
270
            determineAmplitudePhaseEnergy(frames0Primary,
291
            determineAmplitudePhaseEnergy(frames0Primary,
271
                                          amplitude0Primary,
292
                                          amplitude0Primary,
272
                                          up0Primary,
293
                                          up0Primary,
273
                                          energy0Primary);
294
                                          energy0Primary);
274
            determineAmplitudePhaseEnergy(frames0Secondary,
295
            determineAmplitudePhaseEnergy(frames0Secondary,
275
                                          amplitude0Secondary,
296
                                          amplitude0Secondary,
276
                                          up0Secondary,
297
                                          up0Secondary,
277
                                          energy0Secondary);
298
                                          energy0Secondary);
278
 
299
 
279
            unWrapPhaseMap(up0Primary, up0Secondary, up0);
300
            unWrapPhaseMap(up0Primary, up0Secondary, up0);
280
 
-
 
281
            // Threshold on energy at primary frequency
301
            // Threshold on energy at primary frequency
-
 
302
            if(frames0Rect[0].depth() == CV_8U)
282
            occlusion0 = occlusion0 & (amplitude0Primary > 5.0*nStepsPrimary);
303
                occlusion0 = occlusion0 & (amplitude0Primary > 5.0*nStepsPrimary);
-
 
304
            else
-
 
305
                occlusion0 = occlusion0 & (amplitude0Primary * 255.0 > 5.0*nStepsPrimary);
-
 
306
 
283
            // Threshold on energy ratios
307
            // Threshold on energy ratios
284
            occlusion0 = occlusion0 & (amplitude0Primary > 0.25*energy0Primary);
308
            occlusion0 = occlusion0 & (amplitude0Primary > 0.25*energy0Primary);
285
            occlusion0 = occlusion0 & (amplitude0Secondary > 0.25*energy0Secondary);
309
            occlusion0 = occlusion0 & (amplitude0Secondary > 0.25*energy0Secondary);
286
 
310
 
287
            // // Erode occlusion masks
311
            // // Erode occlusion masks
288
            // cv::erode(occlusion0, occlusion0, strel);
312
            // cv::erode(occlusion0, occlusion0, strel);
289
 
313
 
290
            // Threshold on vertical gradient of phase
314
            // Threshold on vertical gradient of phase
291
            cv::Mat edges0;
315
            cv::Mat edges0;
292
            cv::Sobel(up0, edges0, -1, 1, 1, 5);
316
            cv::Sobel(up0, edges0, -1, 1, 1, 5);
293
            occlusion0 = occlusion0 & (abs(edges0) < 10);
317
            occlusion0 = occlusion0 & (abs(edges0) < 10);
294
 
318
 
295
            #ifdef SM_DEBUG
319
            #ifdef SM_DEBUG
-
 
320
                cv::Mat tmp;
-
 
321
                double min, max;
-
 
322
                cv::minMaxLoc(up0Primary, &min, &max);
-
 
323
                up0Primary.convertTo(tmp, CV_8U, 255.0 / (max - min), -min * 255.0 / (max - min));
-
 
324
                cv::imwrite("up0Primary.jpg", tmp & occlusion0);
-
 
325
                cv::minMaxLoc(up0Secondary, &min, &max);
-
 
326
                up0Secondary.convertTo(tmp, CV_8U, 255.0 / (max - min), -min * 255.0 / (max - min));
-
 
327
                cv::imwrite("up0Secondary.jpg", tmp & occlusion0);
-
 
328
                cv::minMaxLoc(up0, &min, &max);
-
 
329
                up0.convertTo(tmp, CV_8U, 255.0 / (max - min),- min * 255.0 / (max - min));
-
 
330
                cv::imwrite("up0.jpg", tmp & occlusion0);
-
 
331
                cv::minMaxLoc(amplitude0Primary, &min, &max);
-
 
332
                amplitude0Primary.convertTo(tmp, CV_8U, 255.0 / (max - min), -min * 255.0 / (max - min));
-
 
333
                cv::imwrite("amplitude0Primary.jpg", tmp & occlusion0);
-
 
334
                cv::minMaxLoc(amplitude0Secondary, &min, &max);
-
 
335
                amplitude0Secondary.convertTo(tmp, CV_8U, 255.0 / (max - min), -min * 255.0 / (max - min));
-
 
336
                cv::imwrite("amplitude0Secondary.jpg", tmp & occlusion0);
-
 
337
                cv::minMaxLoc(energy0Primary, &min, &max);
-
 
338
                energy0Primary.convertTo(tmp, CV_8U, 255.0 / (max - min),- min * 255.0 / (max - min));
-
 
339
                cv::imwrite("energy0Primary.jpg", tmp & occlusion0);
-
 
340
                cv::minMaxLoc(energy0Secondary, &min, &max);
-
 
341
                energy0Secondary.convertTo(tmp, CV_8U, 255.0 / (max - min), -min * 255.0 / (max - min));
-
 
342
                cv::imwrite("energy0Secondary.jpg", tmp & occlusion0);
-
 
343
 
296
                cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
344
                cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
297
                cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
345
                cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
298
                cvtools::writeMat(up0, "up0.mat", "up0");
346
                cvtools::writeMat(up0, "up0.mat", "up0");
299
                cvtools::writeMat(amplitude0Primary,
347
                cvtools::writeMat(amplitude0Primary,
300
                                  "amplitude0Primary.mat", "amplitude0Primary");
348
                                  "amplitude0Primary.mat", "amplitude0Primary");
301
                cvtools::writeMat(amplitude0Secondary,
349
                cvtools::writeMat(amplitude0Secondary,
302
                                  "amplitude0Secondary.mat", "amplitude0Secondary");
350
                                  "amplitude0Secondary.mat", "amplitude0Secondary");
303
                cvtools::writeMat(energy0Primary,
351
                cvtools::writeMat(energy0Primary,
304
                                  "energy0Primary.mat", "energy0Primary");
352
                                  "energy0Primary.mat", "energy0Primary");
305
                cvtools::writeMat(energy0Secondary,
353
                cvtools::writeMat(energy0Secondary,
306
                                  "energy0Secondary.mat", "energy0Secondary");
354
                                  "energy0Secondary.mat", "energy0Secondary");
307
                cvtools::writeMat(edges0, "edges0.mat", "edges0");
355
                cvtools::writeMat(edges0, "edges0.mat", "edges0");
308
                cvtools::writeMat(occlusion0, "occlusion0.mat", "occlusion0");
356
                cvtools::writeMat(occlusion0, "occlusion0.mat", "occlusion0");
309
                cvtools::writeMat(color0, "color0.mat", "color0");
357
                cvtools::writeMat(color0, "color0.mat", "color0");
310
            #endif
358
            #endif
311
 
359
 
312
        }
360
        }
313
        #pragma omp section
361
        #pragma omp section
314
        {
362
        {
315
 
363
 
316
            // Gray-scale and remap
364
            // Gray-scale and remap
317
            std::vector<cv::Mat> frames1Rect(N);
365
            std::vector<cv::Mat> frames1Rect(N);
318
 
366
 
319
            for(unsigned int i=0; i<N; i++){
367
            for(unsigned int i=0; i<N; i++){
320
                cv::Mat temp;
368
                cv::Mat temp;
321
                if(frames1[i].depth() == CV_8U)
369
                if(frames1[i].depth() == CV_8U)
322
                    cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
370
                    cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
-
 
371
                else if(frames1[i].channels() == 3)
-
 
372
                    cv::cvtColor(frames1[i], temp, CV_RGB2GRAY);
323
                else
373
                else
324
                    temp = frames1[i];
374
                    temp = frames1[i];
325
                cv::remap(temp, frames1Rect[i],
375
                cv::remap(temp, frames1Rect[i],
326
                          stereoRect.map1X, stereoRect.map1Y,
376
                          stereoRect.map1X, stereoRect.map1Y,
327
                          CV_INTER_LINEAR);
377
                          CV_INTER_LINEAR);
328
            }
378
            }
329
 
379
 
-
 
380
            if(frames1[0].depth() == CV_8U) {
-
 
381
                cv::Mat temp;
-
 
382
                cv::cvtColor(frames1[0], temp, CV_BayerBG2RGB);
330
            // If images are HDR (float), we need to convert to uchar
383
                cv::remap(temp, color1, stereoRect.map1X, stereoRect.map1Y,
-
 
384
                          CV_INTER_LINEAR);
-
 
385
            }
-
 
386
            else if(frames1[0].channels() == 1) {
331
            frames1Rect[0].convertTo(color1, CV_32FC3);
387
                frames1Rect[0].convertTo(color1, CV_32FC3);
-
 
388
            }
-
 
389
            else {
-
 
390
                cv::remap(frames1[0], color1, stereoRect.map1X, stereoRect.map1Y,
-
 
391
                          CV_INTER_LINEAR);
-
 
392
            }
332
 
393
 
333
            // Occlusion masks
394
            // Occlusion masks
334
            cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1);
395
            cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1);
-
 
396
            if(frames1Rect[0].depth() == CV_8U)
335
            occlusion1 = (occlusion1 > 25) & (occlusion1 < 250);
397
                occlusion1 = (occlusion1 > 25) & (occlusion1 < 250);
-
 
398
            else
-
 
399
                occlusion1 = (occlusion1 > 25.0 / 255.0) & (occlusion1 < 250.0 / 255.0);
336
 
400
 
337
            // Decode camera1
401
            // Decode camera1
338
            std::vector<cv::Mat> frames1Primary(frames1Rect.begin()+2,
402
            std::vector<cv::Mat> frames1Primary(frames1Rect.begin()+2,
339
                                                frames1Rect.begin()+2+nStepsPrimary);
403
                                                frames1Rect.begin()+2+nStepsPrimary);
340
            std::vector<cv::Mat> frames1Secondary(frames1Rect.begin()+2+nStepsPrimary,
404
            std::vector<cv::Mat> frames1Secondary(frames1Rect.begin()+2+nStepsPrimary,
341
                                                  frames1Rect.end());
405
                                                  frames1Rect.end());
342
 
406
 
343
            frames1Rect.clear();
407
            frames1Rect.clear();
344
 
408
 
345
            cv::Mat amplitude1Primary, amplitude1Secondary;
409
            cv::Mat amplitude1Primary, amplitude1Secondary;
346
            cv::Mat up1Primary, up1Secondary;
410
            cv::Mat up1Primary, up1Secondary;
347
            cv::Mat energy1Primary, energy1Secondary;
411
            cv::Mat energy1Primary, energy1Secondary;
348
            determineAmplitudePhaseEnergy(frames1Primary,
412
            determineAmplitudePhaseEnergy(frames1Primary,
349
                                          amplitude1Primary,
413
                                          amplitude1Primary,
350
                                          up1Primary,
414
                                          up1Primary,
351
                                          energy1Primary);
415
                                          energy1Primary);
352
            determineAmplitudePhaseEnergy(frames1Secondary,
416
            determineAmplitudePhaseEnergy(frames1Secondary,
353
                                          amplitude1Secondary,
417
                                          amplitude1Secondary,
354
                                          up1Secondary,
418
                                          up1Secondary,
355
                                          energy1Secondary);
419
                                          energy1Secondary);
356
 
420
 
357
            unWrapPhaseMap(up1Primary, up1Secondary, up1);
421
            unWrapPhaseMap(up1Primary, up1Secondary, up1);
358
 
422
 
359
            // Threshold on energy at primary frequency
423
            // Threshold on energy at primary frequency
-
 
424
            if(frames1Rect[0].depth() == CV_8U)
360
            occlusion1 = occlusion1 & (amplitude1Primary > 5.0*nStepsPrimary);
425
                occlusion1 = occlusion1 & (amplitude1Primary > 5.0*nStepsPrimary);
-
 
426
            else
-
 
427
                occlusion1 = occlusion1 & (amplitude1Primary * 255.0 > 5.0*nStepsPrimary);
-
 
428
 
361
            // Threshold on energy ratios
429
            // Threshold on energy ratios
362
            occlusion1 = occlusion1 & (amplitude1Primary > 0.25*energy1Primary);
430
            occlusion1 = occlusion1 & (amplitude1Primary > 0.25*energy1Primary);
363
            occlusion1 = occlusion1 & (amplitude1Secondary > 0.25*energy1Secondary);
431
            occlusion1 = occlusion1 & (amplitude1Secondary > 0.25*energy1Secondary);
364
 
432
 
365
            // // Erode occlusion masks
433
            // // Erode occlusion masks
366
            // cv::erode(occlusion1, occlusion1, strel);
434
            // cv::erode(occlusion1, occlusion1, strel);
367
 
435
 
368
 
436
 
369
            // Threshold on vertical gradient of phase
437
            // Threshold on vertical gradient of phase
370
            cv::Mat edges1;
438
            cv::Mat edges1;
371
            cv::Sobel(up1, edges1, -1, 1, 1, 5);
439
            cv::Sobel(up1, edges1, -1, 1, 1, 5);
372
            occlusion1 = occlusion1 & (abs(edges1) < 10);
440
            occlusion1 = occlusion1 & (abs(edges1) < 10);
373
 
441
 
374
            #ifdef SM_DEBUG
442
            #ifdef SM_DEBUG
-
 
443
                cv::Mat tmp;
-
 
444
                double min, max;
-
 
445
                cv::minMaxLoc(up1Primary, &min, &max);
-
 
446
                up1Primary.convertTo(tmp, CV_8U, 255.0 / (max - min), -min * 255.0 / (max - min));
-
 
447
                cv::imwrite("up1Primary.jpg", tmp & occlusion1);
-
 
448
                cv::minMaxLoc(up1Secondary, &min, &max);
-
 
449
                up1Secondary.convertTo(tmp, CV_8U, 255.0 / (max - min), -min * 255.0 / (max - min));
-
 
450
                cv::imwrite("up1Secondary.jpg", tmp & occlusion1);
-
 
451
                cv::minMaxLoc(up1, &min, &max);
-
 
452
                up1.convertTo(tmp, CV_8U, 255.0 / (max - min),- min * 255.0 / (max - min));
-
 
453
                cv::imwrite("up1.jpg", tmp & occlusion1);
-
 
454
                cv::minMaxLoc(amplitude1Primary, &min, &max);
-
 
455
                amplitude1Primary.convertTo(tmp, CV_8U, 255.0 / (max - min), -min * 255.0 / (max - min));
-
 
456
                cv::imwrite("amplitude1Primary.jpg", tmp & occlusion1);
-
 
457
                cv::minMaxLoc(amplitude1Secondary, &min, &max);
-
 
458
                amplitude1Secondary.convertTo(tmp, CV_8U, 255.0 / (max - min), -min * 255.0 / (max - min));
-
 
459
                cv::imwrite("amplitude1Secondary.jpg", tmp & occlusion1);
-
 
460
                cv::minMaxLoc(energy1Primary, &min, &max);
-
 
461
                energy1Primary.convertTo(tmp, CV_8U, 255.0 / (max - min),- min * 255.0 / (max - min));
-
 
462
                cv::imwrite("energy1Primary.jpg", tmp & occlusion1);
-
 
463
                cv::minMaxLoc(energy1Secondary, &min, &max);
-
 
464
                energy1Secondary.convertTo(tmp, CV_8U, 255.0 / (max - min), -min * 255.0 / (max - min));
-
 
465
                cv::imwrite("energy1Secondary.jpg", tmp & occlusion1);
-
 
466
 
375
                cvtools::writeMat(up1Primary, "up1Primary.mat", "up1Primary");
467
                cvtools::writeMat(up1Primary, "up1Primary.mat", "up1Primary");
376
                cvtools::writeMat(up1Secondary, "up1Secondary.mat", "up1Secondary");
468
                cvtools::writeMat(up1Secondary, "up1Secondary.mat", "up1Secondary");
377
                cvtools::writeMat(up1, "up1.mat", "up1");
469
                cvtools::writeMat(up1, "up1.mat", "up1");
378
                cvtools::writeMat(amplitude1Primary,
470
                cvtools::writeMat(amplitude1Primary,
379
                                  "amplitude1Primary.mat", "amplitude1Primary");
471
                                  "amplitude1Primary.mat", "amplitude1Primary");
380
                cvtools::writeMat(amplitude1Secondary,
472
                cvtools::writeMat(amplitude1Secondary,
381
                                  "amplitude1Secondary.mat", "amplitude1Secondary");
473
                                  "amplitude1Secondary.mat", "amplitude1Secondary");
382
                cvtools::writeMat(energy1Primary,
474
                cvtools::writeMat(energy1Primary,
383
                                  "energy1Primary.mat", "energy1Primary");
475
                                  "energy1Primary.mat", "energy1Primary");
384
                cvtools::writeMat(energy1Secondary,
476
                cvtools::writeMat(energy1Secondary,
385
                                  "energy1Secondary.mat", "energy1Secondary");
477
                                  "energy1Secondary.mat", "energy1Secondary");
386
                cvtools::writeMat(edges1, "edges1.mat", "edges1");
478
                cvtools::writeMat(edges1, "edges1.mat", "edges1");
387
                cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
479
                cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
388
                cvtools::writeMat(color1, "color1.mat", "color1");
480
                cvtools::writeMat(color1, "color1.mat", "color1");
389
            #endif
481
            #endif
390
 
482
 
391
        }
483
        }
392
    }
484
    }
393
 
485
 
394
 
486
 
395
    // Match phase maps
487
    // Match phase maps
396
 
488
 
397
    // camera0 against camera1
489
    // camera0 against camera1
398
    std::vector<cv::Vec2f> q0, q1;
490
    std::vector<cv::Vec2f> q0, q1;
399
    matchPhaseMaps(occlusion0, occlusion1, up0, up1, q0, q1);
491
    matchPhaseMaps(occlusion0, occlusion1, up0, up1, q0, q1);
400
 
492
 
401
    size_t nMatches = q0.size();
493
    size_t nMatches = q0.size();
402
 
494
 
403
    if(nMatches < 1){
495
    if(nMatches < 1){
404
        Q.resize(0);
496
        Q.resize(0);
405
        color.resize(0);
497
        color.resize(0);
406
 
498
 
407
        return;
499
        return;
408
    }
500
    }
409
    else {
501
    else {
410
        // Retrieve color information
502
        // Retrieve color information
411
        color.resize(nMatches);
503
        color.resize(nMatches);
412
        for(unsigned int i=0; i<nMatches; i++){
504
        for(unsigned int i=0; i<nMatches; i++){
413
 
505
 
414
            cv::Vec3f c0 = color0.at<cv::Vec3f>(int(q0[i][1]), int(q0[i][0]));
506
            cv::Vec3f c0 = color0.at<cv::Vec3f>(int(q0[i][1]), int(q0[i][0]));
415
            cv::Vec3f c1 = color1.at<cv::Vec3f>(int(q1[i][1]), int(q1[i][0]));
507
            cv::Vec3f c1 = color1.at<cv::Vec3f>(int(q1[i][1]), int(q1[i][0]));
416
 
508
 
417
            color[i] = 0.5*c0 + 0.5*c1;
509
            color[i] = (0.5*c0 + 0.5*c1);
418
        }
510
        }
419
    }
511
    }
420
 
512
 
421
    // Triangulate points
513
    // Triangulate points
422
    triangulate(stereoRect, q0, q1, Q);
514
    triangulate(stereoRect, q0, q1, Q);
423
 
515
 
424
}
516
}
425
 
517
 
426
 
518