OpenCV學習筆記:實現獲取匹配度最高的前N個匹配點對(SIFT算法)

來感覺了,再更一篇,馬上科研。閒言少敘,開始操作。

搞計算機視覺的monkey可能最不陌生的就是LOWE大神的SIFT算法了,無論科研還是工作,可能會有獲取匹配度最高的前N個匹配點對這種需求,OK,碼上來!

#include "highgui/highgui.hpp"    
#include "opencv2/nonfree/nonfree.hpp"    
#include "opencv2/legacy/legacy.hpp"

using namespace std;
using namespace cv;

int main()
{
    Mat input1 = imread("img2.png", 1);
    Mat input2 = imread("img1.png", 1);
    Mat imgGray1, imgGray2;
    
    //轉換灰度圖
    cvtColor(input1, imgGray1, CV_BGR2GRAY);
    cvtColor(input2, imgGray2, CV_BGR2GRAY);
    SiftFeatureDetector detector;
    vector<KeyPoint> keypoint1, keypoint2;
    detector.detect(imgGray1, keypoint1);
    detector.detect(imgGray2, keypoint2);

    SiftDescriptorExtractor extractor;
    Mat descriptor1, descriptor2;

    BruteForceMatcher<L2<float>> matcher;
    vector<DMatch> matches;

    extractor.compute(imgGray1, keypoint1, descriptor1);
    extractor.compute(imgGray2, keypoint2, descriptor2);

    matcher.match(descriptor1, descriptor2, matches);
    
    //特徵點排序
    sort(matches.begin(), matches.end());
    
    vector<KeyPoint> goodImagePoints1, goodImagePoints2;
    vector<DMatch> matchesVoted;
    
    //獲取排名前10個的匹配度高的匹配點集
    for (int i = 0; i<10; i++)
    {
        DMatch dmatch;
        dmatch.queryIdx = i;
        dmatch.trainIdx = i;

        matchesVoted.push_back(dmatch);
        goodImagePoints1.push_back(keypoint1[matches[i].queryIdx]);
        goodImagePoints2.push_back(keypoint2[matches[i].trainIdx]);
    }

    Mat img_matches;
    std::vector< DMatch > emptyVec;
    drawMatches(input1, goodImagePoints1, input2, goodImagePoints2, matchesVoted, img_matches, DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
    
    imshow("SIFT_Match_Image", img_matches);
    namedWindow("SIFT_Match_Image",WINDOW_NORMAL);
    waitKey();
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章