Opencv學習之ORB算法

Opencv學習之ORB算法
ORB是ORiented Brief的簡稱,是brief算法的改進版,算法效率比SIFT高兩個數量級,在計算速度上,ORB是SIFT的100倍,是SURF的10倍。
Brief描述子:Brief是Binary Robust Independent Elementary Features的縮寫,主要思路就是在特徵點附近隨機選取若干點對,將這些點對的灰度值的大小,組合成一個二進制串,並將這個二進制串作爲該特徵點的特徵描述子。
BRIEF的有點在於速度,缺點也很明顯
*不具備旋轉不變性
*對噪聲敏感
*不具備尺度不變性
而ORB算法就是試圖解決上述缺點中的1和2,對於3,在OPENCV中用了圖像金字塔來進行改善。
與SURF類似,用了OrbFeatureDetector來提取關鍵點,用OrbDescriptorExtractor來提取特徵向量。

 #include<opencv2/opencv.hpp>
#include<opencv2/nonfree/nonfree.hpp>

int main()
{
//載入原始圖像和模版圖像
    cv::Mat srcImage=cv::imread("/Users/new/Desktop/3.jpg");
    cv::Mat templaImage=cv::imread("/Users/new/Desktop/4.jpg");
//灰度化
cv::Mat dstImage,grayImage1,grayImage2;
    cv::cvtColor(srcImage, grayImage1, CV_BGR2GRAY);

    cv::cvtColor(templaImage, grayImage2, CV_BGR2GRAY);



//***************檢測ORB特徵點並在圖像中提取物體的描述符***************8
//定義參數
cv::OrbFeatureDetector featureDetector;
std::vector<cv::KeyPoint>keyPoints,keyPoints_templ;
cv::Mat descriptors,descriptors_templ;

//調用detect函數檢測出特徵關鍵點,保存在vector容器中
featureDetector.detect(grayImage1, keyPoints);
featureDetector.detect(grayImage2, keyPoints_templ);
//計算描述符(特徵向量)
cv::OrbDescriptorExtractor featureExtractor;
featureExtractor.compute(grayImage1, keyPoints, descriptors);
featureExtractor.compute(grayImage2, keyPoints_templ, descriptors_templ);
//匹配和測試描述符,獲取兩個最近鄰的描述符
cv::Mat matchIndex(descriptors.rows,2,CV_32SC1),matchDistance(descriptors.rows,2,CV_32FC1);
//基於FLANN的描述符對象匹配
cv::flann::Index flannIndex(descriptors_templ,cv::flann::LshIndexParams(12,20,2),cvflann::FLANN_DIST_HAMMING);
flannIndex.knnSearch(descriptors, matchIndex, matchDistance, 2,cv::flann::SearchParams());//調用k近鄰算法
//根據勞氏算法,選出優秀的匹配
std::vector<cv::DMatch>goodMatches;
for(int i=0;i<matchDistance.rows;++i)
{
    if(matchDistance.at<float>(i,0)<0.6 * matchDistance.at<float>(i,1))
    {
        cv::DMatch dmatches(i,matchIndex.at<int>(i,0),matchDistance.at<float>(i,0));
        goodMatches.push_back(dmatches);

    }
}
//繪製並顯示匹配窗口
cv::Mat resultImage;
drawMatches(srcImage,keyPoints,templaImage,keyPoints_templ,goodMatches,dstImage);
imshow("image[Orb]",dstImage);
    cv::waitKey(0);
    return 0;

}

這裏寫圖片描述

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