Subversion Repositories seema-scanner

Rev

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