Subversion Repositories seema-scanner

Rev

Rev 251 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
181 jakw 1
//
2
// Two Frequency Phase Shifting using the Heterodyne Principle
3
//
234 sorgre 4
// This implementation follows "Reich, Ritter, Thesing, White light heterodyne
5
// principle for 3D-measurement", SPIE (1997).
6
//
181 jakw 7
// Different from the paper, it uses only two different frequencies.
8
//
234 sorgre 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
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
13
// energy at non-primary frequencies.
181 jakw 14
//
15
 
128 jakw 16
#include "AlgorithmPhaseShiftTwoFreq.h"
4 jakw 17
#include <math.h>
18
 
19
#include "cvtools.h"
182 jakw 20
#include "algorithmtools.h"
4 jakw 21
 
231 jakw 22
#include <omp.h>
23
 
143 jakw 24
static unsigned int nStepsPrimary = 16; // number of shifts/steps in primary
25
static unsigned int nStepsSecondary = 8; // number of shifts/steps in secondary
190 jakw 26
static float nPeriodsPrimary = 40; // primary period
4 jakw 27
 
234 sorgre 28
AlgorithmPhaseShiftTwoFreq::AlgorithmPhaseShiftTwoFreq(unsigned int _screenCols,
29
                                                       unsigned int _screenRows)
30
    : Algorithm(_screenCols, _screenRows) {
4 jakw 31
 
72 jakw 32
    // Set N
118 jakw 33
    N = 2+nStepsPrimary+nStepsSecondary;
72 jakw 34
 
190 jakw 35
    // Determine the secondary (wider) period to fulfill the heterodyne condition
36
    float nPeriodsSecondary = nPeriodsPrimary + 1;
74 jakw 37
 
70 jakw 38
    // all on pattern
39
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
40
    patterns.push_back(allOn);
41
 
42
    // all off pattern
43
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
44
    patterns.push_back(allOff);
45
 
74 jakw 46
    // Primary encoding patterns
118 jakw 47
    for(unsigned int i=0; i<nStepsPrimary; i++){
192 jakw 48
        float phase = 2.0*CV_PI/nStepsPrimary * i;
190 jakw 49
        float pitch = screenCols/nPeriodsPrimary;
70 jakw 50
        cv::Mat patternI(1,1,CV_8U);
51
        patternI = computePhaseVector(screenCols, phase, pitch);
52
        patterns.push_back(patternI.t());
53
    }
4 jakw 54
 
74 jakw 55
    // Secondary encoding patterns
118 jakw 56
    for(unsigned int i=0; i<nStepsSecondary; i++){
192 jakw 57
        float phase = 2.0*CV_PI/nStepsSecondary * i;
190 jakw 58
        float pitch = screenCols/nPeriodsSecondary;
72 jakw 59
        cv::Mat patternI(1,1,CV_8U);
70 jakw 60
        patternI = computePhaseVector(screenCols, phase, pitch);
61
        patterns.push_back(patternI.t());
4 jakw 62
    }
63
 
72 jakw 64
 
4 jakw 65
}
66
 
128 jakw 67
cv::Mat AlgorithmPhaseShiftTwoFreq::getEncodingPattern(unsigned int depth){
4 jakw 68
    return patterns[depth];
69
}
70
 
233 - 71
 
72
struct StereoRectifyier {
73
    cv::Mat map0X, map0Y, map1X, map1Y;
74
    cv::Mat R0, R1, P0, P1, QRect;
75
};
244 jakw 76
void getStereoRectifier(const SMCalibrationParameters &calibration,
77
                         const cv::Size& frameSize,
78
                         StereoRectifyier& stereoRect){
233 - 79
 
244 jakw 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);
233 - 84
 
244 jakw 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
 
103
void determineAmplitudePhaseEnergy(std::vector<cv::Mat>& frames,
104
                                   cv::Mat& amplitude,
105
                                   cv::Mat& phase,
106
                                   cv::Mat& energy) {
107
 
108
    std::vector<cv::Mat> fourier = getDFTComponents(frames);
109
 
257 - 110
 
244 jakw 111
    cv::phase(fourier[2], -fourier[3], phase);
112
 
113
    // Signal energy at unit frequency
114
    cv::magnitude(fourier[2], -fourier[3], amplitude);
115
 
116
    // Collected signal energy at higher frequencies
117
    energy = cv::Mat(phase.rows, phase.cols, CV_32F, cv::Scalar(0.0));
118
 
119
    for(unsigned int i=0; i<frames.size()-1; i++){
120
        cv::Mat magnitude;
121
        cv::magnitude(fourier[i*2 + 2], fourier[i*2 + 3], magnitude);
122
        cv::add(energy, magnitude, energy, cv::noArray(), CV_32F);
123
    }
124
 
125
    frames.clear();
126
}
127
 
128
void unWrapPhaseMap(const cv::Mat& phasePrimary,
129
                   const cv::Mat& phaseSecondary,
130
                   cv::Mat& phase) {
131
    cv::Mat phaseEquivalent = phaseSecondary - phasePrimary;
132
    phaseEquivalent = cvtools::modulo(phaseEquivalent, 2.0*CV_PI);
133
    phase = unwrapWithCue(phasePrimary, phaseEquivalent, nPeriodsPrimary);
134
    phase *= phasePrimary.cols/(2.0*CV_PI);
135
}
136
 
137
 
138
void matchPhaseMaps(const cv::Mat& occlusion0, const cv::Mat& occlusion1,
139
                    const cv::Mat& phase0, const cv::Mat& phase1,
140
                    std::vector<cv::Vec2f>& q0, std::vector<cv::Vec2f>& q1) {
141
 
142
    #pragma omp parallel for
143
    for(int row=0; row<occlusion0.rows; row++){
144
        for(int col=0; col<occlusion0.cols; col++){
145
 
146
            if(!occlusion0.at<char>(row,col))
147
                continue;
148
 
149
            float phase0i = phase0.at<float>(row,col);
150
            for(int col1=0; col1<phase1.cols-1; col1++){
151
 
152
                if(!occlusion1.at<char>(row,col1) || !occlusion1.at<char>(row,col1+1))
153
                    continue;
154
 
155
                float phase1Left = phase1.at<float>(row,col1);
156
                float phase1Right = phase1.at<float>(row,col1+1);
157
 
158
                bool match = (phase1Left <= phase0i)
159
                              && (phase0i <= phase1Right)
160
                              && (phase0i-phase1Left < 1.0)
161
                              && (phase1Right-phase0i < 1.0)
162
                              && (phase1Right-phase1Left > 0.1);
163
 
164
                if(match){
165
 
166
                    float col1i = col1 + (phase0i-phase1Left)/(phase1Right-phase1Left);
167
 
168
                    #pragma omp critical
169
                    {
170
                    q0.push_back(cv::Point2f(col, row));
171
                    q1.push_back(cv::Point2f(col1i, row));
172
                    }
173
                    break;
174
                }
175
            }
176
        }
177
    }
178
 
179
}
180
 
181
void triangulate(const StereoRectifyier& stereoRect,
182
                 const std::vector<cv::Vec2f>& q0,
183
                 const std::vector<cv::Vec2f>& q1,
184
                 std::vector<cv::Point3f>& Q) {
185
 
186
    // cv::Mat QMatHomogenous, QMat;
187
    // cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
188
    // cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
189
 
190
    // // Undo rectification
191
    // cv::Mat R0Inv;
192
    // cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
193
    // QMat = R0Inv*QMat;
194
 
195
    // cvtools::matToPoints3f(QMat, Q);
196
 
197
 
198
    // Triangulate by means of disparity projection
199
    Q.resize(q0.size());
200
    cv::Matx44f QRectx = cv::Matx44f(stereoRect.QRect);
201
    cv::Matx33f R0invx = cv::Matx33f(cv::Mat(stereoRect.R0.t()));
202
 
203
    #pragma omp parallel for
204
    for(unsigned int i=0; i < q0.size(); i++){
205
        float disparity = q0[i][0] - q1[i][0];
206
        cv::Vec4f Qih = QRectx*cv::Vec4f(q0[i][0], q0[i][1], disparity, 1.0);
207
        float winv = float(1.0) / Qih[3];
208
        Q[i] = R0invx * cv::Point3f(Qih[0]*winv, Qih[1]*winv, Qih[2]*winv);
209
    }
210
}
211
 
234 sorgre 212
void AlgorithmPhaseShiftTwoFreq::
213
    get3DPoints(const SMCalibrationParameters & calibration,
214
                const std::vector<cv::Mat>& frames0,
215
                const std::vector<cv::Mat>& frames1,
216
                std::vector<cv::Point3f>& Q,
247 jakw 217
                std::vector<cv::Vec3f>& color){
4 jakw 218
 
70 jakw 219
    assert(frames0.size() == N);
220
    assert(frames1.size() == N);
221
 
233 - 222
    StereoRectifyier stereoRect;
244 jakw 223
    getStereoRectifier(calibration,
234 sorgre 224
                        cv::Size(frames0[0].cols, frames0[0].rows),
225
                        stereoRect);
231 jakw 226
 
233 - 227
    // // Erode occlusion masks
228
    // cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
229
 
230
    cv::Mat up0, up1;
231
    cv::Mat occlusion0, occlusion1;
232
    cv::Mat color0, color1;
233
 
234
    #pragma omp parallel sections
231 jakw 235
    {
233 - 236
        #pragma omp section
237
        {
238
            // Gray-scale and remap/rectify
239
            std::vector<cv::Mat> frames0Rect(N);
178 jakw 240
 
233 - 241
            for(unsigned int i=0; i<N; i++){
242
                cv::Mat temp;
251 - 243
                if(frames0[i].depth() == CV_8U)
244
                    cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
257 - 245
                else if(frames0[i].channels() == 3)
246
                    cv::cvtColor(frames0[i], temp, CV_RGB2GRAY);
244 jakw 247
                else
251 - 248
                    temp = frames0[i];
257 - 249
 
234 sorgre 250
                cv::remap(temp, frames0Rect[i],
251
                          stereoRect.map0X, stereoRect.map0Y,
252
                          CV_INTER_LINEAR);
233 - 253
            }
207 flgw 254
 
257 - 255
            if(frames0[0].depth() == CV_8U) {
256
                cv::Mat temp;
257
                cv::cvtColor(frames0[0], temp, CV_BayerBG2RGB);
258
                cv::remap(temp, color0, stereoRect.map0X, stereoRect.map0Y,
259
                          CV_INTER_LINEAR);
260
            }
261
            else if(frames0[0].channels() == 1) {
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
            }
234 sorgre 268
 
257 - 269
            cv::Mat tmp;
270
            color0.convertTo(tmp, CV_8U, 255.0);
271
            cv::imwrite("color0.jpg", tmp);
272
 
233 - 273
            // Occlusion masks
274
            cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0);
257 - 275
            if(frames0Rect[0].depth() == CV_8U)
276
                occlusion0 = (occlusion0 > 25) & (occlusion0 < 250);
277
            else
278
                occlusion0 = (occlusion0 > 25.0 / 255.0) & (occlusion0 < 250.0 / 255.0);
182 jakw 279
 
233 - 280
            // Decode camera0
234 sorgre 281
            std::vector<cv::Mat> frames0Primary(frames0Rect.begin()+2,
282
                                                frames0Rect.begin()+2+nStepsPrimary);
283
            std::vector<cv::Mat> frames0Secondary(frames0Rect.begin()+2+nStepsPrimary,
284
                                                  frames0Rect.end());
70 jakw 285
 
233 - 286
            frames0Rect.clear();
182 jakw 287
 
233 - 288
            cv::Mat amplitude0Primary, amplitude0Secondary;
289
            cv::Mat up0Primary, up0Secondary;
290
            cv::Mat energy0Primary, energy0Secondary;
291
            determineAmplitudePhaseEnergy(frames0Primary,
234 sorgre 292
                                          amplitude0Primary,
293
                                          up0Primary,
294
                                          energy0Primary);
233 - 295
            determineAmplitudePhaseEnergy(frames0Secondary,
234 sorgre 296
                                          amplitude0Secondary,
297
                                          up0Secondary,
298
                                          energy0Secondary);
207 flgw 299
 
244 jakw 300
            unWrapPhaseMap(up0Primary, up0Secondary, up0);
257 - 301
            // Threshold on energy at primary frequency
302
            if(frames0Rect[0].depth() == CV_8U)
303
                occlusion0 = occlusion0 & (amplitude0Primary > 5.0*nStepsPrimary);
304
            else
305
                occlusion0 = occlusion0 & (amplitude0Primary * 255.0 > 5.0*nStepsPrimary);
231 jakw 306
 
233 - 307
            // Threshold on energy ratios
308
            occlusion0 = occlusion0 & (amplitude0Primary > 0.25*energy0Primary);
309
            occlusion0 = occlusion0 & (amplitude0Secondary > 0.25*energy0Secondary);
231 jakw 310
 
233 - 311
            // // Erode occlusion masks
312
            // cv::erode(occlusion0, occlusion0, strel);
231 jakw 313
 
244 jakw 314
            // Threshold on vertical gradient of phase
233 - 315
            cv::Mat edges0;
316
            cv::Sobel(up0, edges0, -1, 1, 1, 5);
317
            occlusion0 = occlusion0 & (abs(edges0) < 10);
182 jakw 318
 
233 - 319
            #ifdef SM_DEBUG
257 - 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
 
233 - 344
                cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
345
                cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
346
                cvtools::writeMat(up0, "up0.mat", "up0");
234 sorgre 347
                cvtools::writeMat(amplitude0Primary,
348
                                  "amplitude0Primary.mat", "amplitude0Primary");
349
                cvtools::writeMat(amplitude0Secondary,
350
                                  "amplitude0Secondary.mat", "amplitude0Secondary");
351
                cvtools::writeMat(energy0Primary,
352
                                  "energy0Primary.mat", "energy0Primary");
353
                cvtools::writeMat(energy0Secondary,
354
                                  "energy0Secondary.mat", "energy0Secondary");
233 - 355
                cvtools::writeMat(edges0, "edges0.mat", "edges0");
356
                cvtools::writeMat(occlusion0, "occlusion0.mat", "occlusion0");
234 sorgre 357
                cvtools::writeMat(color0, "color0.mat", "color0");
233 - 358
            #endif
182 jakw 359
 
233 - 360
        }
361
        #pragma omp section
362
        {
70 jakw 363
 
233 - 364
            // Gray-scale and remap
365
            std::vector<cv::Mat> frames1Rect(N);
200 jakw 366
 
233 - 367
            for(unsigned int i=0; i<N; i++){
368
                cv::Mat temp;
244 jakw 369
                if(frames1[i].depth() == CV_8U)
370
                    cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
257 - 371
                else if(frames1[i].channels() == 3)
372
                    cv::cvtColor(frames1[i], temp, CV_RGB2GRAY);
244 jakw 373
                else
374
                    temp = frames1[i];
234 sorgre 375
                cv::remap(temp, frames1Rect[i],
376
                          stereoRect.map1X, stereoRect.map1Y,
377
                          CV_INTER_LINEAR);
233 - 378
            }
182 jakw 379
 
257 - 380
            if(frames1[0].depth() == CV_8U) {
381
                cv::Mat temp;
382
                cv::cvtColor(frames1[0], temp, CV_BayerBG2RGB);
383
                cv::remap(temp, color1, stereoRect.map1X, stereoRect.map1Y,
384
                          CV_INTER_LINEAR);
385
            }
386
            else if(frames1[0].channels() == 1) {
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
            }
234 sorgre 393
 
233 - 394
            // Occlusion masks
395
            cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1);
257 - 396
            if(frames1Rect[0].depth() == CV_8U)
397
                occlusion1 = (occlusion1 > 25) & (occlusion1 < 250);
398
            else
399
                occlusion1 = (occlusion1 > 25.0 / 255.0) & (occlusion1 < 250.0 / 255.0);
233 - 400
 
401
            // Decode camera1
234 sorgre 402
            std::vector<cv::Mat> frames1Primary(frames1Rect.begin()+2,
403
                                                frames1Rect.begin()+2+nStepsPrimary);
404
            std::vector<cv::Mat> frames1Secondary(frames1Rect.begin()+2+nStepsPrimary,
405
                                                  frames1Rect.end());
233 - 406
 
407
            frames1Rect.clear();
408
 
409
            cv::Mat amplitude1Primary, amplitude1Secondary;
410
            cv::Mat up1Primary, up1Secondary;
411
            cv::Mat energy1Primary, energy1Secondary;
412
            determineAmplitudePhaseEnergy(frames1Primary,
234 sorgre 413
                                          amplitude1Primary,
414
                                          up1Primary,
415
                                          energy1Primary);
233 - 416
            determineAmplitudePhaseEnergy(frames1Secondary,
234 sorgre 417
                                          amplitude1Secondary,
418
                                          up1Secondary,
419
                                          energy1Secondary);
233 - 420
 
244 jakw 421
            unWrapPhaseMap(up1Primary, up1Secondary, up1);
233 - 422
 
423
            // Threshold on energy at primary frequency
257 - 424
            if(frames1Rect[0].depth() == CV_8U)
425
                occlusion1 = occlusion1 & (amplitude1Primary > 5.0*nStepsPrimary);
426
            else
427
                occlusion1 = occlusion1 & (amplitude1Primary * 255.0 > 5.0*nStepsPrimary);
428
 
233 - 429
            // Threshold on energy ratios
430
            occlusion1 = occlusion1 & (amplitude1Primary > 0.25*energy1Primary);
431
            occlusion1 = occlusion1 & (amplitude1Secondary > 0.25*energy1Secondary);
432
 
433
            // // Erode occlusion masks
434
            // cv::erode(occlusion1, occlusion1, strel);
435
 
436
 
244 jakw 437
            // Threshold on vertical gradient of phase
233 - 438
            cv::Mat edges1;
439
            cv::Sobel(up1, edges1, -1, 1, 1, 5);
440
            occlusion1 = occlusion1 & (abs(edges1) < 10);
441
 
442
            #ifdef SM_DEBUG
257 - 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
 
233 - 467
                cvtools::writeMat(up1Primary, "up1Primary.mat", "up1Primary");
468
                cvtools::writeMat(up1Secondary, "up1Secondary.mat", "up1Secondary");
469
                cvtools::writeMat(up1, "up1.mat", "up1");
234 sorgre 470
                cvtools::writeMat(amplitude1Primary,
471
                                  "amplitude1Primary.mat", "amplitude1Primary");
472
                cvtools::writeMat(amplitude1Secondary,
473
                                  "amplitude1Secondary.mat", "amplitude1Secondary");
474
                cvtools::writeMat(energy1Primary,
475
                                  "energy1Primary.mat", "energy1Primary");
476
                cvtools::writeMat(energy1Secondary,
477
                                  "energy1Secondary.mat", "energy1Secondary");
233 - 478
                cvtools::writeMat(edges1, "edges1.mat", "edges1");
479
                cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
234 sorgre 480
                cvtools::writeMat(color1, "color1.mat", "color1");
233 - 481
            #endif
482
 
483
        }
182 jakw 484
    }
485
 
207 flgw 486
 
233 - 487
    // Match phase maps
185 jakw 488
 
233 - 489
    // camera0 against camera1
490
    std::vector<cv::Vec2f> q0, q1;
491
    matchPhaseMaps(occlusion0, occlusion1, up0, up1, q0, q1);
231 jakw 492
 
233 - 493
    size_t nMatches = q0.size();
494
 
495
    if(nMatches < 1){
496
        Q.resize(0);
497
        color.resize(0);
498
 
499
        return;
231 jakw 500
    }
234 sorgre 501
    else {
233 - 502
        // Retrieve color information
503
        color.resize(nMatches);
504
        for(unsigned int i=0; i<nMatches; i++){
505
 
247 jakw 506
            cv::Vec3f c0 = color0.at<cv::Vec3f>(int(q0[i][1]), int(q0[i][0]));
507
            cv::Vec3f c1 = color1.at<cv::Vec3f>(int(q1[i][1]), int(q1[i][0]));
233 - 508
 
257 - 509
            color[i] = (0.5*c0 + 0.5*c1);
233 - 510
        }
231 jakw 511
    }
207 flgw 512
 
233 - 513
    // Triangulate points
514
    triangulate(stereoRect, q0, q1, Q);
207 flgw 515
 
233 - 516
}
207 flgw 517