Subversion Repositories seema-scanner

Rev

Rev 72 | Rev 74 | 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
 
73 jakw 10
static unsigned int nPhases = 50;
11
static unsigned int nSteps = 10;
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
34
    N = 2+2*nSteps;
35
 
70 jakw 36
    // all on pattern
37
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
38
    patterns.push_back(allOn);
39
 
40
    // all off pattern
41
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
42
    patterns.push_back(allOff);
43
 
4 jakw 44
    // Precompute encoded patterns
45
    const float pi = M_PI;
46
 
70 jakw 47
    // Horizontally encoding patterns
48
    for(unsigned int i=0; i<nSteps; i++){
49
        float phase = 2.0*pi/nSteps * i;
50
        float pitch = (float)screenCols/(float)nPhases;
51
        cv::Mat patternI(1,1,CV_8U);
52
        patternI = computePhaseVector(screenCols, phase, pitch);
53
        patterns.push_back(patternI.t());
54
    }
4 jakw 55
 
70 jakw 56
    // Phase cue patterns
72 jakw 57
    for(unsigned int i=0; i<nSteps; i++){
58
        float phase = 2.0*pi/nSteps * i;
70 jakw 59
        float pitch = screenCols;
72 jakw 60
        cv::Mat patternI(1,1,CV_8U);
70 jakw 61
        patternI = computePhaseVector(screenCols, phase, pitch);
62
        patterns.push_back(patternI.t());
4 jakw 63
    }
64
 
72 jakw 65
 
4 jakw 66
}
67
 
41 jakw 68
cv::Mat AlgorithmPhaseShift::getEncodingPattern(unsigned int depth){
4 jakw 69
    return patterns[depth];
70
}
71
 
72
 
70 jakw 73
// Absolute phase from 3 frames
74
cv::Mat getPhase(const cv::Mat I1, const cv::Mat I2, const cv::Mat I3){
4 jakw 75
 
70 jakw 76
    cv::Mat_<float> I1_(I1);
77
    cv::Mat_<float> I2_(I2);
78
    cv::Mat_<float> I3_(I3);
79
 
80
    cv::Mat phase;
81
 
82
    // One call approach
83
    cv::phase(2.0*I1_-I3_-I2_, sqrt(3.0)*(I2_-I3_), phase);
84
    return phase;
85
 
86
}
87
 
88
// Phase unwrapping by means of a phase cue
89
cv::Mat unwrapWithCue(const cv::Mat up, const cv::Mat upCue, unsigned int nPhases){
90
 
4 jakw 91
    const float pi = M_PI;
92
 
70 jakw 93
    // Determine number of jumps
94
    cv::Mat P = (upCue*nPhases-up)/(2*pi);
4 jakw 95
 
70 jakw 96
    // Round to integers
97
    P.convertTo(P, CV_8U);
98
    P.convertTo(P, CV_32F);
4 jakw 99
 
70 jakw 100
    // Add to phase
101
    cv::Mat upUnwrapped = up + P*2*pi;
4 jakw 102
 
70 jakw 103
    // Scale to range [0; 2pi]
104
    upUnwrapped *= 1.0/nPhases;
105
 
106
    return upUnwrapped;
4 jakw 107
}
108
 
70 jakw 109
// Absolute phase and magnitude from N frames
110
std::vector<cv::Mat> getDFTComponents(const std::vector<cv::Mat> frames){
111
 
112
    unsigned int N = frames.size();
113
 
71 jakw 114
//    std::vector<cv::Mat> framesReverse = frames;
115
//    std::reverse(framesReverse.begin(), framesReverse.end());
70 jakw 116
 
117
    // DFT approach
118
    cv::Mat I;
119
    cv::merge(frames, I);
120
    unsigned int w = I.cols;
121
    unsigned int h = I.rows;
122
    I = I.reshape(1, h*w);
123
    I.convertTo(I, CV_32F);
124
    cv::Mat fI;
125
    cv::dft(I, fI, cv::DFT_ROWS + cv::DFT_COMPLEX_OUTPUT);
126
    fI = fI.reshape(N*2, h);
127
 
128
    std::vector<cv::Mat> fIcomp;
129
    cv::split(fI, fIcomp);
130
 
131
    return fIcomp;
132
 
133
}
134
 
42 jakw 135
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 136
 
70 jakw 137
    const float pi = M_PI;
138
 
139
    assert(frames0.size() == N);
140
    assert(frames1.size() == N);
141
 
142
    int frameRows = frames0[0].rows;
143
    int frameCols = frames0[0].cols;
144
 
72 jakw 145
    // Gray-scale everything
70 jakw 146
    std::vector<cv::Mat> frames0Gray(N);
147
    std::vector<cv::Mat> frames1Gray(N);
148
    for(int i=0; i<N; i++){
149
        cv::cvtColor(frames0[i], frames0Gray[i], CV_RGB2GRAY);
150
        cv::cvtColor(frames1[i], frames1Gray[i], CV_RGB2GRAY);
151
    }
152
 
153
    // Decode camera0
154
    std::vector<cv::Mat> frames0Enc(frames0Gray.begin()+2, frames0Gray.begin()+2+nSteps);
72 jakw 155
    std::vector<cv::Mat> frames0Cue(frames0Gray.begin()+2+nSteps, frames0Gray.begin()+2+nSteps+nSteps);
156
    std::vector<cv::Mat> F0Enc = getDFTComponents(frames0Enc);
70 jakw 157
    cv::Mat up0;
72 jakw 158
    cv::phase(F0Enc[2], -F0Enc[3], up0);
71 jakw 159
cvtools::writeMat(up0, "up0.mat", "up0");
72 jakw 160
    std::vector<cv::Mat> F0Cue = getDFTComponents(frames0Cue);
161
    cv::Mat up0Cue;
162
    cv::phase(F0Cue[2], -F0Cue[3], up0Cue);
70 jakw 163
    up0 = unwrapWithCue(up0, up0Cue, nPhases);
164
    up0 *= screenCols/(2*pi);
165
 
166
    // Decode camera1
167
    std::vector<cv::Mat> frames1Enc(frames1Gray.begin()+2, frames1Gray.begin()+2+nSteps);
72 jakw 168
    std::vector<cv::Mat> frames1Cue(frames1Gray.begin()+2+nSteps, frames1Gray.begin()+2+nSteps+nSteps);
70 jakw 169
    std::vector<cv::Mat> f1Icomp = getDFTComponents(frames1Enc);
170
    cv::Mat up1;
171
    cv::phase(f1Icomp[2], -f1Icomp[3], up1);
71 jakw 172
cvtools::writeMat(up1, "up1.mat", "up1");
72 jakw 173
    std::vector<cv::Mat> F1Cue = getDFTComponents(frames1Cue);
174
    cv::Mat up1Cue;
175
    cv::phase(F1Cue[2], -F1Cue[3], up1Cue);
70 jakw 176
    up1 = unwrapWithCue(up1, up1Cue, nPhases);
177
    up1 *= screenCols/(2*pi);
178
 
71 jakw 179
cvtools::writeMat(up0Cue, "up0Cue.mat", "up0Cue");
180
cvtools::writeMat(up1Cue, "up1Cue.mat", "up1Cue");
181
 
70 jakw 182
    // Rectifying homographies (rotation+projections)
183
    cv::Size frameSize(frameCols, frameRows);
184
    cv::Mat R, T;
185
    // stereoRectify segfaults unless R is double precision
186
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
187
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
188
    cv::Mat R0, R1, P0, P1, QRect;
189
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
190
 
191
    // Interpolation maps (lens distortion and rectification)
192
    cv::Mat map0X, map0Y, map1X, map1Y;
193
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
194
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
195
 
196
    // Phase remaps
197
    cv::Mat up0Rect, up1Rect;
198
    cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_CUBIC);
199
    cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_CUBIC);
200
 
71 jakw 201
cvtools::writeMat(up0Rect, "up0Rect.mat", "up0Rect");
202
cvtools::writeMat(up1Rect, "up1Rect.mat", "up1Rect");
203
 
70 jakw 204
    // Color remaps
205
    cv::Mat color0Rect, color1Rect;
206
    cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
207
    cv::remap(frames1[0], color1Rect, map1X, map1Y, CV_INTER_CUBIC);
208
 
71 jakw 209
cvtools::writeMat(color0Rect, "color0Rect.mat", "color0Rect");
210
cvtools::writeMat(color1Rect, "color1Rect.mat", "color1Rect");
211
 
70 jakw 212
    // On/off remaps
213
    cv::Mat frames0OnRect, frames0OffRect;
214
    cv::remap(frames0Gray[0], frames0OnRect, map0X, map0Y, CV_INTER_CUBIC);
215
    cv::remap(frames0Gray[1], frames0OffRect, map0X, map0Y, CV_INTER_CUBIC);
216
 
217
    cv::Mat frames1OnRect, frames1OffRect;
71 jakw 218
    cv::remap(frames1Gray[0], frames1OnRect, map1X, map1Y, CV_INTER_CUBIC);
219
    cv::remap(frames1Gray[1], frames1OffRect, map1X, map1Y, CV_INTER_CUBIC);
70 jakw 220
 
221
    // Occlusion masks
222
    cv::Mat occlusion0Rect, occlusion1Rect;
223
    cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
224
    occlusion0Rect = occlusion0Rect > 50;
225
    cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
226
    occlusion1Rect = occlusion1Rect > 50;
227
 
228
    // Erode occlusion masks
229
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3,3));
230
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
231
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
232
 
71 jakw 233
    // Threshold on gradient of phase
234
    cv::Mat edges0;
73 jakw 235
    cv::Sobel(up0Rect, edges0, -1, 1, 0, 5);
236
    occlusion0Rect = occlusion0Rect & (abs(edges0) < 150);
71 jakw 237
 
238
    cv::Mat edges1;
73 jakw 239
    cv::Sobel(up1Rect, edges1, -1, 1, 0, 5);
240
    occlusion1Rect = occlusion1Rect & (abs(edges1) < 150);
71 jakw 241
 
242
cvtools::writeMat(edges0, "edges0.mat", "edges0");
243
cvtools::writeMat(edges1, "edges1.mat", "edges1");
244
 
73 jakw 245
cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
246
cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
247
 
70 jakw 248
    // Match phase maps
249
    int frameRectRows = map0X.rows;
250
    int frameRectCols = map0X.cols;
251
 
252
    // camera0 against camera1
253
    std::vector<cv::Vec2f> q0Rect, q1Rect;
254
    for(int row=0; row<frameRectRows; row++){
255
 
256
        for(int col=0; col<frameRectCols; col++){
257
 
258
            if(!occlusion0Rect.at<char>(row,col))
259
                continue;
260
 
261
            float up0i = up0Rect.at<float>(row,col);
262
            for(int col1=0; col1<up1Rect.cols-1; col1++){
263
 
264
                if(!occlusion1Rect.at<char>(row,col1) || !occlusion1Rect.at<char>(row,col1+1))
265
                    continue;
266
 
267
                float up1Left = up1Rect.at<float>(row,col1);
268
                float up1Right = up1Rect.at<float>(row,col1+1);
269
 
270
                if((up1Left <= up0i) && (up0i <= up1Right)){
271
 
272
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
273
 
274
                    q0Rect.push_back(cv::Point2f(col, row));
275
                    q1Rect.push_back(cv::Point2f(col1i, row));
71 jakw 276
 
277
                    break;
70 jakw 278
                }
279
 
280
            }
281
 
282
        }
283
 
284
    }
285
 
286
    // camera1 against camera0
287
    //[...]
288
 
289
    int nMatches = q0Rect.size();
290
 
291
    if(nMatches < 1){
292
        Q.resize(0);
293
        color.resize(0);
294
 
295
        return;
296
    }
297
 
298
    // Retrieve color information
299
    color.resize(nMatches);
300
    for(int i=0; i<nMatches; i++){
301
 
302
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
303
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
304
 
305
        color[i] = 0.5*c0 + 0.5*c1;
306
    }
307
 
308
    // Triangulate points
309
    cv::Mat QMatHomogenous, QMat;
310
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
311
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
312
 
313
    // Undo rectification
314
    cv::Mat R0Inv;
315
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
316
    QMat = R0Inv*QMat;
317
 
318
    cvtools::matToPoints3f(QMat, Q);
319
 
4 jakw 320
}