1. 程式人生 > >特征檢測和特征匹配方法

特征檢測和特征匹配方法

.com comment 場景 原則 它的 自帶 相同 for 視覺

一幅圖像中總存在著其獨特的像素點,這些點我們可以認為就是這幅圖像的特征,成為特征點。計算機視覺領域中的很重要的圖像特征匹配就是一特征點為基礎而進行的,所以,如何定義和找出一幅圖像中的特征點就非常重要。這篇文章我總結了視覺領域最常用的幾種特征點以及特征匹配的方法。

在計算機視覺領域,興趣點(也稱關鍵點或特征點)的概念已經得到了廣泛的應用, 包括目標識別、 圖像配準、 視覺跟蹤、 三維重建等。 這個概念的原理是, 從圖像中選取某些特征點並對圖像進行局部分析,而非觀察整幅圖像。 只要圖像中有足夠多可檢測的興趣點,並且這些興趣點各不相同且特征穩定, 能被精確地定位,上述方法就十分有效。

以下是實驗用的圖像:第一幅是手機抓拍的風景圖,第二幅是遙感圖像。
技術分享

技術分享

1.SURF

特征檢測的視覺不變性是一個非常重要的概念。 但是要解決尺度不變性問題,難度相當大。 為解決這一問題,計算機視覺界引入了尺度不變特征的概念。 它的理念是, 不僅在任何尺度下拍攝的物體都能檢測到一致的關鍵點,而且每個被檢測的特征點都對應一個尺度因子。 理想情況下,對於兩幅圖像中不同尺度的的同一個物體點, 計算得到的兩個尺度因子之間的比率應該等於圖像尺度的比率。近幾年, 人們提出了多種尺度不變特征,本節介紹其中的一種:SURF特征。 SURF全稱為“加速穩健特征”(Speeded Up Robust Feature),我們將會看到,它們不僅是尺度不變特征,而且是具有較高計算效率的特征。

我們首先進行常規的特征提取和特征點匹配,看看效果如何。

#include "highgui/highgui.hpp"    
#include "opencv2/nonfree/nonfree.hpp"    
#include "opencv2/legacy/legacy.hpp"   
#include <iostream>  

using namespace cv;
using namespace std;


int main()
{
    Mat image01 = imread("2.jpg", 1);    //右圖
    Mat image02 = imread("1.jpg", 1);    //左圖
    namedWindow("p2", 0);
    namedWindow("p1", 0);
    imshow("p2", image01);
    imshow("p1", image02);

    //灰度圖轉換  
    Mat image1, image2;
    cvtColor(image01, image1, CV_RGB2GRAY);
    cvtColor(image02, image2, CV_RGB2GRAY);


    //提取特征點    
    SurfFeatureDetector surfDetector(800);  // 海塞矩陣閾值,在這裏調整精度,值越大點越少,越精準 
    vector<KeyPoint> keyPoint1, keyPoint2;
    surfDetector.detect(image1, keyPoint1);
    surfDetector.detect(image2, keyPoint2);

    //特征點描述,為下邊的特征點匹配做準備    
    SurfDescriptorExtractor SurfDescriptor;
    Mat imageDesc1, imageDesc2;
    SurfDescriptor.compute(image1, keyPoint1, imageDesc1);
    SurfDescriptor.compute(image2, keyPoint2, imageDesc2);

    //獲得匹配特征點,並提取最優配對     
    FlannBasedMatcher matcher;
    vector<DMatch> matchePoints;

    matcher.match(imageDesc1, imageDesc2, matchePoints, Mat());
    cout << "total match points: " << matchePoints.size() << endl;


    Mat img_match;
    drawMatches(image01, keyPoint1, image02, keyPoint2, matchePoints, img_match);
    namedWindow("match", 0);
    imshow("match",img_match);
    imwrite("match.jpg", img_match);

    waitKey();
    return 0;
}

技術分享

技術分享

由上面的特征點匹配的效果來看,匹配的效果還是相當糟糕的,如果我們拿著這樣子的匹配結果去實現圖像拼接或者物體追蹤,效果肯定是極差的。所以我們需要進一步篩選匹配點,來獲取優秀的匹配點,這就是所謂的“去粗取精”。這裏我們采用了Lowe’s算法來進一步獲取優秀匹配點。

為了排除因為圖像遮擋和背景混亂而產生的無匹配關系的關鍵點,SIFT的作者Lowe提出了比較最近鄰距離與次近鄰距離的SIFT匹配方式:取一幅圖像中的一個SIFT關鍵點,並找出其與另一幅圖像中歐式距離最近的前兩個關鍵點,在這兩個關鍵點中,如果最近的距離除以次近的距離得到的比率ratio少於某個閾值T,則接受這一對匹配點。因為對於錯誤匹配,由於特征空間的高維性,相似的距離可能有大量其他的錯誤匹配,從而它的ratio值比較高。顯然降低這個比例閾值T,SIFT匹配點數目會減少,但更加穩定,反之亦然。

Lowe推薦ratio的閾值為0.8,但作者對大量任意存在尺度、旋轉和亮度變化的兩幅圖片進行匹配,結果表明ratio取值在0. 4~0. 6 之間最佳,小於0. 4的很少有匹配點,大於0. 6的則存在大量錯誤匹配點,所以建議ratio的取值原則如下:

ratio=0. 4:對於準確度要求高的匹配;

ratio=0. 6:對於匹配點數目要求比較多的匹配;

ratio=0. 5:一般情況下。

#include "highgui/highgui.hpp"    
#include "opencv2/nonfree/nonfree.hpp"    
#include "opencv2/legacy/legacy.hpp"   
#include <iostream>  

using namespace cv;
using namespace std;


int main()
{

    Mat image01 = imread("g2.jpg", 1);    
    Mat image02 = imread("g4.jpg", 1);    
    imshow("p2", image01);
    imshow("p1", image02);

    //灰度圖轉換  
    Mat image1, image2;
    cvtColor(image01, image1, CV_RGB2GRAY);
    cvtColor(image02, image2, CV_RGB2GRAY);


    //提取特征點    
    SurfFeatureDetector surfDetector(2000);  // 海塞矩陣閾值,在這裏調整精度,值越大點越少,越精準 
    vector<KeyPoint> keyPoint1, keyPoint2;
    surfDetector.detect(image1, keyPoint1);
    surfDetector.detect(image2, keyPoint2);

    //特征點描述,為下邊的特征點匹配做準備    
    SurfDescriptorExtractor SurfDescriptor;
    Mat imageDesc1, imageDesc2;
    SurfDescriptor.compute(image1, keyPoint1, imageDesc1);
    SurfDescriptor.compute(image2, keyPoint2, imageDesc2);

    FlannBasedMatcher matcher;
    vector<vector<DMatch> > matchePoints;
    vector<DMatch> GoodMatchePoints;

    vector<Mat> train_desc(1, imageDesc1);
    matcher.add(train_desc);
    matcher.train();

    matcher.knnMatch(imageDesc2, matchePoints, 2);
    cout << "total match points: " << matchePoints.size() << endl;

    // Lowe‘s algorithm,獲取優秀匹配點
    for (int i = 0; i < matchePoints.size(); i++)
    {
        if (matchePoints[i][0].distance < 0.6 * matchePoints[i][1].distance)
        {
            GoodMatchePoints.push_back(matchePoints[i][0]);
        }
    }

    Mat first_match;
    drawMatches(image02, keyPoint2, image01, keyPoint1, GoodMatchePoints, first_match);
    imshow("first_match ", first_match);
    waitKey();
    return 0;
}

技術分享

技術分享

為了體現所謂的尺度不變形,我特意加入了額外一組圖片來測試
技術分享

由特征點匹配的效果來看,現在的特征點匹配應該是非常精準了,因為我們已經把不合格的匹配點統統移除出去了。

2.SIFT

SURF算法是SIFT算法的加速版, 而SIFT(尺度不變特征轉換, ScaleInvariant Feature Transform) 是另一種著名的尺度不變特征檢測法。我們知道,SURF相對於SIFT而言,特征點檢測的速度有著極大的提升,所以在一些實時視頻流物體匹配上有著很強的應用。而SIFT因為其巨大的特征計算量而使得特征點提取的過程異常花費時間,所以在一些註重速度的場合難有應用場景。但是SIFT相對於SURF的優點就是,由於SIFT基於浮點內核計算特征點,因此通常認為, SIFT算法檢測的特征在空間和尺度上定位更加精確,所以在要求匹配極度精準且不考慮匹配速度的場合可以考慮使用SIFT算法。

SIFT特征檢測的代碼我們僅需要對上面的SURF代碼作出一丁點修改即可。

#include "highgui/highgui.hpp"    
#include "opencv2/nonfree/nonfree.hpp"    
#include "opencv2/legacy/legacy.hpp"   
#include <iostream>  

using namespace cv;
using namespace std;


int main()
{
    Mat image01 = imread("1.jpg", 1);    //右圖
    Mat image02 = imread("2.jpg", 1);    //左圖
    namedWindow("p2", 0);
    namedWindow("p1", 0);
    imshow("p2", image01);
    imshow("p1", image02);

    //灰度圖轉換  
    Mat image1, image2;
    cvtColor(image01, image1, CV_RGB2GRAY);
    cvtColor(image02, image2, CV_RGB2GRAY);


    //提取特征點    
    SiftFeatureDetector siftDetector(2000);  // 海塞矩陣閾值,在這裏調整精度,值越大點越少,越精準 
    vector<KeyPoint> keyPoint1, keyPoint2;
    siftDetector.detect(image1, keyPoint1);
    siftDetector.detect(image2, keyPoint2);

    //特征點描述,為下邊的特征點匹配做準備    
    SiftDescriptorExtractor SiftDescriptor;
    Mat imageDesc1, imageDesc2;
    SiftDescriptor.compute(image1, keyPoint1, imageDesc1);
    SiftDescriptor.compute(image2, keyPoint2, imageDesc2);

    //獲得匹配特征點,並提取最優配對     
    FlannBasedMatcher matcher;
    vector<DMatch> matchePoints;

    matcher.match(imageDesc1, imageDesc2, matchePoints, Mat());
    cout << "total match points: " << matchePoints.size() << endl;


    Mat img_match;
    drawMatches(image01, keyPoint1, image02, keyPoint2, matchePoints, img_match);

    imshow("match",img_match);
    imwrite("match.jpg", img_match);

    waitKey();
    return 0;
}

技術分享

技術分享

沒有經過點篩選的匹配效果同樣糟糕。下面繼續采用Lowe‘s的算法選出優秀匹配點。

#include "highgui/highgui.hpp"    
#include "opencv2/nonfree/nonfree.hpp"    
#include "opencv2/legacy/legacy.hpp"   
#include <iostream>  

using namespace cv;
using namespace std;


int main()
{

    Mat image01 = imread("1.jpg", 1);
    Mat image02 = imread("2.jpg", 1);
    imshow("p2", image01);
    imshow("p1", image02);

    //灰度圖轉換  
    Mat image1, image2;
    cvtColor(image01, image1, CV_RGB2GRAY);
    cvtColor(image02, image2, CV_RGB2GRAY);


    //提取特征點    
    SiftFeatureDetector siftDetector(800);  // 海塞矩陣閾值,在這裏調整精度,值越大點越少,越精準 
    vector<KeyPoint> keyPoint1, keyPoint2;
    siftDetector.detect(image1, keyPoint1);
    siftDetector.detect(image2, keyPoint2);

    //特征點描述,為下邊的特征點匹配做準備    
    SiftDescriptorExtractor SiftDescriptor;
    Mat imageDesc1, imageDesc2;
    SiftDescriptor.compute(image1, keyPoint1, imageDesc1);
    SiftDescriptor.compute(image2, keyPoint2, imageDesc2);

    FlannBasedMatcher matcher;
    vector<vector<DMatch> > matchePoints;
    vector<DMatch> GoodMatchePoints;

    vector<Mat> train_desc(1, imageDesc1);
    matcher.add(train_desc);
    matcher.train();

    matcher.knnMatch(imageDesc2, matchePoints, 2);
    cout << "total match points: " << matchePoints.size() << endl;

    // Lowe‘s algorithm,獲取優秀匹配點
    for (int i = 0; i < matchePoints.size(); i++)
    {
        if (matchePoints[i][0].distance < 0.6 * matchePoints[i][1].distance)
        {
            GoodMatchePoints.push_back(matchePoints[i][0]);
        }
    }

    Mat first_match;
    drawMatches(image02, keyPoint2, image01, keyPoint1, GoodMatchePoints, first_match);
    imshow("first_match ", first_match);
    imwrite("first_match.jpg", first_match);
    waitKey();
    return 0;
}

技術分享

技術分享

技術分享

3.ORB

ORB是ORiented Brief的簡稱,是brief算法的改進版。ORB算法比SIFT算法快100倍,比SURF算法快10倍。在計算機視覺領域有種說法,ORB算法的綜合性能在各種測評裏較其他特征提取算法是最好的。

ORB算法是brief算法的改進,那麽我們先說一下brief算法有什麽去缺點。
BRIEF的優點在於其速度,其缺點是:

  • 不具備旋轉不變性
  • 對噪聲敏感
  • 不具備尺度不變性

而ORB算法就是試圖解決上述缺點中1和2提出的一種新概念。值得註意的是,ORB沒有解決尺度不變性

#include "highgui/highgui.hpp"    
#include "opencv2/nonfree/nonfree.hpp"    
#include "opencv2/legacy/legacy.hpp"   
#include <iostream>  

using namespace cv;
using namespace std;


int main()
{

    Mat image01 = imread("g2.jpg", 1);
    Mat image02 = imread("g4.jpg", 1);
    imshow("p2", image01);
    imshow("p1", image02);

    //灰度圖轉換  
    Mat image1, image2;
    cvtColor(image01, image1, CV_RGB2GRAY);
    cvtColor(image02, image2, CV_RGB2GRAY);


    //提取特征點    
    OrbFeatureDetector OrbDetector(1000);  // 在這裏調整精度,值越小點越少,越精準 
    vector<KeyPoint> keyPoint1, keyPoint2;
    OrbDetector.detect(image1, keyPoint1);
    OrbDetector.detect(image2, keyPoint2);

    //特征點描述,為下邊的特征點匹配做準備    
    OrbDescriptorExtractor OrbDescriptor;
    Mat imageDesc1, imageDesc2;
    OrbDescriptor.compute(image1, keyPoint1, imageDesc1);
    OrbDescriptor.compute(image2, keyPoint2, imageDesc2);

    flann::Index flannIndex(imageDesc1, flann::LshIndexParams(12, 20, 2), cvflann::FLANN_DIST_HAMMING);

    vector<DMatch> GoodMatchePoints;

    Mat macthIndex(imageDesc2.rows, 2, CV_32SC1), matchDistance(imageDesc2.rows, 2, CV_32FC1);
    flannIndex.knnSearch(imageDesc2, macthIndex, matchDistance, 2, flann::SearchParams());

    // Lowe‘s algorithm,獲取優秀匹配點
    for (int i = 0; i < matchDistance.rows; i++)
    {
        if (matchDistance.at<float>(i,0) < 0.6 * matchDistance.at<float>(i, 1))
        {
            DMatch dmatches(i, macthIndex.at<int>(i, 0), matchDistance.at<float>(i, 0));
            GoodMatchePoints.push_back(dmatches);
        }
    }

    Mat first_match;
    drawMatches(image02, keyPoint2, image01, keyPoint1, GoodMatchePoints, first_match);
    imshow("first_match ", first_match);
    imwrite("first_match.jpg", first_match);
    waitKey();
    return 0;
}

技術分享

技術分享

4.FAST

FAST(加速分割測試獲得特征, Features from Accelerated Segment Test) 。 這種算子專門用來快速檢測興趣點, 只需要對比幾個像素,就可以判斷是否為關鍵點。

跟Harris檢測器的情況一樣, FAST算法源於對構成角點的定義。FAST對角點的定義基於候選特征點周圍的圖像強度值。 以某個點為中心作一個圓, 根據圓上的像素值判斷該點是否為關鍵點。 如果存在這樣一段圓弧, 它的連續長度超過周長的3/4, 並且它上面所有像素的強度值都與圓心的強度值明顯不同(全部更黑或更亮) , 那麽就認定這是一個關鍵點。

用這個算法檢測興趣點的速度非常快, 因此十分適合需要優先考慮速度的應用。 這些應用包括實時視覺跟蹤、 目標識別等, 它們需要在實
時視頻流中跟蹤或匹配多個點。

我們使用FastFeatureDetector 進行特征點提取,因為opencv沒有提供fast專用的描述子提取器,所以我們借用SiftDescriptorExtractor 來實現描述子的提取。

#include "highgui/highgui.hpp"    
#include "opencv2/nonfree/nonfree.hpp"    
#include "opencv2/legacy/legacy.hpp"   
#include <iostream>  

using namespace cv;
using namespace std;


int main()
{

    Mat image01 = imread("1.jpg", 1);
    Mat image02 = imread("2.jpg", 1);
    imshow("p2", image01);
    imshow("p1", image02);

    //灰度圖轉換  
    Mat image1, image2;
    cvtColor(image01, image1, CV_RGB2GRAY);
    cvtColor(image02, image2, CV_RGB2GRAY);


    //提取特征點    
    FastFeatureDetector Detector(50);  //閾值 
    vector<KeyPoint> keyPoint1, keyPoint2;
    Detector.detect(image1, keyPoint1);
    Detector.detect(image2, keyPoint2);

    //特征點描述,為下邊的特征點匹配做準備    
    SiftDescriptorExtractor   Descriptor;
    Mat imageDesc1, imageDesc2;
    Descriptor.compute(image1, keyPoint1, imageDesc1);
    Descriptor.compute(image2, keyPoint2, imageDesc2);

    BruteForceMatcher< L2<float> > matcher;   
    vector<vector<DMatch> > matchePoints;
    vector<DMatch> GoodMatchePoints;

    vector<Mat> train_desc(1, imageDesc1);
    matcher.add(train_desc);
    matcher.train();

    matcher.knnMatch(imageDesc2, matchePoints, 2);
    cout << "total match points: " << matchePoints.size() << endl;

    // Lowe‘s algorithm,獲取優秀匹配點
    for (int i = 0; i < matchePoints.size(); i++)
    {
        if (matchePoints[i][0].distance < 0.6 * matchePoints[i][1].distance)
        {
            GoodMatchePoints.push_back(matchePoints[i][0]);
        }
    }

    Mat first_match;
    drawMatches(image02, keyPoint2, image01, keyPoint1, GoodMatchePoints, first_match);
    imshow("first_match ", first_match);
    imwrite("first_match.jpg", first_match);
    waitKey();
    return 0;
}

技術分享

技術分享

如果我們把描述子換成SurfDescriptorExtractor,即FastFeatureDetector + SurfDescriptorExtractor的組合,看看效果
技術分享

可以看出,這種組合下的特征點匹配並不精確。

如果我們把描述子換成SurfDescriptorExtractor,即FastFeatureDetector + BriefDescriptorExtractor 的組合,看看效果
技術分享

速度雖快,但是精度卻差強人意。

看到這裏可能很多人會有疑惑:為什麽FAST特征點可以用所以我們借用SiftDescriptorExtractor或者其他描述子提取器進行提取?

在這裏我說一下自己的理解。要完成特征點的匹配第一個步驟就是找出每幅圖像的特征點,這叫做特征檢測,比如我們使用FastFeatureDetector、SiftFeatureDetector都是特征檢測的模塊。我們得到這些圖像的特征點後,我們就對這些特征點進行進一步的分析,用一些數學上的特征對其進行描述,如梯度直方圖,局部隨機二值特征等。所以在這一步我們可以選擇其他描述子提取器對這些點進行特征描述,進而完成特征點的精確匹配。

在opencv中,SURF,ORB,SIFT既包含FeatureDetector,又包含 DescriptorExtractor,所以我們使用上述三種算法做特征匹配時,都用其自帶的方法配套使用。

除此之外,如果我們相用FAST角點檢測並作特征點匹配該怎麽辦?此時可以使用上述的FastFeatureDetector + BriefDescriptorExtractor 的方式,這種組合方式其實就是著名的ORB算法。所以特征點檢測和特征點匹配是兩種不同的步驟,我們只需根據自己項目的需求對這兩個步驟的方法隨意組合就好。

5.Harris角點

在圖像中搜索有價值的特征點時,使用角點是一種不錯的方法。 角點是很容易在圖像中定位的局部特征, 並且大量存在於人造物體中(例如墻壁、 門、 窗戶、 桌子等產生的角點)。 角點的價值在於它是兩條邊緣線的接合點, 是一種二維特征,可以被精確地定位(即使是子像素級精度)。 與此相反的是位於均勻區域或物體輪廓上的點以及在同一物體的不同圖像上很難重復精確定位的點。 Harris特征檢測是檢測角點的經典方法。

這裏僅展示GoodFeaturesToTrackDetector + SiftDescriptorExtractor的組合方式的代碼,其他組合不再演示。

#include "highgui/highgui.hpp"    
#include "opencv2/nonfree/nonfree.hpp"    
#include "opencv2/legacy/legacy.hpp"   
#include <iostream>  

using namespace cv;
using namespace std;


int main()
{

    Mat image01 = imread("1.jpg", 1);
    Mat image02 = imread("2.jpg", 1);
    imshow("p2", image01);
    imshow("p1", image02);

    //灰度圖轉換  
    Mat image1, image2;
    cvtColor(image01, image1, CV_RGB2GRAY);
    cvtColor(image02, image2, CV_RGB2GRAY);


    //提取特征點    
    GoodFeaturesToTrackDetector Detector(500);  //最大點數,值越大,點越多
    vector<KeyPoint> keyPoint1, keyPoint2;
    Detector.detect(image1, keyPoint1);
    Detector.detect(image2, keyPoint2);

    //特征點描述,為下邊的特征點匹配做準備    
    SiftDescriptorExtractor  Descriptor;
    Mat imageDesc1, imageDesc2;
    Descriptor.compute(image1, keyPoint1, imageDesc1);
    Descriptor.compute(image2, keyPoint2, imageDesc2);

    BruteForceMatcher< L2<float> > matcher;   
    vector<vector<DMatch> > matchePoints;
    vector<DMatch> GoodMatchePoints;

    vector<Mat> train_desc(1, imageDesc1);
    matcher.add(train_desc);
    matcher.train();

    matcher.knnMatch(imageDesc2, matchePoints, 2);
    cout << "total match points: " << matchePoints.size() << endl;

    // Lowe‘s algorithm,獲取優秀匹配點
    for (int i = 0; i < matchePoints.size(); i++)
    {
        if (matchePoints[i][0].distance < 0.6 * matchePoints[i][1].distance)
        {
            GoodMatchePoints.push_back(matchePoints[i][0]);
        }
    }

    Mat first_match;
    drawMatches(image02, keyPoint2, image01, keyPoint1, GoodMatchePoints, first_match);
    imshow("first_match ", first_match);
    imwrite("first_match.jpg", first_match);
    waitKey();
    return 0;
}

匹配相當精準
技術分享

技術分享

計算機視覺領域其實還有了很多特征檢測的方法,比如HOG、Harr、LBP等,這裏就不再敘述了,因為方法都是類似,我們根據自己的需求挑選相應的方法就好了。

轉自:http://www.cnblogs.com/skyfsm/p/7401523.html

特征檢測和特征匹配方法