Subversion Repositories seema-scanner

Rev

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

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