opencv3& c++之ORB特徵匹配

#include
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>


using namespace std;
using namespace cv;


int main(int argc, char** argv)
{
/*
    if(argc!=3)//判斷命令行輸入對錯
    {
        cout<<"usage: feature_extraction img1 img2"<<endl;
        return 1;
    }
*/
    //讀取要匹配的兩張圖像
    Mat img_1 = imread("1.jpg", CV_LOAD_IMAGE_COLOR);
    Mat img_2 = imread("2.jpg", CV_LOAD_IMAGE_COLOR);




    //初始化
    //首先創建兩個關鍵點數組,用於存放兩張圖像的關鍵點,數組元素是KeyPoint類型
    std::vector<KeyPoint> keypoints_1, keypoints_2;


    //創建兩張圖像的描述子,類型是Mat類型
    Mat descriptors_1, descriptors_2;


    //創建一個ORB類型指針orb,ORB類是繼承自Feature2D類
    //class CV_EXPORTS_W ORB : public Feature2D
    //這裏看一下create()源碼:參數較多,不介紹。
    //creat()方法所有參數都有默認值,返回static Ptr<ORB>類型。
    /*
    CV_WRAP static Ptr<ORB> create(int nfeatures=500,
                                   float scaleFactor=1.2f,
                                   int nlevels=8,
                                   int edgeThreshold=31,
                                   int firstLevel=0,
                                   int WTA_K=2,
                                   int scoreType=ORB::HARRIS_SCORE,
                                   int patchSize=31,
                                   int fastThreshold=20);
    */
    //所以這裏的語句就是創建一個Ptr<ORB>類型的orb,用於接收ORB類中create()函數的返回值
    Ptr<ORB> orb = ORB::create();




    //第一步:檢測Oriented FAST角點位置.
    //detect是Feature2D中的方法,orb是子類指針,可以調用
    //看一下detect()方法的原型參數:需要檢測的圖像,關鍵點數組,第三個參數爲默認值
    /*
    CV_WRAP virtual void detect( InputArray image,
                                 CV_OUT std::vector<KeyPoint>& keypoints,
                                 InputArray mask=noArray() );
    */
    orb->detect(img_1, keypoints_1);
    orb->detect(img_2, keypoints_2);




    //第二步:根據角點位置計算BRIEF描述子
    //compute是Feature2D中的方法,orb是子類指針,可以調用
    //看一下compute()原型參數:圖像,圖像的關鍵點數組,Mat類型的描述子
    /*
    CV_WRAP virtual void compute( InputArray image,
                                  CV_OUT CV_IN_OUT std::vector<KeyPoint>& keypoints,
                                  OutputArray descriptors );
    */
    orb->compute(img_1, keypoints_1, descriptors_1);
    orb->compute(img_2, keypoints_2, descriptors_2);


    //定義輸出檢測特徵點的圖片。
    Mat outimg1;
    //drawKeypoints()函數原型參數:原圖,原圖關鍵點,帶有關鍵點的輸出圖像,後面兩個爲默認值
    /*
    CV_EXPORTS_W void drawKeypoints( InputArray image,
                                     const std::vector<KeyPoint>& keypoints,
                                     InputOutputArray outImage,
                                     const Scalar& color=Scalar::all(-1),
                                     int flags=DrawMatchesFlags::DEFAULT );
    */
    //注意看,這裏並沒有用到描述子,描述子的作用是用於後面的關鍵點篩選。
    drawKeypoints(img_1, keypoints_1, outimg1, Scalar::all(-1), DrawMatchesFlags::DEFAULT);


    imshow("ORB特徵點",outimg1);




    //第三步:對兩幅圖像中的BRIEF描述子進行匹配,使用 Hamming 距離


    //創建一個匹配點數組,用於承接匹配出的DMatch,其實叫match_points_array更爲貼切。matches類型爲數組,元素類型爲DMatch
    vector<DMatch> matches;


    //創建一個BFMatcher匹配器,BFMatcher類構造函數如下:兩個參數都有默認值,但是第一個距離類型下面使用的並不是默認值,而是漢明距離
    //CV_WRAP BFMatcher( int normType=NORM_L2, bool crossCheck=false );
    BFMatcher matcher (NORM_HAMMING);


    //調用matcher的match方法進行匹配,這裏用到了描述子,沒有用關鍵點。
    //匹配出來的結果寫入上方定義的matches[]數組中
    matcher.match(descriptors_1, descriptors_2, matches);


    //第四步:遍歷matches[]數組,找出匹配點的最大距離和最小距離,用於後面的匹配點篩選。
    //這裏的距離是上方求出的漢明距離數組,漢明距離表徵了兩個匹配的相似程度,所以也就找出了最相似和最不相似的兩組點之間的距離。
    double min_dist=0, max_dist=0;//定義距離


    for (int i = 0; i < descriptors_1.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);


    //第五步:根據最小距離,對匹配點進行篩選,
    //當描述自之間的距離大於兩倍的min_dist,即認爲匹配有誤,捨棄掉。
    //但是有時最小距離非常小,比如趨近於0了,所以這樣就會導致min_dist到2*min_dist之間沒有幾個匹配。
    // 所以,在2*min_dist小於30的時候,就取30當上限值,小於30即可,不用2*min_dist這個值了
    std::vector<DMatch> good_matches;
    for (int j = 0; j < descriptors_1.rows; ++j)
    {
        if (matches[j].distance <= max(2*min_dist, 30.0))
            good_matches.push_back(matches[j]);
    }


    //第六步:繪製匹配結果


    Mat img_match;//所有匹配點圖
    //這裏看一下drawMatches()原型參數,簡單用法就是:圖1,圖1關鍵點,圖2,圖2關鍵點,匹配數組,承接圖像,後面的有默認值
    /*
    CV_EXPORTS_W void drawMatches( InputArray img1,
                                   const std::vector<KeyPoint>& keypoints1,
                                   InputArray img2,
                                   const std::vector<KeyPoint>& keypoints2,
                                   const std::vector<DMatch>& matches1to2,
                                   InputOutputArray outImg,
                                   const Scalar& matchColor=Scalar::all(-1),
                                   const Scalar& singlePointColor=Scalar::all(-1),
                                   const std::vector<char>& matchesMask=std::vector<char>(),
                                   int flags=DrawMatchesFlags::DEFAULT );
    */


    drawMatches(img_1, keypoints_1, img_2, keypoints_2, matches, img_match);
    imshow("所有匹配點對", img_match);


    Mat img_goodmatch;//篩選後的匹配點圖
    drawMatches(img_1, keypoints_1, img_2, keypoints_2, good_matches, img_goodmatch);
    
    imshow("篩選後的匹配點對", img_goodmatch);


    waitKey(0);


    return 0;

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章