Subversion Repositories seema-scanner

Rev

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

Rev 70 Rev 71
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 = 8;
10
static unsigned int nPhases = 40;
11
static unsigned int nSteps = 8;
11
static unsigned int nSteps = 20;
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 19... Line 19...
19
    const float pi = M_PI;
19
    const float pi = M_PI;
20
 
20
 
21
    // Loop through vector
21
    // Loop through vector
22
    for(int i=0; i<phaseVector.rows; i++){
22
    for(int i=0; i<phaseVector.rows; i++){
23
        // Amplitude of channels
23
        // Amplitude of channels
24
        float amp = 0.5*(1+cos(2*pi*i/pitch + phase));
24
        float amp = 0.5*(1+cos(2*pi*i/pitch - phase));
25
        phaseVector.at<cv::Vec3b>(i, 0) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
25
        phaseVector.at<cv::Vec3b>(i, 0) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
26
    }
26
    }
27
 
27
 
28
    return phaseVector;
28
    return phaseVector;
29
}
29
}
Line 108... Line 108...
108
// Absolute phase and magnitude from N frames
108
// Absolute phase and magnitude from N frames
109
std::vector<cv::Mat> getDFTComponents(const std::vector<cv::Mat> frames){
109
std::vector<cv::Mat> getDFTComponents(const std::vector<cv::Mat> frames){
110
 
110
 
111
    unsigned int N = frames.size();
111
    unsigned int N = frames.size();
112
 
112
 
113
    std::vector<cv::Mat> framesReverse = frames;
113
//    std::vector<cv::Mat> framesReverse = frames;
114
    std::reverse(framesReverse.begin(), framesReverse.end());
114
//    std::reverse(framesReverse.begin(), framesReverse.end());
115
 
115
 
116
    // DFT approach
116
    // DFT approach
117
    cv::Mat I;
117
    cv::Mat I;
118
    cv::merge(frames, I);
118
    cv::merge(frames, I);
119
    unsigned int w = I.cols;
119
    unsigned int w = I.cols;
Line 149... Line 149...
149
        cv::cvtColor(frames1[i], frames1Gray[i], CV_RGB2GRAY);
149
        cv::cvtColor(frames1[i], frames1Gray[i], CV_RGB2GRAY);
150
    }
150
    }
151
 
151
 
152
    // Decode camera0
152
    // Decode camera0
153
    std::vector<cv::Mat> frames0Enc(frames0Gray.begin()+2, frames0Gray.begin()+2+nSteps);
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);
154
    std::vector<cv::Mat> frames0Cue(frames0Gray.begin()+2+nSteps, frames0Gray.begin()+2+nSteps+3);
155
    std::vector<cv::Mat> f0Icomp = getDFTComponents(frames0Enc);
155
    std::vector<cv::Mat> f0Icomp = getDFTComponents(frames0Enc);
156
    cv::Mat up0;
156
    cv::Mat up0;
157
    cv::phase(f0Icomp[2], -f0Icomp[3], up0);
157
    cv::phase(f0Icomp[2], -f0Icomp[3], up0);
-
 
158
cvtools::writeMat(up0, "up0.mat", "up0");
158
    cv::Mat up0Cue = getPhase(frames0Cue[0], frames0Cue[1], frames0Cue[2]);
159
    cv::Mat up0Cue = getPhase(frames0Cue[0], frames0Cue[1], frames0Cue[2]);
159
    up0 = unwrapWithCue(up0, up0Cue, nPhases);
160
    up0 = unwrapWithCue(up0, up0Cue, nPhases);
160
    up0 *= screenCols/(2*pi);
161
    up0 *= screenCols/(2*pi);
161
 
162
 
162
    // Decode camera1
163
    // Decode camera1
163
    std::vector<cv::Mat> frames1Enc(frames1Gray.begin()+2, frames1Gray.begin()+2+nSteps);
164
    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> frames1Cue(frames1Gray.begin()+2+nSteps, frames1Gray.begin()+2+nSteps+3);
165
    std::vector<cv::Mat> f1Icomp = getDFTComponents(frames1Enc);
166
    std::vector<cv::Mat> f1Icomp = getDFTComponents(frames1Enc);
166
    cv::Mat up1;
167
    cv::Mat up1;
167
    cv::phase(f1Icomp[2], -f1Icomp[3], up1);
168
    cv::phase(f1Icomp[2], -f1Icomp[3], up1);
-
 
169
cvtools::writeMat(up1, "up1.mat", "up1");
168
    cv::Mat up1Cue = getPhase(frames1Cue[0], frames1Cue[1], frames1Cue[2]);
170
    cv::Mat up1Cue = getPhase(frames1Cue[0], frames1Cue[1], frames1Cue[2]);
169
    up1 = unwrapWithCue(up1, up1Cue, nPhases);
171
    up1 = unwrapWithCue(up1, up1Cue, nPhases);
170
    up1 *= screenCols/(2*pi);
172
    up1 *= screenCols/(2*pi);
171
 
173
 
-
 
174
cvtools::writeMat(up0Cue, "up0Cue.mat", "up0Cue");
-
 
175
cvtools::writeMat(up1Cue, "up1Cue.mat", "up1Cue");
-
 
176
 
172
    // Rectifying homographies (rotation+projections)
177
    // Rectifying homographies (rotation+projections)
173
    cv::Size frameSize(frameCols, frameRows);
178
    cv::Size frameSize(frameCols, frameRows);
174
    cv::Mat R, T;
179
    cv::Mat R, T;
175
    // stereoRectify segfaults unless R is double precision
180
    // stereoRectify segfaults unless R is double precision
176
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
181
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
Line 186... Line 191...
186
    // Phase remaps
191
    // Phase remaps
187
    cv::Mat up0Rect, up1Rect;
192
    cv::Mat up0Rect, up1Rect;
188
    cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_CUBIC);
193
    cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_CUBIC);
189
    cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_CUBIC);
194
    cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_CUBIC);
190
 
195
 
-
 
196
cvtools::writeMat(up0Rect, "up0Rect.mat", "up0Rect");
-
 
197
cvtools::writeMat(up1Rect, "up1Rect.mat", "up1Rect");
-
 
198
 
191
    // Color remaps
199
    // Color remaps
192
    cv::Mat color0Rect, color1Rect;
200
    cv::Mat color0Rect, color1Rect;
193
    cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
201
    cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
194
    cv::remap(frames1[0], color1Rect, map1X, map1Y, CV_INTER_CUBIC);
202
    cv::remap(frames1[0], color1Rect, map1X, map1Y, CV_INTER_CUBIC);
195
 
203
 
-
 
204
cvtools::writeMat(color0Rect, "color0Rect.mat", "color0Rect");
-
 
205
cvtools::writeMat(color1Rect, "color1Rect.mat", "color1Rect");
-
 
206
 
196
    // On/off remaps
207
    // On/off remaps
197
    cv::Mat frames0OnRect, frames0OffRect;
208
    cv::Mat frames0OnRect, frames0OffRect;
198
    cv::remap(frames0Gray[0], frames0OnRect, map0X, map0Y, CV_INTER_CUBIC);
209
    cv::remap(frames0Gray[0], frames0OnRect, map0X, map0Y, CV_INTER_CUBIC);
199
    cv::remap(frames0Gray[1], frames0OffRect, map0X, map0Y, CV_INTER_CUBIC);
210
    cv::remap(frames0Gray[1], frames0OffRect, map0X, map0Y, CV_INTER_CUBIC);
200
 
211
 
201
    cv::Mat frames1OnRect, frames1OffRect;
212
    cv::Mat frames1OnRect, frames1OffRect;
202
    cv::remap(frames1Gray[0], frames1OnRect, map0X, map0Y, CV_INTER_CUBIC);
213
    cv::remap(frames1Gray[0], frames1OnRect, map1X, map1Y, CV_INTER_CUBIC);
203
    cv::remap(frames1Gray[1], frames1OffRect, map0X, map0Y, CV_INTER_CUBIC);
214
    cv::remap(frames1Gray[1], frames1OffRect, map1X, map1Y, CV_INTER_CUBIC);
204
 
215
 
205
    // Occlusion masks
216
    // Occlusion masks
206
    cv::Mat occlusion0Rect, occlusion1Rect;
217
    cv::Mat occlusion0Rect, occlusion1Rect;
207
    cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
218
    cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
208
    occlusion0Rect = occlusion0Rect > 50;
219
    occlusion0Rect = occlusion0Rect > 50;
209
    cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
220
    cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
210
    occlusion1Rect = occlusion1Rect > 50;
221
    occlusion1Rect = occlusion1Rect > 50;
211
 
222
 
-
 
223
cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
-
 
224
cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
-
 
225
 
212
    // Erode occlusion masks
226
    // Erode occlusion masks
213
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3,3));
227
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3,3));
214
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
228
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
215
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
229
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
216
 
230
 
-
 
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
 
217
    // Match phase maps
243
    // Match phase maps
218
    int frameRectRows = map0X.rows;
244
    int frameRectRows = map0X.rows;
219
    int frameRectCols = map0X.cols;
245
    int frameRectCols = map0X.cols;
220
 
246
 
221
    // camera0 against camera1
247
    // camera0 against camera1
Line 240... Line 266...
240
 
266
 
241
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
267
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
242
 
268
 
243
                    q0Rect.push_back(cv::Point2f(col, row));
269
                    q0Rect.push_back(cv::Point2f(col, row));
244
                    q1Rect.push_back(cv::Point2f(col1i, row));
270
                    q1Rect.push_back(cv::Point2f(col1i, row));
-
 
271
 
-
 
272
                    break;
245
                }
273
                }
246
 
274
 
247
            }
275
            }
248
 
276
 
249
        }
277
        }