Subversion Repositories seema-scanner

Rev

Rev 128 | Rev 130 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
128 jakw 1
#include "AlgorithmPhaseShiftThreeFreq.h"
4 jakw 2
#include <math.h>
3
 
4
#include "cvtools.h"
5
 
6
#ifndef M_PI
7
    #define M_PI 3.14159265358979323846
8
#endif
9
 
128 jakw 10
static unsigned int nStepsPrimary = 16; // number of shifts/steps in primary
129 jakw 11
static unsigned int nStepsSecondary = 16; // number of shifts/steps in secondary
12
static unsigned int nStepsTertiary = 16; // number of shifts/steps in tertiary
13
static float periodPrimary = 24; // primary period
14
static float periodSecondary = 30; // primary period
4 jakw 15
 
41 jakw 16
// Algorithm
4 jakw 17
static cv::Mat computePhaseVector(unsigned int length, float phase, float pitch){
18
 
19
    cv::Mat phaseVector(length, 1, CV_8UC3);
20
    //phaseVector.setTo(0);
21
 
22
    const float pi = M_PI;
23
 
24
    // Loop through vector
25
    for(int i=0; i<phaseVector.rows; i++){
26
        // Amplitude of channels
71 jakw 27
        float amp = 0.5*(1+cos(2*pi*i/pitch - phase));
4 jakw 28
        phaseVector.at<cv::Vec3b>(i, 0) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
29
    }
30
 
31
    return phaseVector;
32
}
33
 
128 jakw 34
AlgorithmPhaseShiftThreeFreq::AlgorithmPhaseShiftThreeFreq(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
4 jakw 35
 
72 jakw 36
    // Set N
128 jakw 37
    N = 2+nStepsPrimary+nStepsSecondary+nStepsTertiary;
72 jakw 38
 
129 jakw 39
    // Determine the tertiary period to fulfill the heterodyne condition
40
    float periodTertiary = (screenCols*periodPrimary*periodSecondary)/(periodPrimary*periodSecondary+2*screenCols*periodPrimary-screenCols*periodSecondary);
74 jakw 41
 
70 jakw 42
    // all on pattern
43
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
44
    patterns.push_back(allOn);
45
 
46
    // all off pattern
47
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
48
    patterns.push_back(allOff);
49
 
4 jakw 50
    // Precompute encoded patterns
51
    const float pi = M_PI;
52
 
74 jakw 53
    // Primary encoding patterns
118 jakw 54
    for(unsigned int i=0; i<nStepsPrimary; i++){
55
        float phase = 2.0*pi/nStepsPrimary * i;
56
        float pitch = periodPrimary;
70 jakw 57
        cv::Mat patternI(1,1,CV_8U);
58
        patternI = computePhaseVector(screenCols, phase, pitch);
59
        patterns.push_back(patternI.t());
60
    }
4 jakw 61
 
74 jakw 62
    // Secondary encoding patterns
118 jakw 63
    for(unsigned int i=0; i<nStepsSecondary; i++){
64
        float phase = 2.0*pi/nStepsSecondary * i;
129 jakw 65
        float pitch = periodSecondary;
72 jakw 66
        cv::Mat patternI(1,1,CV_8U);
70 jakw 67
        patternI = computePhaseVector(screenCols, phase, pitch);
68
        patterns.push_back(patternI.t());
4 jakw 69
    }
128 jakw 70
    // Tertiary encoding patterns
71
    for(unsigned int i=0; i<nStepsTertiary; i++){
72
        float phase = 2.0*pi/nStepsTertiary * i;
129 jakw 73
        float pitch = periodTertiary;
128 jakw 74
        cv::Mat patternI(1,1,CV_8U);
75
        patternI = computePhaseVector(screenCols, phase, pitch);
76
        patterns.push_back(patternI.t());
77
    }
4 jakw 78
 
79
}
80
 
128 jakw 81
cv::Mat AlgorithmPhaseShiftThreeFreq::getEncodingPattern(unsigned int depth){
4 jakw 82
    return patterns[depth];
83
}
84
 
85
 
70 jakw 86
// Absolute phase from 3 frames
128 jakw 87
static cv::Mat getPhase(const cv::Mat I1, const cv::Mat I2, const cv::Mat I3){
4 jakw 88
 
70 jakw 89
    cv::Mat_<float> I1_(I1);
90
    cv::Mat_<float> I2_(I2);
91
    cv::Mat_<float> I3_(I3);
92
 
93
    cv::Mat phase;
94
 
95
    // One call approach
96
    cv::phase(2.0*I1_-I3_-I2_, sqrt(3.0)*(I2_-I3_), phase);
97
    return phase;
98
 
99
}
100
 
101
// Phase unwrapping by means of a phase cue
128 jakw 102
static cv::Mat unwrapWithCue(const cv::Mat up, const cv::Mat upCue, float nPhases){
70 jakw 103
 
4 jakw 104
    const float pi = M_PI;
105
 
70 jakw 106
    // Determine number of jumps
128 jakw 107
    cv::Mat P = (upCue*nPhases-up)/(2.0*pi);
4 jakw 108
 
70 jakw 109
    // Round to integers
110
    P.convertTo(P, CV_8U);
111
    P.convertTo(P, CV_32F);
4 jakw 112
 
70 jakw 113
    // Add to phase
114
    cv::Mat upUnwrapped = up + P*2*pi;
4 jakw 115
 
70 jakw 116
    // Scale to range [0; 2pi]
117
    upUnwrapped *= 1.0/nPhases;
118
 
119
    return upUnwrapped;
4 jakw 120
}
121
 
70 jakw 122
// Absolute phase and magnitude from N frames
128 jakw 123
static std::vector<cv::Mat> getDFTComponents(const std::vector<cv::Mat> frames){
70 jakw 124
 
125
    unsigned int N = frames.size();
126
 
71 jakw 127
//    std::vector<cv::Mat> framesReverse = frames;
128
//    std::reverse(framesReverse.begin(), framesReverse.end());
70 jakw 129
 
130
    // DFT approach
131
    cv::Mat I;
132
    cv::merge(frames, I);
133
    unsigned int w = I.cols;
134
    unsigned int h = I.rows;
135
    I = I.reshape(1, h*w);
136
    I.convertTo(I, CV_32F);
137
    cv::Mat fI;
138
    cv::dft(I, fI, cv::DFT_ROWS + cv::DFT_COMPLEX_OUTPUT);
139
    fI = fI.reshape(N*2, h);
140
 
141
    std::vector<cv::Mat> fIcomp;
142
    cv::split(fI, fIcomp);
143
 
144
    return fIcomp;
145
 
146
}
147
 
128 jakw 148
void AlgorithmPhaseShiftThreeFreq::get3DPoints(SMCalibrationParameters calibration, const std::vector<cv::Mat>& frames0, const std::vector<cv::Mat>& frames1, std::vector<cv::Point3f>& Q, std::vector<cv::Vec3b>& color){
4 jakw 149
 
70 jakw 150
    const float pi = M_PI;
151
 
152
    assert(frames0.size() == N);
153
    assert(frames1.size() == N);
154
 
155
    int frameRows = frames0[0].rows;
156
    int frameCols = frames0[0].cols;
157
 
72 jakw 158
    // Gray-scale everything
70 jakw 159
    std::vector<cv::Mat> frames0Gray(N);
160
    std::vector<cv::Mat> frames1Gray(N);
161
    for(int i=0; i<N; i++){
113 jakw 162
        cv::cvtColor(frames0[i], frames0Gray[i], CV_BayerBG2GRAY);
163
        cv::cvtColor(frames1[i], frames1Gray[i], CV_BayerBG2GRAY);
70 jakw 164
    }
165
 
166
    // Decode camera0
118 jakw 167
    std::vector<cv::Mat> frames0Primary(frames0Gray.begin()+2, frames0Gray.begin()+2+nStepsPrimary);
128 jakw 168
    std::vector<cv::Mat> frames0Secondary(frames0Gray.begin()+2+nStepsPrimary, frames0Gray.end()-nStepsTertiary);
169
    std::vector<cv::Mat> frames0Tertiary(frames0Gray.end()-nStepsTertiary, frames0Gray.end());
129 jakw 170
 
76 jakw 171
    std::vector<cv::Mat> F0Primary = getDFTComponents(frames0Primary);
74 jakw 172
    cv::Mat up0Primary;
76 jakw 173
    cv::phase(F0Primary[2], -F0Primary[3], up0Primary);
174
    std::vector<cv::Mat> F0Secondary = getDFTComponents(frames0Secondary);
175
    cv::Mat up0Secondary;
176
    cv::phase(F0Secondary[2], -F0Secondary[3], up0Secondary);
128 jakw 177
    std::vector<cv::Mat> F0Tertiary = getDFTComponents(frames0Tertiary);
178
    cv::Mat up0Tertiary;
179
    cv::phase(F0Tertiary[2], -F0Tertiary[3], up0Tertiary);
180
 
181
    cv::Mat up0EquivalentPS = up0Primary - up0Secondary;
182
    up0EquivalentPS = cvtools::modulo(up0EquivalentPS, 2.0*pi);
183
 
184
    cv::Mat up0EquivalentST = up0Secondary - up0Tertiary;
185
    up0EquivalentST = cvtools::modulo(up0EquivalentST, 2.0*pi);
186
 
187
    cv::Mat up0Equivalent = up0EquivalentPS - up0EquivalentST;
188
    up0Equivalent = cvtools::modulo(up0Equivalent, 2.0*pi);
189
 
129 jakw 190
cv::Mat up0Equivalent2 = unwrapWithCue(up0EquivalentPS, up0Equivalent, (float)screenCols/periodPrimary);
191
 
192
    cv::Mat up0 = unwrapWithCue(up0Primary, up0Equivalent2, (float)screenCols/periodPrimary);
128 jakw 193
    up0 *= screenCols/(2.0*pi);
194
    cv::Mat amplitude0;
195
    cv::magnitude(F0Primary[2], -F0Primary[3], amplitude0);
70 jakw 196
 
197
    // Decode camera1
118 jakw 198
    std::vector<cv::Mat> frames1Primary(frames1Gray.begin()+2, frames1Gray.begin()+2+nStepsPrimary);
129 jakw 199
    std::vector<cv::Mat> frames1Secondary(frames1Gray.begin()+2+nStepsPrimary, frames1Gray.end()-nStepsTertiary);
200
    std::vector<cv::Mat> frames1Tertiary(frames1Gray.end()-nStepsTertiary, frames1Gray.end());
201
 
76 jakw 202
    std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
74 jakw 203
    cv::Mat up1Primary;
76 jakw 204
    cv::phase(F1Primary[2], -F1Primary[3], up1Primary);
205
    std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
206
    cv::Mat up1Secondary;
207
    cv::phase(F1Secondary[2], -F1Secondary[3], up1Secondary);
128 jakw 208
    std::vector<cv::Mat> F1Tertiary = getDFTComponents(frames1Tertiary);
209
    cv::Mat up1Tertiary;
210
    cv::phase(F1Tertiary[2], -F1Tertiary[3], up1Tertiary);
211
 
212
    cv::Mat up1EquivalentPS = up1Primary - up1Secondary;
213
    up1EquivalentPS = cvtools::modulo(up1EquivalentPS, 2.0*pi);
214
 
215
    cv::Mat up1EquivalentST = up1Secondary - up1Tertiary;
216
    up1EquivalentST = cvtools::modulo(up1EquivalentST, 2.0*pi);
217
 
218
    cv::Mat up1Equivalent = up1EquivalentPS - up1EquivalentST;
219
    up1Equivalent = cvtools::modulo(up1Equivalent, 2.0*pi);
220
 
118 jakw 221
    cv::Mat up1 = unwrapWithCue(up1Primary, up1Equivalent, (float)screenCols/periodPrimary);
128 jakw 222
    up1 *= screenCols/(2.0*pi);
223
    cv::Mat amplitude1;
224
    cv::magnitude(F1Primary[2], -F1Primary[3], amplitude1);
70 jakw 225
 
129 jakw 226
//cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
227
//cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
228
//cvtools::writeMat(up0Tertiary, "up0Tertiary.mat", "up0Tertiary");
229
//cvtools::writeMat(up0Equivalent, "up0Equivalent.mat", "up0Equivalent");
230
cvtools::writeMat(up0Equivalent2, "up0Equivalent2.mat", "up0Equivalent2");
231
//cvtools::writeMat(up0EquivalentPS, "up0EquivalentPS.mat", "up0EquivalentPS");
232
//cvtools::writeMat(up0EquivalentST, "up0EquivalentST.mat", "up0EquivalentST");
233
//cvtools::writeMat(up0, "up0.mat", "up0");
234
//cvtools::writeMat(up1, "up1.mat", "up1");
235
//cvtools::writeMat(amplitude0, "amplitude0.mat", "amplitude0");
71 jakw 236
 
70 jakw 237
    // Rectifying homographies (rotation+projections)
238
    cv::Size frameSize(frameCols, frameRows);
239
    cv::Mat R, T;
240
    // stereoRectify segfaults unless R is double precision
241
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
242
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
243
    cv::Mat R0, R1, P0, P1, QRect;
244
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
245
 
246
    // Interpolation maps (lens distortion and rectification)
247
    cv::Mat map0X, map0Y, map1X, map1Y;
248
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
249
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
250
 
251
    // Phase remaps
252
    cv::Mat up0Rect, up1Rect;
118 jakw 253
    cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_LINEAR);
254
    cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_LINEAR);
70 jakw 255
 
128 jakw 256
    // amplitude remaps
257
    cv::Mat amplitude0Rect, amplitude1Rect;
258
    cv::remap(amplitude0, amplitude0Rect, map0X, map0Y, CV_INTER_LINEAR);
259
    cv::remap(amplitude1, amplitude1Rect, map1X, map1Y, CV_INTER_LINEAR);
260
 
74 jakw 261
//cvtools::writeMat(up0Rect, "up0Rect.mat", "up0Rect");
262
//cvtools::writeMat(up1Rect, "up1Rect.mat", "up1Rect");
71 jakw 263
 
118 jakw 264
    // color debayer and remap
70 jakw 265
    cv::Mat color0Rect, color1Rect;
121 jakw 266
//    frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
267
    cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
118 jakw 268
    cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
70 jakw 269
 
121 jakw 270
//    frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
271
    cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
118 jakw 272
    cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
273
 
274
//cvtools::writeMat(frames0Rect[18], "frames0Rect_18.mat", "frames0Rect_18");
275
//cvtools::writeMat(frames0Rect[19], "frames0Rect_19.mat", "frames0Rect_19");
276
 
74 jakw 277
//cvtools::writeMat(color0Rect, "color0Rect.mat", "color0Rect");
278
//cvtools::writeMat(color1Rect, "color1Rect.mat", "color1Rect");
71 jakw 279
 
70 jakw 280
    // On/off remaps
281
    cv::Mat frames0OnRect, frames0OffRect;
118 jakw 282
    cv::remap(frames0Gray[0], frames0OnRect, map0X, map0Y, CV_INTER_LINEAR);
283
    cv::remap(frames0Gray[1], frames0OffRect, map0X, map0Y, CV_INTER_LINEAR);
70 jakw 284
 
285
    cv::Mat frames1OnRect, frames1OffRect;
118 jakw 286
    cv::remap(frames1Gray[0], frames1OnRect, map1X, map1Y, CV_INTER_LINEAR);
287
    cv::remap(frames1Gray[1], frames1OffRect, map1X, map1Y, CV_INTER_LINEAR);
70 jakw 288
 
289
    // Occlusion masks
290
    cv::Mat occlusion0Rect, occlusion1Rect;
291
    cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
121 jakw 292
    occlusion0Rect = (occlusion0Rect > 25) & (occlusion0Rect < 250);
70 jakw 293
    cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
121 jakw 294
    occlusion1Rect = (occlusion1Rect > 25) & (occlusion1Rect < 250);
70 jakw 295
 
128 jakw 296
    // Threshold on energy at primary frequency
297
    occlusion0Rect = occlusion0Rect & (amplitude0Rect > 5.0*nStepsPrimary);
298
    occlusion1Rect = occlusion1Rect & (amplitude1Rect > 5.0*nStepsPrimary);
299
 
74 jakw 300
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
301
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
302
 
70 jakw 303
    // Erode occlusion masks
74 jakw 304
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
70 jakw 305
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
306
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
307
 
71 jakw 308
    // Threshold on gradient of phase
309
    cv::Mat edges0;
128 jakw 310
    cv::Sobel(up0Rect, edges0, -1, 1, 1, 5);
73 jakw 311
    occlusion0Rect = occlusion0Rect & (abs(edges0) < 150);
71 jakw 312
 
313
    cv::Mat edges1;
128 jakw 314
    cv::Sobel(up1Rect, edges1, -1, 1, 1, 5);
73 jakw 315
    occlusion1Rect = occlusion1Rect & (abs(edges1) < 150);
71 jakw 316
 
74 jakw 317
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
318
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
71 jakw 319
 
70 jakw 320
    // Match phase maps
321
    int frameRectRows = map0X.rows;
322
    int frameRectCols = map0X.cols;
323
 
324
    // camera0 against camera1
325
    std::vector<cv::Vec2f> q0Rect, q1Rect;
326
    for(int row=0; row<frameRectRows; row++){
327
        for(int col=0; col<frameRectCols; col++){
328
 
329
            if(!occlusion0Rect.at<char>(row,col))
330
                continue;
331
 
332
            float up0i = up0Rect.at<float>(row,col);
333
            for(int col1=0; col1<up1Rect.cols-1; col1++){
334
 
335
                if(!occlusion1Rect.at<char>(row,col1) || !occlusion1Rect.at<char>(row,col1+1))
336
                    continue;
337
 
338
                float up1Left = up1Rect.at<float>(row,col1);
339
                float up1Right = up1Rect.at<float>(row,col1+1);
340
 
128 jakw 341
                if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 0.5) && (up1Right-up0i < 0.5)){
70 jakw 342
 
343
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
344
 
345
                    q0Rect.push_back(cv::Point2f(col, row));
346
                    q1Rect.push_back(cv::Point2f(col1i, row));
71 jakw 347
 
348
                    break;
70 jakw 349
                }
350
            }
351
        }
352
    }
353
 
76 jakw 354
//    // camera1 against camera0
355
//    for(int row=0; row<frameRectRows; row++){
356
//        for(int col=0; col<frameRectCols; col++){
70 jakw 357
 
76 jakw 358
//            if(!occlusion1Rect.at<char>(row,col))
359
//                continue;
74 jakw 360
 
76 jakw 361
//            float up1i = up1Rect.at<float>(row,col);
362
//            for(int col0=0; col0<up0Rect.cols-1; col0++){
74 jakw 363
 
76 jakw 364
//                if(!occlusion0Rect.at<char>(row,col0) || !occlusion0Rect.at<char>(row,col0+1))
365
//                    continue;
74 jakw 366
 
76 jakw 367
//                float up0Left = up0Rect.at<float>(row,col0);
368
//                float up0Right = up0Rect.at<float>(row,col0+1);
74 jakw 369
 
76 jakw 370
//                if((up0Left <= up1i) && (up1i <= up0Right) && (up1i-up0Left < 1) && (up0Right-up1i < 1)){
74 jakw 371
 
76 jakw 372
//                    float col0i = col0 + (up1i-up0Left)/(up0Right-up0Left);
74 jakw 373
 
76 jakw 374
//                    q1Rect.push_back(cv::Point2f(col, row));
375
//                    q0Rect.push_back(cv::Point2f(col0i, row));
74 jakw 376
 
76 jakw 377
//                    break;
378
//                }
379
//            }
380
//        }
381
//    }
74 jakw 382
 
70 jakw 383
    int nMatches = q0Rect.size();
384
 
385
    if(nMatches < 1){
386
        Q.resize(0);
387
        color.resize(0);
388
 
389
        return;
390
    }
391
 
392
    // Retrieve color information
393
    color.resize(nMatches);
394
    for(int i=0; i<nMatches; i++){
395
 
396
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
397
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
398
 
399
        color[i] = 0.5*c0 + 0.5*c1;
400
    }
401
 
402
    // Triangulate points
403
    cv::Mat QMatHomogenous, QMat;
404
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
405
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
406
 
407
    // Undo rectification
408
    cv::Mat R0Inv;
409
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
410
    QMat = R0Inv*QMat;
411
 
412
    cvtools::matToPoints3f(QMat, Q);
413
 
4 jakw 414
}