在两个图像之间使用SIFT进行特征点匹配

时间:2016-06-15 07:37:39

标签: c++ opencv feature-detection feature-extraction sift

我想知道为什么使用SIFT的两个图像之间没有很好的匹配。 匹配的图像如下所示。 enter image description here 原始图片

enter image description here

enter image description here

我的计划如下。

int imagematching(Mat &img1, Mat & img2, std::vector<Point2f> &first_keypoints, std::vector<Point2f> &second_keypoints){
    int max_keypoints = 500;

    Ptr<SIFT> detector = SIFT::create();
    Ptr<SIFT> extractor = SIFT::create();

    //--Step 1: Key point detection
    std::vector<KeyPoint> keypoints1, keypoints2;
    //-- Step 2: Calculate descriptors (feature vectors)
    Mat descriptors1, descriptors2;

    detector->detect( img1, keypoints1 );
    detector->detect( img2, keypoints2 );   

    extractor->compute(img1, keypoints1, descriptors1);
    extractor->compute(img2, keypoints2, descriptors2);    

    FlannBasedMatcher matcher;


    vector<DMatch> matches;
    matcher.match(descriptors1, descriptors2, matches);   

    double max_dist = 0; double min_dist = 999999;

    //-- Quick calculation of max and min distances between keypoints
    for( int i = 0; i < descriptors1.rows; i++ )
    { 
      double dist = matches[i].distance;
      if( dist < min_dist ) min_dist = dist;
      if( dist > max_dist ) max_dist = dist;
    }

    printf("-- Max dist : %f \n", max_dist );
    printf("-- Min dist : %f \n", min_dist );
    //-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
    std::vector< DMatch > good_matches;

    for( int i = 0; i < descriptors1.rows; i++ )
    { 
      if( matches[i].distance < 3*min_dist )
         { good_matches.push_back( matches[i]); }
    }
    matches.clear();

    Mat img_matches;
    drawMatches( img1, keypoints1, img2, keypoints2,
               good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
               vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

    descriptors1.release();
    descriptors2.release();

    //-- Localize the object
    //std::vector<Point2f> first_keypoints;
    //std::vector<Point2f> second_keypoints;

    for( int i = 0; i < good_matches.size(); i++ )
    {
       //cout << i << " :";
       //-- Get the keypoints from the good matches
       if( keypoints1[ good_matches[i].queryIdx ].pt.x > 0 && keypoints1[ good_matches[i].queryIdx ].pt.y > 0 
               && keypoints2[ good_matches[i].trainIdx ].pt.x > 0 && keypoints2[ good_matches[i].trainIdx ].pt.y > 0){
           first_keypoints.push_back( keypoints1[ good_matches[i].queryIdx ].pt );
           //cout << "first point" << keypoints1[ good_matches[i].queryIdx ].pt << endl;

           second_keypoints.push_back( keypoints2[ good_matches[i].trainIdx ].pt );
           //cout << "second point" << keypoints2[ good_matches[i].trainIdx ].pt << endl;
       }
    }
    keypoints1.clear();
    keypoints2.clear();
    good_matches.clear();
    //-- Show detected matches
    imshow( "Good Matches & Object detection", img_matches );
    waitKey(0);

    return SUCCESS;
}

1 个答案:

答案 0 :(得分:0)

SIFT可能是旋转不变的,但绝对不是透视不变的。 您需要添加一些机器学习 - 可能是SVM - 以便能够匹配具有不同视角的图像,仅仅SIFT功能是不够的。