Subversion Repositories seema-scanner

Rev

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

Rev Author Line No. Line
128 jakw 1
#include "AlgorithmPhaseShiftTwoFreq.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
 
143 jakw 10
static unsigned int nStepsPrimary = 16; // number of shifts/steps in primary
11
static unsigned int nStepsSecondary = 8; // number of shifts/steps in secondary
133 jakw 12
static float periodPrimary = 48; // 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
 
128 jakw 32
AlgorithmPhaseShiftTwoFreq::AlgorithmPhaseShiftTwoFreq(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
 
128 jakw 72
cv::Mat AlgorithmPhaseShiftTwoFreq::getEncodingPattern(unsigned int depth){
4 jakw 73
    return patterns[depth];
74
}
75
 
76
 
167 jakw 77
//// Absolute phase from 3 frames
78
//static cv::Mat getPhase(const cv::Mat I1, const cv::Mat I2, const cv::Mat I3){
4 jakw 79
 
167 jakw 80
//    cv::Mat_<float> I1_(I1);
81
//    cv::Mat_<float> I2_(I2);
82
//    cv::Mat_<float> I3_(I3);
70 jakw 83
 
167 jakw 84
//    cv::Mat phase;
70 jakw 85
 
167 jakw 86
//    // One call approach
87
//    cv::phase(2.0*I1_-I3_-I2_, sqrt(3.0)*(I2_-I3_), phase);
88
//    return phase;
70 jakw 89
 
167 jakw 90
//}
70 jakw 91
 
92
// Phase unwrapping by means of a phase cue
128 jakw 93
static 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
128 jakw 98
    cv::Mat P = (upCue*nPhases-up)/(2.0*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
128 jakw 114
static std::vector<cv::Mat> getDFTComponents(const std::vector<cv::Mat> frames){
70 jakw 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
 
128 jakw 139
void AlgorithmPhaseShiftTwoFreq::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
 
178 jakw 149
    // Rectifying homographies (rotation+projections)
150
    cv::Size frameSize(frameCols, frameRows);
151
    cv::Mat R, T;
152
    // stereoRectify segfaults unless R is double precision
153
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
154
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
155
    cv::Mat R0, R1, P0, P1, QRect;
156
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
157
 
158
    // Interpolation maps (lens distortion and rectification)
159
    cv::Mat map0X, map0Y, map1X, map1Y;
160
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
161
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
162
 
179 jakw 163
    // Gray-scale and remap
178 jakw 164
    std::vector<cv::Mat> frames0Rect(N);
165
    std::vector<cv::Mat> frames1Rect(N);
167 jakw 166
    for(unsigned int i=0; i<N; i++){
178 jakw 167
        cv::Mat temp;
168
        cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
169
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
170
        cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
171
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
70 jakw 172
    }
173
 
174
    // Decode camera0
178 jakw 175
    std::vector<cv::Mat> frames0Primary(frames0Rect.begin()+2, frames0Rect.begin()+2+nStepsPrimary);
176
    std::vector<cv::Mat> frames0Secondary(frames0Rect.begin()+2+nStepsPrimary, frames0Rect.end());
76 jakw 177
    std::vector<cv::Mat> F0Primary = getDFTComponents(frames0Primary);
74 jakw 178
    cv::Mat up0Primary;
76 jakw 179
    cv::phase(F0Primary[2], -F0Primary[3], up0Primary);
180
    //cv::Mat up0Secondary = getPhase(frames0Secondary[0], frames0Secondary[1], frames0Secondary[2]);
181
    std::vector<cv::Mat> F0Secondary = getDFTComponents(frames0Secondary);
182
    cv::Mat up0Secondary;
183
    cv::phase(F0Secondary[2], -F0Secondary[3], up0Secondary);
74 jakw 184
    cv::Mat up0Equivalent = up0Primary - up0Secondary;
128 jakw 185
    up0Equivalent = cvtools::modulo(up0Equivalent, 2.0*pi);
118 jakw 186
    cv::Mat up0 = unwrapWithCue(up0Primary, up0Equivalent, (float)screenCols/periodPrimary);
128 jakw 187
    up0 *= screenCols/(2.0*pi);
188
    cv::Mat amplitude0;
189
    cv::magnitude(F0Primary[2], -F0Primary[3], amplitude0);
70 jakw 190
 
191
    // Decode camera1
178 jakw 192
    std::vector<cv::Mat> frames1Primary(frames1Rect.begin()+2, frames1Rect.begin()+2+nStepsPrimary);
193
    std::vector<cv::Mat> frames1Secondary(frames1Rect.begin()+2+nStepsPrimary, frames1Rect.end());
76 jakw 194
    std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
74 jakw 195
    cv::Mat up1Primary;
76 jakw 196
    cv::phase(F1Primary[2], -F1Primary[3], up1Primary);
197
    //cv::Mat up1Secondary = getPhase(frames1Secondary[0], frames1Secondary[1], frames1Secondary[2]);
198
    std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
199
    cv::Mat up1Secondary;
200
    cv::phase(F1Secondary[2], -F1Secondary[3], up1Secondary);
74 jakw 201
    cv::Mat up1Equivalent = up1Primary - up1Secondary;
128 jakw 202
    up1Equivalent = cvtools::modulo(up1Equivalent, 2.0*pi);
118 jakw 203
    cv::Mat up1 = unwrapWithCue(up1Primary, up1Equivalent, (float)screenCols/periodPrimary);
128 jakw 204
    up1 *= screenCols/(2.0*pi);
205
    cv::Mat amplitude1;
206
    cv::magnitude(F1Primary[2], -F1Primary[3], amplitude1);
70 jakw 207
 
148 jakw 208
//cvtools::writeMat(up0Primary, "up0Primary.mat", "up0Primary");
209
//cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
210
//cvtools::writeMat(up0Equivalent, "up0Equivalent.mat", "up0Equivalent");
211
//cvtools::writeMat(up0, "up0.mat", "up0");
212
//cvtools::writeMat(amplitude0, "amplitude0.mat", "amplitude0");
71 jakw 213
 
118 jakw 214
    // color debayer and remap
178 jakw 215
    cv::Mat color0, color1;
121 jakw 216
//    frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
178 jakw 217
    cv::cvtColor(frames0[0], color0, CV_BayerBG2RGB);
218
    cv::remap(color0, color0, map0X, map0Y, CV_INTER_LINEAR);
70 jakw 219
 
121 jakw 220
//    frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
178 jakw 221
    cv::cvtColor(frames1[0], color1, CV_BayerBG2RGB);
222
    cv::remap(color1, color1, map1X, map1Y, CV_INTER_LINEAR);
118 jakw 223
 
178 jakw 224
//cvtools::writeMat(color0, "color0.mat", "color0");
225
//cvtools::writeMat(color1, "color1.mat", "color1");
118 jakw 226
 
70 jakw 227
    // Occlusion masks
178 jakw 228
    cv::Mat occlusion0, occlusion1;
229
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0);
230
    occlusion0 = (occlusion0 > 25) & (occlusion0 < 250);
231
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1);
232
    occlusion1 = (occlusion1 > 25) & (occlusion1 < 250);
70 jakw 233
 
128 jakw 234
    // Threshold on energy at primary frequency
178 jakw 235
    occlusion0 = occlusion0 & (amplitude0 > 5.0*nStepsPrimary);
236
    occlusion1 = occlusion1 & (amplitude1 > 5.0*nStepsPrimary);
128 jakw 237
 
178 jakw 238
//cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
239
//cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
74 jakw 240
 
70 jakw 241
    // Erode occlusion masks
74 jakw 242
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
178 jakw 243
    cv::erode(occlusion0, occlusion0, strel);
244
    cv::erode(occlusion1, occlusion1, strel);
70 jakw 245
 
71 jakw 246
    // Threshold on gradient of phase
247
    cv::Mat edges0;
178 jakw 248
    cv::Sobel(up0, edges0, -1, 1, 1, 5);
249
    occlusion0 = occlusion0 & (abs(edges0) < 150);
71 jakw 250
 
251
    cv::Mat edges1;
178 jakw 252
    cv::Sobel(up1, edges1, -1, 1, 1, 5);
253
    occlusion1 = occlusion1 & (abs(edges1) < 150);
71 jakw 254
 
74 jakw 255
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
256
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
71 jakw 257
 
70 jakw 258
    // Match phase maps
259
    int frameRectRows = map0X.rows;
260
    int frameRectCols = map0X.cols;
261
 
262
    // camera0 against camera1
178 jakw 263
    std::vector<cv::Vec2f> q0, q1;
70 jakw 264
    for(int row=0; row<frameRectRows; row++){
265
        for(int col=0; col<frameRectCols; col++){
266
 
178 jakw 267
            if(!occlusion0.at<char>(row,col))
70 jakw 268
                continue;
269
 
178 jakw 270
            float up0i = up0.at<float>(row,col);
271
            for(int col1=0; col1<up1.cols-1; col1++){
70 jakw 272
 
178 jakw 273
                if(!occlusion1.at<char>(row,col1) || !occlusion1.at<char>(row,col1+1))
70 jakw 274
                    continue;
275
 
178 jakw 276
                float up1Left = up1.at<float>(row,col1);
277
                float up1Right = up1.at<float>(row,col1+1);
70 jakw 278
 
74 jakw 279
                if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 1) && (up1Right-up0i < 1)){
70 jakw 280
 
281
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
282
 
178 jakw 283
                    q0.push_back(cv::Point2f(col, row));
284
                    q1.push_back(cv::Point2f(col1i, row));
71 jakw 285
 
286
                    break;
70 jakw 287
                }
288
            }
289
        }
290
    }
291
 
292
 
178 jakw 293
    int nMatches = q0.size();
74 jakw 294
 
70 jakw 295
    if(nMatches < 1){
296
        Q.resize(0);
297
        color.resize(0);
298
 
299
        return;
300
    }
301
 
302
    // Retrieve color information
303
    color.resize(nMatches);
304
    for(int i=0; i<nMatches; i++){
305
 
178 jakw 306
        cv::Vec3b c0 = color0.at<cv::Vec3b>(q0[i][1], q0[i][0]);
307
        cv::Vec3b c1 = color1.at<cv::Vec3b>(q1[i][1], q1[i][0]);
70 jakw 308
 
309
        color[i] = 0.5*c0 + 0.5*c1;
310
    }
311
 
312
    // Triangulate points
313
    cv::Mat QMatHomogenous, QMat;
178 jakw 314
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
70 jakw 315
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
316
 
317
    // Undo rectification
318
    cv::Mat R0Inv;
319
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
320
    QMat = R0Inv*QMat;
321
 
322
    cvtools::matToPoints3f(QMat, Q);
323
 
4 jakw 324
}