Subversion Repositories seema-scanner

Rev

Rev 42 | Rev 71 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 42 Rev 70
Line 6... Line 6...
6
#ifndef M_PI
6
#ifndef M_PI
7
    #define M_PI 3.14159265358979323846
7
    #define M_PI 3.14159265358979323846
8
#endif
8
#endif
9
 
9
 
10
static unsigned int nPhases = 8;
10
static unsigned int nPhases = 8;
-
 
11
static unsigned int nSteps = 8;
11
 
12
 
12
// Algorithm
13
// Algorithm
13
static cv::Mat computePhaseVector(unsigned int length, float phase, float pitch){
14
static cv::Mat computePhaseVector(unsigned int length, float phase, float pitch){
14
 
15
 
15
    cv::Mat phaseVector(length, 1, CV_8UC3);
16
    cv::Mat phaseVector(length, 1, CV_8UC3);
Line 25... Line 26...
25
    }
26
    }
26
 
27
 
27
    return phaseVector;
28
    return phaseVector;
28
}
29
}
29
 
30
 
30
AlgorithmPhaseShift::AlgorithmPhaseShift(unsigned int _screenCols, unsigned int _screenRows, CodingDir _dir) : Algorithm(_screenCols, _screenRows, _dir){
31
AlgorithmPhaseShift::AlgorithmPhaseShift(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
-
 
32
 
-
 
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);
31
 
40
 
32
    // Set N
41
    // Set N
33
    if(dir == CodingDirBoth)
-
 
34
        this->N = 12;
-
 
35
    else
-
 
36
        this->N = 6;
42
    N = 2+nSteps+3;
37
 
43
 
38
    // Precompute encoded patterns
44
    // Precompute encoded patterns
39
    const float pi = M_PI;
45
    const float pi = M_PI;
40
 
46
 
41
    if(dir & CodingDirHorizontal){
-
 
42
        // Horizontally encoding patterns
47
    // Horizontally encoding patterns
43
        for(unsigned int i=0; i<3; i++){
48
    for(unsigned int i=0; i<nSteps; i++){
44
            float phase = 2.0*pi/3.0 * (1.0 - (float)i);
49
        float phase = 2.0*pi/nSteps * i;
45
            float pitch = (float)screenCols/(float)nPhases;
50
        float pitch = (float)screenCols/(float)nPhases;
46
            cv::Mat patternI(1,1,CV_8U);
51
        cv::Mat patternI(1,1,CV_8U);
47
            patternI = computePhaseVector(screenCols, phase, pitch);
-
 
48
            patternI = patternI.t();
-
 
49
            patterns.push_back(patternI);
-
 
50
        }
-
 
51
 
-
 
52
        // Phase cue patterns
-
 
53
        for(unsigned int i=0; i<3; i++){
-
 
54
            float phase = 2.0*pi/3.0 * (1.0 - (float)i);
-
 
55
            float pitch = screenCols;
-
 
56
            cv::Mat patternI;
-
 
57
            patternI = computePhaseVector(screenCols, phase, pitch);
52
        patternI = computePhaseVector(screenCols, phase, pitch);
58
            patternI = patternI.t();
-
 
59
            patterns.push_back(patternI);
53
        patterns.push_back(patternI.t());
60
        }
-
 
61
    }
54
    }
62
    if(dir & CodingDirVertical){
-
 
63
        // Precompute vertically encoding patterns
-
 
64
        for(unsigned int i=0; i<3; i++){
-
 
65
            float phase = 2.0*pi/3.0 * (1.0 - (float)i);
-
 
66
            float pitch = (float)screenRows/(float)nPhases;
-
 
67
            cv::Mat patternI;
-
 
68
            patternI = computePhaseVector(screenRows, phase, pitch);
-
 
69
            patterns.push_back(patternI);
-
 
70
        }
-
 
71
 
55
 
72
        // Precompute vertically phase cue patterns
56
    // Phase cue patterns
73
        for(unsigned int i=0; i<3; i++){
57
    for(unsigned int i=0; i<3; i++){
74
            float phase = 2.0*pi/3.0 * (1.0 - (float)i);
58
        float phase = 2.0*pi/3.0 * i;
75
            float pitch = screenRows;
59
        float pitch = screenCols;
76
            cv::Mat patternI;
60
        cv::Mat patternI;
77
            patternI = computePhaseVector(screenRows, phase, pitch);
61
        patternI = computePhaseVector(screenCols, phase, pitch);
78
            patterns.push_back(patternI);
62
        patterns.push_back(patternI.t());
79
        }
-
 
80
    }
63
    }
-
 
64
 
81
}
65
}
82
 
66
 
83
cv::Mat AlgorithmPhaseShift::getEncodingPattern(unsigned int depth){
67
cv::Mat AlgorithmPhaseShift::getEncodingPattern(unsigned int depth){
84
    return patterns[depth];
68
    return patterns[depth];
85
}
69
}
86
 
70
 
87
 
71
 
-
 
72
// Absolute phase from 3 frames
88
static cv::Mat absolutePhase(cv::Mat _I1, cv::Mat _I2, cv::Mat _I3){
73
cv::Mat getPhase(const cv::Mat I1, const cv::Mat I2, const cv::Mat I3){
-
 
74
 
-
 
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
 
89
 
90
    const float pi = M_PI;
90
    const float pi = M_PI;
91
 
91
 
92
    // Mat_ wrapper for easier indexing
92
    // Determine number of jumps
93
    cv::Mat_<float> I1(_I1);
93
    cv::Mat P = (upCue*nPhases-up)/(2*pi);
-
 
94
 
-
 
95
    // Round to integers
94
    cv::Mat_<float> I2(_I2);
96
    P.convertTo(P, CV_8U);
95
    cv::Mat_<float> I3(_I3);
97
    P.convertTo(P, CV_32F);
96
 
98
 
-
 
99
    // Add to phase
97
    cv::Mat absPhase(I1.size(), CV_32F);
100
    cv::Mat upUnwrapped = up + P*2*pi;
98
 
101
 
-
 
102
    // Scale to range [0; 2pi]
99
    for(int i = 0; i < absPhase.rows; i++){
103
    upUnwrapped *= 1.0/nPhases;
-
 
104
 
-
 
105
    return upUnwrapped;
-
 
106
}
-
 
107
 
100
        for(int j = 0; j < absPhase.cols; j++){
108
// Absolute phase and magnitude from N frames
101
            float phase = atan2(sqrt(3.0) * (I1(i,j)-I3(i,j)), I1(i,j) + I3(i,j) - 2.0*I2(i,j)) + pi;
109
std::vector<cv::Mat> getDFTComponents(const std::vector<cv::Mat> frames){
-
 
110
 
-
 
111
    unsigned int N = frames.size();
-
 
112
 
102
            absPhase.at<float>(i,j) = phase;
113
    std::vector<cv::Mat> framesReverse = frames;
-
 
114
    std::reverse(framesReverse.begin(), framesReverse.end());
-
 
115
 
-
 
116
    // DFT approach
103
        }
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);
104
    }
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;
105
 
131
 
106
    //absPhase.addref();
-
 
107
    return absPhase;
-
 
108
}
132
}
109
 
133
 
110
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){
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){
111
 
135
 
-
 
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);
-
 
154
    std::vector<cv::Mat> frames0Cue(frames0Gray.begin()+2+nSteps, frames0Gray.begin()+nSteps+2+nSteps+3);
-
 
155
    std::vector<cv::Mat> f0Icomp = getDFTComponents(frames0Enc);
-
 
156
    cv::Mat up0;
-
 
157
    cv::phase(f0Icomp[2], -f0Icomp[3], up0);
-
 
158
    cv::Mat up0Cue = getPhase(frames0Cue[0], frames0Cue[1], frames0Cue[2]);
-
 
159
    up0 = unwrapWithCue(up0, up0Cue, nPhases);
-
 
160
    up0 *= screenCols/(2*pi);
-
 
161
 
-
 
162
    // Decode camera1
-
 
163
    std::vector<cv::Mat> frames1Enc(frames1Gray.begin()+2, frames1Gray.begin()+2+nSteps);
-
 
164
    std::vector<cv::Mat> frames1Cue(frames1Gray.begin()+2+nSteps, frames1Gray.begin()+nSteps+2+nSteps+3);
-
 
165
    std::vector<cv::Mat> f1Icomp = getDFTComponents(frames1Enc);
-
 
166
    cv::Mat up1;
-
 
167
    cv::phase(f1Icomp[2], -f1Icomp[3], up1);
-
 
168
    cv::Mat up1Cue = getPhase(frames1Cue[0], frames1Cue[1], frames1Cue[2]);
-
 
169
    up1 = unwrapWithCue(up1, up1Cue, nPhases);
-
 
170
    up1 *= screenCols/(2*pi);
-
 
171
 
-
 
172
    // Rectifying homographies (rotation+projections)
-
 
173
    cv::Size frameSize(frameCols, frameRows);
-
 
174
    cv::Mat R, T;
-
 
175
    // stereoRectify segfaults unless R is double precision
-
 
176
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
-
 
177
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
-
 
178
    cv::Mat R0, R1, P0, P1, QRect;
-
 
179
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
-
 
180
 
-
 
181
    // Interpolation maps (lens distortion and rectification)
-
 
182
    cv::Mat map0X, map0Y, map1X, map1Y;
-
 
183
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
-
 
184
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
-
 
185
 
-
 
186
    // Phase remaps
-
 
187
    cv::Mat up0Rect, up1Rect;
-
 
188
    cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_CUBIC);
-
 
189
    cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_CUBIC);
-
 
190
 
-
 
191
    // Color remaps
-
 
192
    cv::Mat color0Rect, color1Rect;
-
 
193
    cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
-
 
194
    cv::remap(frames1[0], color1Rect, map1X, map1Y, CV_INTER_CUBIC);
-
 
195
 
-
 
196
    // On/off remaps
-
 
197
    cv::Mat frames0OnRect, frames0OffRect;
-
 
198
    cv::remap(frames0Gray[0], frames0OnRect, map0X, map0Y, CV_INTER_CUBIC);
-
 
199
    cv::remap(frames0Gray[1], frames0OffRect, map0X, map0Y, CV_INTER_CUBIC);
-
 
200
 
-
 
201
    cv::Mat frames1OnRect, frames1OffRect;
-
 
202
    cv::remap(frames1Gray[0], frames1OnRect, map0X, map0Y, CV_INTER_CUBIC);
-
 
203
    cv::remap(frames1Gray[1], frames1OffRect, map0X, map0Y, CV_INTER_CUBIC);
-
 
204
 
-
 
205
    // Occlusion masks
-
 
206
    cv::Mat occlusion0Rect, occlusion1Rect;
-
 
207
    cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
-
 
208
    occlusion0Rect = occlusion0Rect > 50;
-
 
209
    cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
-
 
210
    occlusion1Rect = occlusion1Rect > 50;
-
 
211
 
-
 
212
    // Erode occlusion masks
-
 
213
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3,3));
-
 
214
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
-
 
215
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
-
 
216
 
-
 
217
    // Match phase maps
-
 
218
    int frameRectRows = map0X.rows;
-
 
219
    int frameRectCols = map0X.cols;
-
 
220
 
-
 
221
    // camera0 against camera1
-
 
222
    std::vector<cv::Vec2f> q0Rect, q1Rect;
-
 
223
    for(int row=0; row<frameRectRows; row++){
-
 
224
 
-
 
225
        for(int col=0; col<frameRectCols; col++){
-
 
226
 
-
 
227
            if(!occlusion0Rect.at<char>(row,col))
-
 
228
                continue;
-
 
229
 
-
 
230
            float up0i = up0Rect.at<float>(row,col);
-
 
231
            for(int col1=0; col1<up1Rect.cols-1; col1++){
-
 
232
 
-
 
233
                if(!occlusion1Rect.at<char>(row,col1) || !occlusion1Rect.at<char>(row,col1+1))
-
 
234
                    continue;
-
 
235
 
-
 
236
                float up1Left = up1Rect.at<float>(row,col1);
-
 
237
                float up1Right = up1Rect.at<float>(row,col1+1);
-
 
238
 
-
 
239
                if((up1Left <= up0i) && (up0i <= up1Right)){
-
 
240
 
-
 
241
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
-
 
242
 
-
 
243
                    q0Rect.push_back(cv::Point2f(col, row));
-
 
244
                    q1Rect.push_back(cv::Point2f(col1i, row));
-
 
245
                }
-
 
246
 
-
 
247
            }
-
 
248
 
-
 
249
        }
-
 
250
 
-
 
251
    }
-
 
252
 
-
 
253
    // camera1 against camera0
-
 
254
    //[...]
-
 
255
 
-
 
256
    int nMatches = q0Rect.size();
-
 
257
 
-
 
258
    if(nMatches < 1){
-
 
259
        Q.resize(0);
-
 
260
        color.resize(0);
-
 
261
 
-
 
262
        return;
-
 
263
    }
-
 
264
 
-
 
265
    // Retrieve color information
-
 
266
    color.resize(nMatches);
-
 
267
    for(int i=0; i<nMatches; i++){
-
 
268
 
-
 
269
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
-
 
270
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
-
 
271
 
-
 
272
        color[i] = 0.5*c0 + 0.5*c1;
-
 
273
    }
-
 
274
 
-
 
275
    // Triangulate points
-
 
276
    cv::Mat QMatHomogenous, QMat;
-
 
277
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
-
 
278
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
-
 
279
 
-
 
280
    // Undo rectification
-
 
281
    cv::Mat R0Inv;
-
 
282
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
-
 
283
    QMat = R0Inv*QMat;
-
 
284
 
-
 
285
    cvtools::matToPoints3f(QMat, Q);
-
 
286
 
112
}
287
}