Subversion Repositories seema-scanner

Rev

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