Subversion Repositories seema-scanner

Rev

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

Rev 73 Rev 74
Line 5... Line 5...
5
 
5
 
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 = 50;
10
static unsigned int nSteps = 20; // number of shifts/steps in primary
11
static unsigned int nSteps = 10;
11
static float pPrimary = 200; // primary period
12
 
12
 
13
// Algorithm
13
// Algorithm
14
static cv::Mat computePhaseVector(unsigned int length, float phase, float pitch){
14
static cv::Mat computePhaseVector(unsigned int length, float phase, float pitch){
15
 
15
 
16
    cv::Mat phaseVector(length, 1, CV_8UC3);
16
    cv::Mat phaseVector(length, 1, CV_8UC3);
Line 29... Line 29...
29
}
29
}
30
 
30
 
31
AlgorithmPhaseShift::AlgorithmPhaseShift(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
31
AlgorithmPhaseShift::AlgorithmPhaseShift(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
32
 
32
 
33
    // Set N
33
    // Set N
34
    N = 2+2*nSteps;
34
    N = 2+nSteps+3;
-
 
35
 
-
 
36
    // Determine the secondary (wider) period
-
 
37
    float pSecondary = (screenCols*pPrimary)/(screenCols-pPrimary);
35
 
38
 
36
    // all on pattern
39
    // all on pattern
37
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
40
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
38
    patterns.push_back(allOn);
41
    patterns.push_back(allOn);
39
 
42
 
Line 42... Line 45...
42
    patterns.push_back(allOff);
45
    patterns.push_back(allOff);
43
 
46
 
44
    // Precompute encoded patterns
47
    // Precompute encoded patterns
45
    const float pi = M_PI;
48
    const float pi = M_PI;
46
 
49
 
47
    // Horizontally encoding patterns
50
    // Primary encoding patterns
48
    for(unsigned int i=0; i<nSteps; i++){
51
    for(unsigned int i=0; i<nSteps; i++){
49
        float phase = 2.0*pi/nSteps * i;
52
        float phase = 2.0*pi/nSteps * i;
50
        float pitch = (float)screenCols/(float)nPhases;
53
        float pitch = pPrimary;
51
        cv::Mat patternI(1,1,CV_8U);
54
        cv::Mat patternI(1,1,CV_8U);
52
        patternI = computePhaseVector(screenCols, phase, pitch);
55
        patternI = computePhaseVector(screenCols, phase, pitch);
53
        patterns.push_back(patternI.t());
56
        patterns.push_back(patternI.t());
54
    }
57
    }
55
 
58
 
56
    // Phase cue patterns
59
    // Secondary encoding patterns
57
    for(unsigned int i=0; i<nSteps; i++){
60
    for(unsigned int i=0; i<3; i++){
58
        float phase = 2.0*pi/nSteps * i;
61
        float phase = 2.0*pi/3 * i;
59
        float pitch = screenCols;
62
        float pitch = pSecondary;
60
        cv::Mat patternI(1,1,CV_8U);
63
        cv::Mat patternI(1,1,CV_8U);
61
        patternI = computePhaseVector(screenCols, phase, pitch);
64
        patternI = computePhaseVector(screenCols, phase, pitch);
62
        patterns.push_back(patternI.t());
65
        patterns.push_back(patternI.t());
63
    }
66
    }
64
 
67
 
Line 84... Line 87...
84
    return phase;
87
    return phase;
85
 
88
 
86
}
89
}
87
 
90
 
88
// Phase unwrapping by means of a phase cue
91
// Phase unwrapping by means of a phase cue
89
cv::Mat unwrapWithCue(const cv::Mat up, const cv::Mat upCue, unsigned int nPhases){
92
cv::Mat unwrapWithCue(const cv::Mat up, const cv::Mat upCue, float nPhases){
90
 
93
 
91
    const float pi = M_PI;
94
    const float pi = M_PI;
92
 
95
 
93
    // Determine number of jumps
96
    // Determine number of jumps
94
    cv::Mat P = (upCue*nPhases-up)/(2*pi);
97
    cv::Mat P = (upCue*nPhases-up)/(2*pi);
Line 149... Line 152...
149
        cv::cvtColor(frames0[i], frames0Gray[i], CV_RGB2GRAY);
152
        cv::cvtColor(frames0[i], frames0Gray[i], CV_RGB2GRAY);
150
        cv::cvtColor(frames1[i], frames1Gray[i], CV_RGB2GRAY);
153
        cv::cvtColor(frames1[i], frames1Gray[i], CV_RGB2GRAY);
151
    }
154
    }
152
 
155
 
153
    // Decode camera0
156
    // Decode camera0
154
    std::vector<cv::Mat> frames0Enc(frames0Gray.begin()+2, frames0Gray.begin()+2+nSteps);
157
    std::vector<cv::Mat> frames0Primary(frames0Gray.begin()+2, frames0Gray.begin()+2+nSteps);
155
    std::vector<cv::Mat> frames0Cue(frames0Gray.begin()+2+nSteps, frames0Gray.begin()+2+nSteps+nSteps);
158
    std::vector<cv::Mat> frames0Secondary(frames0Gray.begin()+2+nSteps, frames0Gray.begin()+2+nSteps+3);
156
    std::vector<cv::Mat> F0Enc = getDFTComponents(frames0Enc);
159
    std::vector<cv::Mat> F0Enc = getDFTComponents(frames0Primary);
157
    cv::Mat up0;
160
    cv::Mat up0Primary;
158
    cv::phase(F0Enc[2], -F0Enc[3], up0);
161
    cv::phase(F0Enc[2], -F0Enc[3], up0Primary);
159
cvtools::writeMat(up0, "up0.mat", "up0");
-
 
160
    std::vector<cv::Mat> F0Cue = getDFTComponents(frames0Cue);
162
    cv::Mat up0Secondary = getPhase(frames0Secondary[0], frames0Secondary[1], frames0Secondary[2]);
161
    cv::Mat up0Cue;
163
    cv::Mat up0Equivalent = up0Primary - up0Secondary;
162
    cv::phase(F0Cue[2], -F0Cue[3], up0Cue);
164
    up0Equivalent = cvtools::modulo(up0Equivalent, 2*pi);
163
    up0 = unwrapWithCue(up0, up0Cue, nPhases);
165
    cv::Mat up0 = unwrapWithCue(up0Primary, up0Equivalent, (float)screenCols/pPrimary);
164
    up0 *= screenCols/(2*pi);
166
    up0 *= screenCols/(2*pi);
165
 
167
 
166
    // Decode camera1
168
    // Decode camera1
167
    std::vector<cv::Mat> frames1Enc(frames1Gray.begin()+2, frames1Gray.begin()+2+nSteps);
169
    std::vector<cv::Mat> frames1Primary(frames1Gray.begin()+2, frames1Gray.begin()+2+nSteps);
168
    std::vector<cv::Mat> frames1Cue(frames1Gray.begin()+2+nSteps, frames1Gray.begin()+2+nSteps+nSteps);
170
    std::vector<cv::Mat> frames1Secondary(frames1Gray.begin()+2+nSteps, frames1Gray.begin()+2+nSteps+3);
169
    std::vector<cv::Mat> f1Icomp = getDFTComponents(frames1Enc);
171
    std::vector<cv::Mat> F1Enc = getDFTComponents(frames1Primary);
170
    cv::Mat up1;
172
    cv::Mat up1Primary;
171
    cv::phase(f1Icomp[2], -f1Icomp[3], up1);
173
    cv::phase(F1Enc[2], -F1Enc[3], up1Primary);
172
cvtools::writeMat(up1, "up1.mat", "up1");
-
 
173
    std::vector<cv::Mat> F1Cue = getDFTComponents(frames1Cue);
174
    cv::Mat up1Secondary = getPhase(frames1Secondary[0], frames1Secondary[1], frames1Secondary[2]);
174
    cv::Mat up1Cue;
175
    cv::Mat up1Equivalent = up1Primary - up1Secondary;
175
    cv::phase(F1Cue[2], -F1Cue[3], up1Cue);
176
    up1Equivalent = cvtools::modulo(up1Equivalent, 2*pi);
176
    up1 = unwrapWithCue(up1, up1Cue, nPhases);
177
    cv::Mat up1 = unwrapWithCue(up1Primary, up1Equivalent, (float)screenCols/pPrimary);
177
    up1 *= screenCols/(2*pi);
178
    up1 *= screenCols/(2*pi);
178
 
179
 
179
cvtools::writeMat(up0Cue, "up0Cue.mat", "up0Cue");
-
 
180
cvtools::writeMat(up1Cue, "up1Cue.mat", "up1Cue");
-
 
181
 
180
 
182
    // Rectifying homographies (rotation+projections)
181
    // Rectifying homographies (rotation+projections)
183
    cv::Size frameSize(frameCols, frameRows);
182
    cv::Size frameSize(frameCols, frameRows);
184
    cv::Mat R, T;
183
    cv::Mat R, T;
185
    // stereoRectify segfaults unless R is double precision
184
    // stereoRectify segfaults unless R is double precision
Line 196... Line 195...
196
    // Phase remaps
195
    // Phase remaps
197
    cv::Mat up0Rect, up1Rect;
196
    cv::Mat up0Rect, up1Rect;
198
    cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_CUBIC);
197
    cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_CUBIC);
199
    cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_CUBIC);
198
    cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_CUBIC);
200
 
199
 
201
cvtools::writeMat(up0Rect, "up0Rect.mat", "up0Rect");
200
//cvtools::writeMat(up0Rect, "up0Rect.mat", "up0Rect");
202
cvtools::writeMat(up1Rect, "up1Rect.mat", "up1Rect");
201
//cvtools::writeMat(up1Rect, "up1Rect.mat", "up1Rect");
203
 
202
 
204
    // Color remaps
203
    // Color remaps
205
    cv::Mat color0Rect, color1Rect;
204
    cv::Mat color0Rect, color1Rect;
206
    cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
205
    cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
207
    cv::remap(frames1[0], color1Rect, map1X, map1Y, CV_INTER_CUBIC);
206
    cv::remap(frames1[0], color1Rect, map1X, map1Y, CV_INTER_CUBIC);
208
 
207
 
209
cvtools::writeMat(color0Rect, "color0Rect.mat", "color0Rect");
208
//cvtools::writeMat(color0Rect, "color0Rect.mat", "color0Rect");
210
cvtools::writeMat(color1Rect, "color1Rect.mat", "color1Rect");
209
//cvtools::writeMat(color1Rect, "color1Rect.mat", "color1Rect");
211
 
210
 
212
    // On/off remaps
211
    // On/off remaps
213
    cv::Mat frames0OnRect, frames0OffRect;
212
    cv::Mat frames0OnRect, frames0OffRect;
214
    cv::remap(frames0Gray[0], frames0OnRect, map0X, map0Y, CV_INTER_CUBIC);
213
    cv::remap(frames0Gray[0], frames0OnRect, map0X, map0Y, CV_INTER_CUBIC);
215
    cv::remap(frames0Gray[1], frames0OffRect, map0X, map0Y, CV_INTER_CUBIC);
214
    cv::remap(frames0Gray[1], frames0OffRect, map0X, map0Y, CV_INTER_CUBIC);
Line 223... Line 222...
223
    cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
222
    cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
224
    occlusion0Rect = occlusion0Rect > 50;
223
    occlusion0Rect = occlusion0Rect > 50;
225
    cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
224
    cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
226
    occlusion1Rect = occlusion1Rect > 50;
225
    occlusion1Rect = occlusion1Rect > 50;
227
 
226
 
-
 
227
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
-
 
228
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
-
 
229
 
228
    // Erode occlusion masks
230
    // Erode occlusion masks
229
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3,3));
231
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
230
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
232
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
231
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
233
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
232
 
234
 
233
    // Threshold on gradient of phase
235
    // Threshold on gradient of phase
234
    cv::Mat edges0;
236
    cv::Mat edges0;
Line 237... Line 239...
237
 
239
 
238
    cv::Mat edges1;
240
    cv::Mat edges1;
239
    cv::Sobel(up1Rect, edges1, -1, 1, 0, 5);
241
    cv::Sobel(up1Rect, edges1, -1, 1, 0, 5);
240
    occlusion1Rect = occlusion1Rect & (abs(edges1) < 150);
242
    occlusion1Rect = occlusion1Rect & (abs(edges1) < 150);
241
 
243
 
242
cvtools::writeMat(edges0, "edges0.mat", "edges0");
244
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
243
cvtools::writeMat(edges1, "edges1.mat", "edges1");
245
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
244
 
-
 
245
cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
-
 
246
cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
-
 
247
 
246
 
248
    // Match phase maps
247
    // Match phase maps
249
    int frameRectRows = map0X.rows;
248
    int frameRectRows = map0X.rows;
250
    int frameRectCols = map0X.cols;
249
    int frameRectCols = map0X.cols;
251
 
250
 
252
    // camera0 against camera1
251
    // camera0 against camera1
253
    std::vector<cv::Vec2f> q0Rect, q1Rect;
252
    std::vector<cv::Vec2f> q0Rect, q1Rect;
254
    for(int row=0; row<frameRectRows; row++){
253
    for(int row=0; row<frameRectRows; row++){
255
 
-
 
256
        for(int col=0; col<frameRectCols; col++){
254
        for(int col=0; col<frameRectCols; col++){
257
 
255
 
258
            if(!occlusion0Rect.at<char>(row,col))
256
            if(!occlusion0Rect.at<char>(row,col))
259
                continue;
257
                continue;
260
 
258
 
Line 265... Line 263...
265
                    continue;
263
                    continue;
266
 
264
 
267
                float up1Left = up1Rect.at<float>(row,col1);
265
                float up1Left = up1Rect.at<float>(row,col1);
268
                float up1Right = up1Rect.at<float>(row,col1+1);
266
                float up1Right = up1Rect.at<float>(row,col1+1);
269
 
267
 
270
                if((up1Left <= up0i) && (up0i <= up1Right)){
268
                if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 1) && (up1Right-up0i < 1)){
271
 
269
 
272
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
270
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
273
 
271
 
274
                    q0Rect.push_back(cv::Point2f(col, row));
272
                    q0Rect.push_back(cv::Point2f(col, row));
275
                    q1Rect.push_back(cv::Point2f(col1i, row));
273
                    q1Rect.push_back(cv::Point2f(col1i, row));
276
 
274
 
277
                    break;
275
                    break;
278
                }
276
                }
279
 
-
 
280
            }
277
            }
281
 
-
 
282
        }
278
        }
283
 
-
 
284
    }
279
    }
285
 
280
 
286
    // camera1 against camera0
281
    // camera1 against camera0
-
 
282
    for(int row=0; row<frameRectRows; row++){
-
 
283
        for(int col=0; col<frameRectCols; col++){
-
 
284
 
-
 
285
            if(!occlusion1Rect.at<char>(row,col))
-
 
286
                continue;
-
 
287
 
-
 
288
            float up1i = up1Rect.at<float>(row,col);
-
 
289
            for(int col0=0; col0<up0Rect.cols-1; col0++){
-
 
290
 
-
 
291
                if(!occlusion0Rect.at<char>(row,col0) || !occlusion0Rect.at<char>(row,col0+1))
-
 
292
                    continue;
-
 
293
 
-
 
294
                float up0Left = up0Rect.at<float>(row,col0);
-
 
295
                float up0Right = up0Rect.at<float>(row,col0+1);
-
 
296
 
-
 
297
                if((up0Left <= up1i) && (up1i <= up0Right) && (up1i-up0Left < 1) && (up0Right-up1i < 1)){
-
 
298
 
-
 
299
                    float col0i = col0 + (up1i-up0Left)/(up0Right-up0Left);
-
 
300
 
-
 
301
                    q1Rect.push_back(cv::Point2f(col, row));
-
 
302
                    q0Rect.push_back(cv::Point2f(col0i, row));
-
 
303
 
-
 
304
                    break;
-
 
305
                }
-
 
306
            }
287
    //[...]
307
        }
-
 
308
    }
288
 
309
 
289
    int nMatches = q0Rect.size();
310
    int nMatches = q0Rect.size();
290
 
311
 
291
    if(nMatches < 1){
312
    if(nMatches < 1){
292
        Q.resize(0);
313
        Q.resize(0);