Subversion Repositories seema-scanner

Rev

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