Subversion Repositories seema-scanner

Rev

Rev 167 | Rev 179 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 167 Rev 178
Line 144... Line 144...
144
    assert(frames1.size() == N);
144
    assert(frames1.size() == N);
145
 
145
 
146
    int frameRows = frames0[0].rows;
146
    int frameRows = frames0[0].rows;
147
    int frameCols = frames0[0].cols;
147
    int frameCols = frames0[0].cols;
148
 
148
 
-
 
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
 
149
    // Gray-scale everything
163
    // gray-scale and remap
150
    std::vector<cv::Mat> frames0Gray(N);
164
    std::vector<cv::Mat> frames0Rect(N);
151
    std::vector<cv::Mat> frames1Gray(N);
165
    std::vector<cv::Mat> frames1Rect(N);
152
    for(unsigned int i=0; i<N; i++){
166
    for(unsigned int i=0; i<N; i++){
-
 
167
        cv::Mat temp;
153
        cv::cvtColor(frames0[i], frames0Gray[i], CV_BayerBG2GRAY);
168
        cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
-
 
169
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
154
        cv::cvtColor(frames1[i], frames1Gray[i], CV_BayerBG2GRAY);
170
        cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
-
 
171
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
155
    }
172
    }
156
 
173
 
157
    // Decode camera0
174
    // Decode camera0
158
    std::vector<cv::Mat> frames0Primary(frames0Gray.begin()+2, frames0Gray.begin()+2+nStepsPrimary);
175
    std::vector<cv::Mat> frames0Primary(frames0Rect.begin()+2, frames0Rect.begin()+2+nStepsPrimary);
159
    std::vector<cv::Mat> frames0Secondary(frames0Gray.begin()+2+nStepsPrimary, frames0Gray.end());
176
    std::vector<cv::Mat> frames0Secondary(frames0Rect.begin()+2+nStepsPrimary, frames0Rect.end());
160
    std::vector<cv::Mat> F0Primary = getDFTComponents(frames0Primary);
177
    std::vector<cv::Mat> F0Primary = getDFTComponents(frames0Primary);
161
    cv::Mat up0Primary;
178
    cv::Mat up0Primary;
162
    cv::phase(F0Primary[2], -F0Primary[3], up0Primary);
179
    cv::phase(F0Primary[2], -F0Primary[3], up0Primary);
163
    //cv::Mat up0Secondary = getPhase(frames0Secondary[0], frames0Secondary[1], frames0Secondary[2]);
180
    //cv::Mat up0Secondary = getPhase(frames0Secondary[0], frames0Secondary[1], frames0Secondary[2]);
164
    std::vector<cv::Mat> F0Secondary = getDFTComponents(frames0Secondary);
181
    std::vector<cv::Mat> F0Secondary = getDFTComponents(frames0Secondary);
Line 170... Line 187...
170
    up0 *= screenCols/(2.0*pi);
187
    up0 *= screenCols/(2.0*pi);
171
    cv::Mat amplitude0;
188
    cv::Mat amplitude0;
172
    cv::magnitude(F0Primary[2], -F0Primary[3], amplitude0);
189
    cv::magnitude(F0Primary[2], -F0Primary[3], amplitude0);
173
 
190
 
174
    // Decode camera1
191
    // Decode camera1
175
    std::vector<cv::Mat> frames1Primary(frames1Gray.begin()+2, frames1Gray.begin()+2+nStepsPrimary);
192
    std::vector<cv::Mat> frames1Primary(frames1Rect.begin()+2, frames1Rect.begin()+2+nStepsPrimary);
176
    std::vector<cv::Mat> frames1Secondary(frames1Gray.begin()+2+nStepsPrimary, frames1Gray.end());
193
    std::vector<cv::Mat> frames1Secondary(frames1Rect.begin()+2+nStepsPrimary, frames1Rect.end());
177
    std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
194
    std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
178
    cv::Mat up1Primary;
195
    cv::Mat up1Primary;
179
    cv::phase(F1Primary[2], -F1Primary[3], up1Primary);
196
    cv::phase(F1Primary[2], -F1Primary[3], up1Primary);
180
    //cv::Mat up1Secondary = getPhase(frames1Secondary[0], frames1Secondary[1], frames1Secondary[2]);
197
    //cv::Mat up1Secondary = getPhase(frames1Secondary[0], frames1Secondary[1], frames1Secondary[2]);
181
    std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
198
    std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
Line 192... Line 209...
192
//cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
209
//cvtools::writeMat(up0Secondary, "up0Secondary.mat", "up0Secondary");
193
//cvtools::writeMat(up0Equivalent, "up0Equivalent.mat", "up0Equivalent");
210
//cvtools::writeMat(up0Equivalent, "up0Equivalent.mat", "up0Equivalent");
194
//cvtools::writeMat(up0, "up0.mat", "up0");
211
//cvtools::writeMat(up0, "up0.mat", "up0");
195
//cvtools::writeMat(amplitude0, "amplitude0.mat", "amplitude0");
212
//cvtools::writeMat(amplitude0, "amplitude0.mat", "amplitude0");
196
 
213
 
197
    // Rectifying homographies (rotation+projections)
-
 
198
    cv::Size frameSize(frameCols, frameRows);
-
 
199
    cv::Mat R, T;
-
 
200
    // stereoRectify segfaults unless R is double precision
-
 
201
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
-
 
202
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
-
 
203
    cv::Mat R0, R1, P0, P1, QRect;
-
 
204
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
-
 
205
 
-
 
206
    // Interpolation maps (lens distortion and rectification)
-
 
207
    cv::Mat map0X, map0Y, map1X, map1Y;
-
 
208
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
-
 
209
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
-
 
210
 
-
 
211
    // Phase remaps
-
 
212
    cv::Mat up0Rect, up1Rect;
-
 
213
    cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_LINEAR);
-
 
214
    cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_LINEAR);
-
 
215
 
-
 
216
    // amplitude remaps
-
 
217
    cv::Mat amplitude0Rect, amplitude1Rect;
-
 
218
    cv::remap(amplitude0, amplitude0Rect, map0X, map0Y, CV_INTER_LINEAR);
-
 
219
    cv::remap(amplitude1, amplitude1Rect, map1X, map1Y, CV_INTER_LINEAR);
-
 
220
 
-
 
221
//cvtools::writeMat(up0Rect, "up0Rect.mat", "up0Rect");
-
 
222
//cvtools::writeMat(up1Rect, "up1Rect.mat", "up1Rect");
-
 
223
 
-
 
224
    // color debayer and remap
214
    // color debayer and remap
225
    cv::Mat color0Rect, color1Rect;
215
    cv::Mat color0, color1;
226
//    frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
216
//    frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
227
    cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
217
    cv::cvtColor(frames0[0], color0, CV_BayerBG2RGB);
228
    cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
218
    cv::remap(color0, color0, map0X, map0Y, CV_INTER_LINEAR);
229
 
219
 
230
//    frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
220
//    frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
231
    cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
221
    cv::cvtColor(frames1[0], color1, CV_BayerBG2RGB);
232
    cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
222
    cv::remap(color1, color1, map1X, map1Y, CV_INTER_LINEAR);
233
 
223
 
234
//cvtools::writeMat(frames0Rect[18], "frames0Rect_18.mat", "frames0Rect_18");
-
 
235
//cvtools::writeMat(frames0Rect[19], "frames0Rect_19.mat", "frames0Rect_19");
-
 
236
 
-
 
237
//cvtools::writeMat(color0Rect, "color0Rect.mat", "color0Rect");
224
//cvtools::writeMat(color0, "color0.mat", "color0");
238
//cvtools::writeMat(color1Rect, "color1Rect.mat", "color1Rect");
225
//cvtools::writeMat(color1, "color1.mat", "color1");
239
 
-
 
240
    // On/off remaps
-
 
241
    cv::Mat frames0OnRect, frames0OffRect;
-
 
242
    cv::remap(frames0Gray[0], frames0OnRect, map0X, map0Y, CV_INTER_LINEAR);
-
 
243
    cv::remap(frames0Gray[1], frames0OffRect, map0X, map0Y, CV_INTER_LINEAR);
-
 
244
 
-
 
245
    cv::Mat frames1OnRect, frames1OffRect;
-
 
246
    cv::remap(frames1Gray[0], frames1OnRect, map1X, map1Y, CV_INTER_LINEAR);
-
 
247
    cv::remap(frames1Gray[1], frames1OffRect, map1X, map1Y, CV_INTER_LINEAR);
-
 
248
 
226
 
249
    // Occlusion masks
227
    // Occlusion masks
250
    cv::Mat occlusion0Rect, occlusion1Rect;
228
    cv::Mat occlusion0, occlusion1;
251
    cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
229
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0);
252
    occlusion0Rect = (occlusion0Rect > 25) & (occlusion0Rect < 250);
230
    occlusion0 = (occlusion0 > 25) & (occlusion0 < 250);
253
    cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
231
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1);
254
    occlusion1Rect = (occlusion1Rect > 25) & (occlusion1Rect < 250);
232
    occlusion1 = (occlusion1 > 25) & (occlusion1 < 250);
255
 
233
 
256
    // Threshold on energy at primary frequency
234
    // Threshold on energy at primary frequency
257
    occlusion0Rect = occlusion0Rect & (amplitude0Rect > 5.0*nStepsPrimary);
235
    occlusion0 = occlusion0 & (amplitude0 > 5.0*nStepsPrimary);
258
    occlusion1Rect = occlusion1Rect & (amplitude1Rect > 5.0*nStepsPrimary);
236
    occlusion1 = occlusion1 & (amplitude1 > 5.0*nStepsPrimary);
259
 
237
 
260
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
238
//cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
261
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
239
//cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
262
 
240
 
263
    // Erode occlusion masks
241
    // Erode occlusion masks
264
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
242
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
265
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
243
    cv::erode(occlusion0, occlusion0, strel);
266
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
244
    cv::erode(occlusion1, occlusion1, strel);
267
 
245
 
268
    // Threshold on gradient of phase
246
    // Threshold on gradient of phase
269
    cv::Mat edges0;
247
    cv::Mat edges0;
270
    cv::Sobel(up0Rect, edges0, -1, 1, 1, 5);
248
    cv::Sobel(up0, edges0, -1, 1, 1, 5);
271
    occlusion0Rect = occlusion0Rect & (abs(edges0) < 150);
249
    occlusion0 = occlusion0 & (abs(edges0) < 150);
272
 
250
 
273
    cv::Mat edges1;
251
    cv::Mat edges1;
274
    cv::Sobel(up1Rect, edges1, -1, 1, 1, 5);
252
    cv::Sobel(up1, edges1, -1, 1, 1, 5);
275
    occlusion1Rect = occlusion1Rect & (abs(edges1) < 150);
253
    occlusion1 = occlusion1 & (abs(edges1) < 150);
276
 
254
 
277
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
255
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
278
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
256
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
279
 
257
 
280
    // Match phase maps
258
    // Match phase maps
281
    int frameRectRows = map0X.rows;
259
    int frameRectRows = map0X.rows;
282
    int frameRectCols = map0X.cols;
260
    int frameRectCols = map0X.cols;
283
 
261
 
284
    // camera0 against camera1
262
    // camera0 against camera1
285
    std::vector<cv::Vec2f> q0Rect, q1Rect;
263
    std::vector<cv::Vec2f> q0, q1;
286
    for(int row=0; row<frameRectRows; row++){
264
    for(int row=0; row<frameRectRows; row++){
287
        for(int col=0; col<frameRectCols; col++){
265
        for(int col=0; col<frameRectCols; col++){
288
 
266
 
289
            if(!occlusion0Rect.at<char>(row,col))
267
            if(!occlusion0.at<char>(row,col))
290
                continue;
268
                continue;
291
 
269
 
292
            float up0i = up0Rect.at<float>(row,col);
270
            float up0i = up0.at<float>(row,col);
293
            for(int col1=0; col1<up1Rect.cols-1; col1++){
271
            for(int col1=0; col1<up1.cols-1; col1++){
294
 
272
 
295
                if(!occlusion1Rect.at<char>(row,col1) || !occlusion1Rect.at<char>(row,col1+1))
273
                if(!occlusion1.at<char>(row,col1) || !occlusion1.at<char>(row,col1+1))
296
                    continue;
274
                    continue;
297
 
275
 
298
                float up1Left = up1Rect.at<float>(row,col1);
276
                float up1Left = up1.at<float>(row,col1);
299
                float up1Right = up1Rect.at<float>(row,col1+1);
277
                float up1Right = up1.at<float>(row,col1+1);
300
 
278
 
301
                if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 1) && (up1Right-up0i < 1)){
279
                if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 1) && (up1Right-up0i < 1)){
302
 
280
 
303
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
281
                    float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
304
 
282
 
305
                    q0Rect.push_back(cv::Point2f(col, row));
283
                    q0.push_back(cv::Point2f(col, row));
306
                    q1Rect.push_back(cv::Point2f(col1i, row));
284
                    q1.push_back(cv::Point2f(col1i, row));
307
 
285
 
308
                    break;
286
                    break;
309
                }
287
                }
310
            }
288
            }
311
        }
289
        }
312
    }
290
    }
313
 
291
 
314
//    // camera1 against camera0
-
 
315
//    for(int row=0; row<frameRectRows; row++){
-
 
316
//        for(int col=0; col<frameRectCols; col++){
-
 
317
 
-
 
318
//            if(!occlusion1Rect.at<char>(row,col))
-
 
319
//                continue;
-
 
320
 
-
 
321
//            float up1i = up1Rect.at<float>(row,col);
-
 
322
//            for(int col0=0; col0<up0Rect.cols-1; col0++){
-
 
323
 
-
 
324
//                if(!occlusion0Rect.at<char>(row,col0) || !occlusion0Rect.at<char>(row,col0+1))
-
 
325
//                    continue;
-
 
326
 
-
 
327
//                float up0Left = up0Rect.at<float>(row,col0);
-
 
328
//                float up0Right = up0Rect.at<float>(row,col0+1);
-
 
329
 
-
 
330
//                if((up0Left <= up1i) && (up1i <= up0Right) && (up1i-up0Left < 1) && (up0Right-up1i < 1)){
-
 
331
 
-
 
332
//                    float col0i = col0 + (up1i-up0Left)/(up0Right-up0Left);
-
 
333
 
-
 
334
//                    q1Rect.push_back(cv::Point2f(col, row));
-
 
335
//                    q0Rect.push_back(cv::Point2f(col0i, row));
-
 
336
 
-
 
337
//                    break;
-
 
338
//                }
-
 
339
//            }
-
 
340
//        }
-
 
341
//    }
-
 
342
 
292
 
343
    int nMatches = q0Rect.size();
293
    int nMatches = q0.size();
344
 
294
 
345
    if(nMatches < 1){
295
    if(nMatches < 1){
346
        Q.resize(0);
296
        Q.resize(0);
347
        color.resize(0);
297
        color.resize(0);
348
 
298
 
Line 351... Line 301...
351
 
301
 
352
    // Retrieve color information
302
    // Retrieve color information
353
    color.resize(nMatches);
303
    color.resize(nMatches);
354
    for(int i=0; i<nMatches; i++){
304
    for(int i=0; i<nMatches; i++){
355
 
305
 
356
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
306
        cv::Vec3b c0 = color0.at<cv::Vec3b>(q0[i][1], q0[i][0]);
357
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
307
        cv::Vec3b c1 = color1.at<cv::Vec3b>(q1[i][1], q1[i][0]);
358
 
308
 
359
        color[i] = 0.5*c0 + 0.5*c1;
309
        color[i] = 0.5*c0 + 0.5*c1;
360
    }
310
    }
361
 
311
 
362
    // Triangulate points
312
    // Triangulate points
363
    cv::Mat QMatHomogenous, QMat;
313
    cv::Mat QMatHomogenous, QMat;
364
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
314
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
365
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
315
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
366
 
316
 
367
    // Undo rectification
317
    // Undo rectification
368
    cv::Mat R0Inv;
318
    cv::Mat R0Inv;
369
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
319
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);