Subversion Repositories seema-scanner

Rev

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

Rev 108 Rev 109
Line 153... Line 153...
153
 
153
 
154
 
154
 
155
typedef struct intersection{
155
typedef struct intersection{
156
    unsigned int row;
156
    unsigned int row;
157
    unsigned int col;
157
    unsigned int col;
-
 
158
    // 64 bit integer (long long)
158
    unsigned long id;
159
    unsigned long id;
159
    intersection() : row(0), col(0), id(0){}
160
    intersection() : row(0), col(0), id(0){}
160
    intersection(unsigned int _row, unsigned int _col, unsigned long long _id) :
161
    intersection(unsigned int _row, unsigned int _col, unsigned long long _id) :
161
        row(_row), col(_col), id(_id){}
162
        row(_row), col(_col), id(_id){}
162
} intersection;
163
} intersection;
Line 174... Line 175...
174
    int labelVert;
175
    int labelVert;
175
    int labelHorzRight;
176
    int labelHorzRight;
176
    int labelVertBelow;
177
    int labelVertBelow;
177
 
178
 
178
    // collect intersections
179
    // collect intersections
179
    for(int row=0; row<nRows+1; row++){
180
    for(int row=0; row<nRows-1; row++){
180
        for(int col=0; col<nCols+1; col++){
181
        for(int col=0; col<nCols-1; col++){
181
 
182
 
182
            labelHorz = codeHorz.at<int>(row, col);
183
            labelHorz = codeHorz.at<int>(row, col);
183
            labelHorzRight = codeHorz.at<int>(row, col+1);
184
            labelHorzRight = codeHorz.at<int>(row, col+1);
184
 
185
 
185
            labelVert = codeVert.at<int>(row, col);
186
            labelVert = codeVert.at<int>(row, col);
Line 190... Line 191...
190
               countBits(labelHorz^labelHorzRight) == 1 &&
191
               countBits(labelHorz^labelHorzRight) == 1 &&
191
               labelVert != -1 && labelVertBelow != -1 &&
192
               labelVert != -1 && labelVertBelow != -1 &&
192
               countBits(labelVert^labelVertBelow) == 1){
193
               countBits(labelVert^labelVertBelow) == 1){
193
 
194
 
194
                // shift together labels to form unique intersection id
195
                // shift together labels to form unique intersection id
195
                unsigned long id = (labelHorz << NbitsHorz+2*NbitsVert) + (labelHorzRight << 2*NbitsVert) +
196
                unsigned long id = ((ulong)labelHorz << (NbitsHorz+2*NbitsVert)) + ((ulong)labelHorzRight << (2*NbitsVert)) +
196
                                   (labelVert << NbitsVert) + labelVertBelow;
197
                                   ((ulong)labelVert << NbitsVert) + (ulong)labelVertBelow;
197
 
198
 
198
                // store intersection
199
                // store intersection
199
                intersections.push_back(intersection(row, col, id));
200
                intersections.push_back(intersection(row, col, id));
200
            }
201
            }
201
        }
202
        }
Line 216... Line 217...
216
 
217
 
217
    // subpixel refinement finds the intersection of linear interpolants in the positive/negative pattern
218
    // subpixel refinement finds the intersection of linear interpolants in the positive/negative pattern
218
    for(int i=0; i<nMatches; i++){
219
    for(int i=0; i<nMatches; i++){
219
 
220
 
220
        // shift the labels back out from id
221
        // shift the labels back out from id
221
        int labelHorz = (matches[i].id >> NbitsHorz+2*NbitsVert) & (1 << (NbitsHorz+1) - 1);
222
        int labelHorz = (matches[i].id >> (NbitsHorz+2*NbitsVert)) & ((1 << NbitsHorz) - 1);
222
        int labelHorzRight = (matches[i].id >> 2*NbitsVert) & (1 << (NbitsHorz+1) - 1);
223
        int labelHorzRight = (matches[i].id >> (2*NbitsVert)) & ((1 << NbitsHorz) - 1);
223
        int labelVert = (matches[i].id >> NbitsVert) & (1 << (NbitsVert+1) - 1);
224
        int labelVert = (matches[i].id >> NbitsVert) & ((1 << NbitsVert) - 1);
224
        int labelVertBelow = matches[i].id  & (1 << (NbitsVert+1) - 1);
225
        int labelVertBelow = matches[i].id  & ((1 << NbitsVert) - 1);
-
 
226
 
-
 
227
//        std::cout << "id: " << matches[i].id << std::endl;
-
 
228
//        std::cout << "labelHorz: " << labelHorz << std::endl;
-
 
229
//        std::cout << "labelHorzRight: " << labelHorzRight << std::endl;
-
 
230
//        std::cout << "labelVert: " << labelVert << std::endl;
-
 
231
//        std::cout << "labelVertBelow: " << labelVertBelow << std::endl;
225
 
232
 
226
        // determine the levels at which the edges exists
233
        // determine the levels at which the edges exists
227
        int levelHorz = NbitsHorz - leastSignificantBitSet(labelHorz^labelHorzRight);
234
        int levelHorz = NbitsHorz - leastSignificantBitSet(labelHorz^labelHorzRight);
228
        int levelVert = NbitsVert - leastSignificantBitSet(labelVert^labelVertBelow);
235
        int levelVert = NbitsVert - leastSignificantBitSet(labelVert^labelVertBelow);
229
 
236
 
230
        // interpolate horizontal coordinate
237
        // interpolate horizontal coordinate
231
        float row = matches[i].row;
238
        float row = matches[i].row;
232
        float col = matches[i].col;
239
        float col = matches[i].col;
233
        float colRight = col+1;
240
        float colRight = col+1;
234
 
241
 
235
        float posHorz = frames0[2*levelHorz+2].at<char>(row, col);
242
        float posHorz = frames[2*levelHorz+2].at<char>(row, col);
236
        float negHorz = frames0[2*levelHorz+3].at<char>(row, col);
243
        float negHorz = frames[2*levelHorz+3].at<char>(row, col);
237
        float posHorzRight = frames0[2*levelHorz+2].at<char>(row, colRight);
244
        float posHorzRight = frames[2*levelHorz+2].at<char>(row, colRight);
238
        float negHorzRight = frames0[2*levelHorz+3].at<char>(row, colRight);
245
        float negHorzRight = frames[2*levelHorz+3].at<char>(row, colRight);
239
 
246
 
240
        float x = col + (posHorz - negHorz)/(negHorzRight - negHorz - posHorzRight + posHorz);
247
        float x = col + (posHorz - negHorz)/(negHorzRight - negHorz - posHorzRight + posHorz);
241
 
248
 
242
        // interpolate vertical coordinate
249
        // interpolate vertical coordinate
243
        float rowBelow = row+1;
250
        float rowBelow = row+1;
244
 
251
 
245
        float posVert = frames[2*NbitsHorz+2*levelVert+2].at<char>(rowBelow, col);
252
        float posVert = frames[2*NbitsHorz+2*levelVert+2].at<char>(row, col);
246
        float negVert = frames[2*NbitsHorz+2*levelVert+3].at<char>(rowBelow, col);
253
        float negVert = frames[2*NbitsHorz+2*levelVert+3].at<char>(row, col);
247
        float posVertBelow = frames[2*NbitsHorz+2*levelVert+2].at<char>(rowBelow, col);
254
        float posVertBelow = frames[2*NbitsHorz+2*levelVert+2].at<char>(rowBelow, col);
248
        float negVertBelow = frames[2*NbitsHorz+2*levelVert+3].at<char>(rowBelow, col);
255
        float negVertBelow = frames[2*NbitsHorz+2*levelVert+3].at<char>(rowBelow, col);
249
 
256
 
250
        float y = row + (posVert - negVert)/(negVertBelow - negVert - posVertBelow + posVert);
257
        float y = row + (posVert - negVert)/(negVertBelow - negVert - posVertBelow + posVert);
251
 
258
 
252
        // write into return vector
259
        // write into return vector
253
        q[i] = cv::Vec2f(x, y);
260
        q[i] = cv::Point2f(x, y);
254
 
261
 
255
    }
262
    }
256
}
263
}
257
 
264
 
258
static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
265
static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
Line 337... Line 344...
337
    }
344
    }
338
 
345
 
339
    // vertical codes into gray code
346
    // vertical codes into gray code
340
    for(int i=0; i<NbitsVert; i++){
347
    for(int i=0; i<NbitsVert; i++){
341
        cv::Mat bit0;
348
        cv::Mat bit0;
342
        cv::subtract(frames0Gray[i*2+NbitsHorz+2], frames0Gray[i*2+NbitsHorz+3], bit0);
349
        cv::subtract(frames0Gray[i*2+2*NbitsHorz+2], frames0Gray[i*2+2*NbitsHorz+3], bit0);
343
        bit0 = bit0 > 0;
350
        bit0 = bit0 > 0;
344
        bit0.convertTo(bit0, CV_32S, 1.0/255.0);
351
        bit0.convertTo(bit0, CV_32S, 1.0/255.0);
345
        cv::add(code0Vert, bit0*twopowi(NbitsVert-i-1), code0Vert, cv::Mat(), CV_32S);
352
        cv::add(code0Vert, bit0*twopowi(NbitsVert-i-1), code0Vert, cv::Mat(), CV_32S);
346
 
353
 
347
        cv::Mat bit1;
354
        cv::Mat bit1;
348
        cv::subtract(frames1Gray[i*2+NbitsHorz+2], frames1Gray[i*2+NbitsHorz+3], bit1);
355
        cv::subtract(frames1Gray[i*2+2*NbitsHorz+2], frames1Gray[i*2+2*NbitsHorz+3], bit1);
349
        bit1 = bit1 > 0;
356
        bit1 = bit1 > 0;
350
        bit1.convertTo(bit1, CV_32S, 1.0/255.0);
357
        bit1.convertTo(bit1, CV_32S, 1.0/255.0);
351
        cv::add(code1Vert, bit1*twopowi(NbitsVert-i-1), code1Vert, cv::Mat(), CV_32S);
358
        cv::add(code1Vert, bit1*twopowi(NbitsVert-i-1), code1Vert, cv::Mat(), CV_32S);
352
    }
359
    }
353
 
360
 
354
//cvtools::writeMat(code0Horz, "code0Horz.mat", "code0Horz");
-
 
355
//cvtools::writeMat(code1Horz, "code1Horz.mat", "code1Horz");
-
 
356
//cvtools::writeMat(code0Vert, "code0Vert.mat", "code0Vert");
-
 
357
//cvtools::writeMat(code1Vert, "code1Vert.mat", "code1Vert");
-
 
358
 
-
 
359
    // set occluded pixels to -1
361
    // set occluded pixels to -1
360
    for(int r=0; r<frameRows; r++){
362
    for(int r=0; r<frameRows; r++){
361
        for(int c=0; c<frameCols; c++){
363
        for(int c=0; c<frameCols; c++){
362
            if(occlusion0.at<char>(r,c) == 0){
364
            if(occlusion0.at<char>(r,c) == 0){
363
                code0Horz.at<float>(r,c) = -1;
365
                code0Horz.at<int>(r,c) = -1;
364
                code0Vert.at<float>(r,c) = -1;
366
                code0Vert.at<int>(r,c) = -1;
365
            }
367
            }
366
            if(occlusion1.at<char>(r,c) == 0){
368
            if(occlusion1.at<char>(r,c) == 0){
367
                code1Horz.at<float>(r,c) = -1;
369
                code1Horz.at<int>(r,c) = -1;
368
                code1Vert.at<float>(r,c) = -1;
370
                code1Vert.at<int>(r,c) = -1;
369
            }
371
            }
370
        }
372
        }
371
    }
373
    }
372
 
374
 
-
 
375
//    cvtools::writeMat(code0Horz, "code0Horz.mat", "code0Horz");
-
 
376
//    cvtools::writeMat(code1Horz, "code1Horz.mat", "code1Horz");
373
    // matching
377
//    cvtools::writeMat(code0Vert, "code0Vert.mat", "code0Vert");
374
    std::vector<intersection> intersections0, intersections1;
378
//    cvtools::writeMat(code1Vert, "code1Vert.mat", "code1Vert");
375
 
379
 
-
 
380
    // get intersections
376
    // intersection data structure containing [floor(row), floor(column), id]
381
    std::vector<intersection> intersections0, intersections1;
377
    getIntersectionLabels(code0Horz, code0Vert, NbitsHorz, NbitsVert, intersections0);
382
    getIntersectionLabels(code0Horz, code0Vert, NbitsHorz, NbitsVert, intersections0);
378
    getIntersectionLabels(code1Horz, code1Vert, NbitsHorz, NbitsVert, intersections1);
383
    getIntersectionLabels(code1Horz, code1Vert, NbitsHorz, NbitsVert, intersections1);
379
 
384
 
380
    // match intersections
385
    // match intersections
381
    std::vector<intersection> matches0, matches1;
386
    std::vector<intersection> matches0, matches1;
Line 404... Line 409...
404
    }
409
    }
405
 
410
 
406
    std::vector<cv::Point2f> q0(nMatches), q1(nMatches);
411
    std::vector<cv::Point2f> q0(nMatches), q1(nMatches);
407
 
412
 
408
//    for(int i=0; i<nMatches; i++){
413
//    for(int i=0; i<nMatches; i++){
409
//        q0[i] = cv::Vec2f(matches0[i].col, matches0[i].row);
414
//        q0[i] = cv::Point2f(matches0[i].col, matches0[i].row);
410
//        q1[i] = cv::Vec2f(matches1[i].col, matches1[i].row);
415
//        q1[i] = cv::Point2f(matches1[i].col, matches1[i].row);
411
//    }
416
//    }
412
 
417
 
413
    // subpixel refinement
418
    // subpixel refinement
414
    getSubpixelCoordinates(matches0, frames0, NbitsHorz, NbitsVert,q0);
419
    getSubpixelCoordinates(matches0, frames0Gray, NbitsHorz, NbitsVert, q0);
415
    getSubpixelCoordinates(matches1, frames1, NbitsHorz, NbitsVert,q1);
420
    getSubpixelCoordinates(matches1, frames1Gray, NbitsHorz, NbitsVert, q1);
416
 
-
 
417
 
421
 
418
    // retrieve color information (at subpixel coordinates)
422
    // retrieve color information (at subpixel coordinates)
419
    color.resize(nMatches);
423
    color.resize(nMatches);
420
    for(int i=0; i<nMatches; i++){
424
    for(int i=0; i<nMatches; i++){
421
 
-
 
422
//        cv::Vec3b c0 = color0.at<cv::Vec3b>(q0[i][1], q0[i][0]);
425
        cv::Vec3b c0 = color0.at<cv::Vec3b>(std::floor(q0[i].y), std::floor(q0[i].x));
423
//        cv::Vec3b c1 = color1.at<cv::Vec3b>(q1[i][1], q1[i][0]);
426
        cv::Vec3b c1 = color1.at<cv::Vec3b>(std::floor(q1[i].y), std::floor(q1[i].x));
424
        cv::Vec3b c0 = getColorSubpix(color0, q0[i]);
427
//        cv::Vec3b c0 = getColorSubpix(color0, q0[i]);
425
        cv::Vec3b c1 = getColorSubpix(color1, q1[i]);
428
//        cv::Vec3b c1 = getColorSubpix(color1, q1[i]);
426
 
429
 
427
        color[i] = 0.5*c0 + 0.5*c1;
430
        color[i] = 0.5*c0 + 0.5*c1;
428
    }
431
    }
429
 
432
 
430
    // triangulate points
433
    // triangulate points
Line 434... Line 437...
434
    cv::Mat P1(3, 4, CV_32F), temp(3,4,CV_32F);
437
    cv::Mat P1(3, 4, CV_32F), temp(3,4,CV_32F);
435
    cv::Mat(calibration.R1).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
438
    cv::Mat(calibration.R1).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
436
    cv::Mat(calibration.T1).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
439
    cv::Mat(calibration.T1).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
437
    P1 = cv::Mat(calibration.K1) * temp;
440
    P1 = cv::Mat(calibration.K1) * temp;
438
 
441
 
439
    //cv::correctMatches(calibration.F, q0, q1, q0, q1);
442
    cv::correctMatches(calibration.F, q0, q1, q0, q1);
440
 
443
 
441
    cv::Mat QMatHomogenous, QMat;
444
    cv::Mat QMatHomogenous, QMat;
442
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
445
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
443
 
446
 
444
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
447
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);