Subversion Repositories seema-scanner

Rev

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