Subversion Repositories seema-scanner

Rev

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

Rev 91 Rev 119
Line 6... Line 6...
6
 
6
 
7
#include <stdio.h>
7
#include <stdio.h>
8
 
8
 
9
namespace cvtools{
9
namespace cvtools{
10
 
10
 
-
 
11
// Create a mask for feature matching which disallows matches not satisfying the epipolar constraint.
-
 
12
// Works like cv::windowedMatchingMask in conjunction with cv::BFMatcher::match().
-
 
13
// F is the fundamental matrix.
-
 
14
// maxD is the maximum point to line distance permissible.
-
 
15
cv::Mat epipolarMatchingMask(const cv::vector<cv::KeyPoint> &keypoints1, const cv::vector<cv::KeyPoint> &keypoints2, cv::Matx33f F, float maxD){
-
 
16
 
-
 
17
    if(keypoints1.empty() || keypoints2.empty())
-
 
18
        return cv::Mat();
-
 
19
 
-
 
20
    int n1 = (int)keypoints1.size(), n2 = (int)keypoints2.size();
-
 
21
    cv::Mat mask(n1, n2, CV_8UC1);
-
 
22
 
-
 
23
    // Point to line distance
-
 
24
//    for( int i = 0; i < n1; i++ ){
-
 
25
//        cv::Vec3f p1 = cv::Vec3f(keypoints1[i].pt.x, keypoints1[i].pt.y, 1.0);
-
 
26
//        // Epipolar line defined by p1
-
 
27
//        cv::Vec3f l = F*p1;
-
 
28
//        l /= sqrt(l(0)*l(0) + l(1)*l(1));
-
 
29
//        for( int j = 0; j < n2; j++ ){
-
 
30
//            cv::Vec3f p2 = cv::Vec3f(keypoints2[j].pt.x, keypoints2[j].pt.y, 1.0);
-
 
31
//            // Signed distance to line
-
 
32
//            float d = l.dot(p2);
-
 
33
//            mask.at<uchar>(i, j) = fabs(d) < maxD;
-
 
34
//        }
-
 
35
//    }
-
 
36
 
-
 
37
    // Symmetric epipolar distance
-
 
38
    std::vector<cv::Point2f> q1, q2;
-
 
39
    cvtools::keypointsToPoints(keypoints1, q1);
-
 
40
    cvtools::keypointsToPoints(keypoints2, q2);
-
 
41
    std::vector<cv::Point3f> l1, l2;
-
 
42
    cv::computeCorrespondEpilines(q1, 1, F, l1);
-
 
43
    cv::computeCorrespondEpilines(q2, 2, F, l2);
-
 
44
 
-
 
45
    for( int i = 0; i < n1; i++ ){
-
 
46
        cv::Vec3f p1 = cv::Vec3f(q1[i].x, q1[i].y, 1.0);
-
 
47
        for( int j = 0; j < n2; j++ ){
-
 
48
            cv::Vec3f p2 = cv::Vec3f(q2[j].x, q2[j].y, 1.0);
-
 
49
            float d12 = l1[i].dot(p2);
-
 
50
            float d21 = l2[j].dot(p1);
-
 
51
            float d = d12*d12 + d21*d21;
-
 
52
            mask.at<uchar>(i, j) = d < maxD;
-
 
53
        }
-
 
54
    }
-
 
55
 
-
 
56
//    // Sampson Error (H&Z, p. 287) (expensive...)
-
 
57
//    std::vector<cv::Point2f> q1, q2;
-
 
58
//    cvtools::keypointsToPoints(keypoints1, q1);
-
 
59
//    cvtools::keypointsToPoints(keypoints2, q2);
-
 
60
//    std::vector<cv::Point3f> p1, p2;
-
 
61
//    cv::convertPointsToHomogeneous(q1, p1);
-
 
62
//    cv::convertPointsToHomogeneous(q2, p2);
-
 
63
//    cv::Mat Fp1Mat = cv::Mat(F)*cv::Mat(p1).reshape(1).t();
-
 
64
//    cv::Mat FTp2Mat = cv::Mat(F.t())*cv::Mat(p2).reshape(1).t();
-
 
65
//    for( int i = 0; i < n1; i++ ){
-
 
66
//        cv::Vec3f Fp1 = Fp1Mat.col(i);
-
 
67
//        for( int j = 0; j < n2; j++ ){
-
 
68
//            cv::Vec3f FTp2 = FTp2Mat.col(j);
-
 
69
//            cv::Matx<float,1,1> p2TFp1 = cv::Matx31f(p2[j]).t()*F*cv::Matx31f(p1[i]);
-
 
70
//            float d = p2TFp1(0)*p2TFp1(0) / (Fp1(0)*Fp1(0) + Fp1(1)*Fp1(1) + FTp2(0)*FTp2(0) + FTp2(1)*FTp2(1));
-
 
71
//            mask.at<uchar>(i, j) = d < maxD;
-
 
72
//        }
-
 
73
//    }
-
 
74
 
-
 
75
    return mask;
-
 
76
}
-
 
77
 
-
 
78
 
-
 
79
// Remove correspondences which have a distance metric above thresh.
-
 
80
void matchingThreshold(const std::vector<cv::DMatch> &matchesIn, std::vector<cv::DMatch> &matchesOut, float thresh){
-
 
81
 
-
 
82
    int nMatches = matchesIn.size();
-
 
83
    matchesOut.clear();
-
 
84
    matchesOut.reserve(nMatches);
-
 
85
 
-
 
86
    for(int i=0; i<nMatches; i++){
-
 
87
        if(matchesIn[i].distance < thresh)
-
 
88
            matchesOut.push_back(matchesIn[i]);
-
 
89
    }
-
 
90
 
-
 
91
}
-
 
92
 
11
// Lightly modified OpenCV function which accepts a line width argument
93
// Lightly modified OpenCV function which accepts a line width argument
12
void drawChessboardCorners(cv::InputOutputArray _image, cv::Size patternSize, cv::InputArray _corners, bool patternWasFound, int line_width){
94
void drawChessboardCorners(cv::InputOutputArray _image, cv::Size patternSize, cv::InputArray _corners, bool patternWasFound, int line_width){
13
    cv::Mat corners = _corners.getMat();
95
    cv::Mat corners = _corners.getMat();
14
    if( corners.empty() )
96
    if( corners.empty() )
15
        return;
97
        return;