亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

OpenCV實(shí)現(xiàn)特征檢測(cè)和特征匹配方法匯總

 更新時(shí)間:2021年08月18日 15:34:01   作者:Madcola  
一幅圖像中總存在著其獨(dú)特的像素點(diǎn),這些點(diǎn)我們可以認(rèn)為就是這幅圖像的特征,成為特征點(diǎn),本文主要介紹了OpenCV實(shí)現(xiàn)特征檢測(cè)和特征匹配方法,感興趣的可以了解一下

一幅圖像中總存在著其獨(dú)特的像素點(diǎn),這些點(diǎn)我們可以認(rèn)為就是這幅圖像的特征,成為特征點(diǎn)。計(jì)算機(jī)視覺領(lǐng)域中的很重要的圖像特征匹配就是一特征點(diǎn)為基礎(chǔ)而進(jìn)行的,所以,如何定義和找出一幅圖像中的特征點(diǎn)就非常重要。這篇文章我總結(jié)了視覺領(lǐng)域最常用的幾種特征點(diǎn)以及特征匹配的方法。

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

以下是實(shí)驗(yàn)用的圖像:第一幅是手機(jī)抓拍的風(fēng)景圖,第二幅是遙感圖像。

1.SURF

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

我們首先進(jìn)行常規(guī)的特征提取和特征點(diǎn)匹配,看看效果如何。

#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);

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


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

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

    //獲得匹配特征點(diǎn),并提取最優(yōu)配對(duì)     
    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;
}

由上面的特征點(diǎn)匹配的效果來看,匹配的效果還是相當(dāng)糟糕的,如果我們拿著這樣子的匹配結(jié)果去實(shí)現(xiàn)圖像拼接或者物體追蹤,效果肯定是極差的。所以我們需要進(jìn)一步篩選匹配點(diǎn),來獲取優(yōu)秀的匹配點(diǎn),這就是所謂的“去粗取精”。這里我們采用了Lowe's算法來進(jìn)一步獲取優(yōu)秀匹配點(diǎn)。

為了排除因?yàn)閳D像遮擋和背景混亂而產(chǎn)生的無(wú)匹配關(guān)系的關(guān)鍵點(diǎn),SIFT的作者Lowe提出了比較最近鄰距離與次近鄰距離的SIFT匹配方式:取一幅圖像中的一個(gè)SIFT關(guān)鍵點(diǎn),并找出其與另一幅圖像中歐式距離最近的前兩個(gè)關(guān)鍵點(diǎn),在這兩個(gè)關(guān)鍵點(diǎn)中,如果最近的距離除以次近的距離得到的比率ratio少于某個(gè)閾值T,則接受這一對(duì)匹配點(diǎn)。因?yàn)閷?duì)于錯(cuò)誤匹配,由于特征空間的高維性,相似的距離可能有大量其他的錯(cuò)誤匹配,從而它的ratio值比較高。顯然降低這個(gè)比例閾值T,SIFT匹配點(diǎn)數(shù)目會(huì)減少,但更加穩(wěn)定,反之亦然。

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

ratio=0. 4:對(duì)于準(zhǔn)確度要求高的匹配;

ratio=0. 6:對(duì)于匹配點(diǎn)數(shù)目要求比較多的匹配;

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);

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


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

    //特征點(diǎn)描述,為下邊的特征點(diǎn)匹配做準(zhǔn)備    
    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,獲取優(yōu)秀匹配點(diǎn)
    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;
}

為了體現(xiàn)所謂的尺度不變形,我特意加入了額外一組圖片來測(cè)試

由特征點(diǎn)匹配的效果來看,現(xiàn)在的特征點(diǎn)匹配應(yīng)該是非常精準(zhǔn)了,因?yàn)槲覀円呀?jīng)把不合格的匹配點(diǎn)統(tǒng)統(tǒng)移除出去了。

2.SIFT

SURF算法是SIFT算法的加速版, 而SIFT(尺度不變特征轉(zhuǎn)換, ScaleInvariant Feature Transform) 是另一種著名的尺度不變特征檢測(cè)法。我們知道,SURF相對(duì)于SIFT而言,特征點(diǎn)檢測(cè)的速度有著極大的提升,所以在一些實(shí)時(shí)視頻流物體匹配上有著很強(qiáng)的應(yīng)用。而SIFT因?yàn)槠渚薮蟮奶卣饔?jì)算量而使得特征點(diǎn)提取的過程異?;ㄙM(fèi)時(shí)間,所以在一些注重速度的場(chǎng)合難有應(yīng)用場(chǎng)景。但是SIFT相對(duì)于SURF的優(yōu)點(diǎn)就是,由于SIFT基于浮點(diǎn)內(nèi)核計(jì)算特征點(diǎn),因此通常認(rèn)為, SIFT算法檢測(cè)的特征在空間和尺度上定位更加精確,所以在要求匹配極度精準(zhǔn)且不考慮匹配速度的場(chǎng)合可以考慮使用SIFT算法。

SIFT特征檢測(cè)的代碼我們僅需要對(duì)上面的SURF代碼作出一丁點(diǎn)修改即可。

#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);

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


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

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

    //獲得匹配特征點(diǎn),并提取最優(yōu)配對(duì)     
    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;
}

沒有經(jīng)過點(diǎn)篩選的匹配效果同樣糟糕。下面繼續(xù)采用Lowe‘s的算法選出優(yōu)秀匹配點(diǎn)。

#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);

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


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

    //特征點(diǎn)描述,為下邊的特征點(diǎn)匹配做準(zhǔn)備    
    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,獲取優(yōu)秀匹配點(diǎn)
    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的簡(jiǎn)稱,是brief算法的改進(jìn)版。ORB算法比SIFT算法快100倍,比SURF算法快10倍。在計(jì)算機(jī)視覺領(lǐng)域有種說法,ORB算法的綜合性能在各種測(cè)評(píng)里較其他特征提取算法是最好的。

ORB算法是brief算法的改進(jìn),那么我們先說一下brief算法有什么去缺點(diǎn)。

BRIEF的優(yōu)點(diǎn)在于其速度,其缺點(diǎn)是:

  • 不具備旋轉(zhuǎn)不變性
  • 對(duì)噪聲敏感
  • 不具備尺度不變性

而ORB算法就是試圖解決上述缺點(diǎn)中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);

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


    //提取特征點(diǎn)    
    OrbFeatureDetector OrbDetector(1000);  // 在這里調(diào)整精度,值越小點(diǎn)越少,越精準(zhǔn) 
    vector<KeyPoint> keyPoint1, keyPoint2;
    OrbDetector.detect(image1, keyPoint1);
    OrbDetector.detect(image2, keyPoint2);

    //特征點(diǎn)描述,為下邊的特征點(diǎn)匹配做準(zhǔn)備    
    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,獲取優(yōu)秀匹配點(diǎn)
    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(加速分割測(cè)試獲得特征, Features from Accelerated Segment Test) 。 這種算子專門用來快速檢測(cè)興趣點(diǎn), 只需要對(duì)比幾個(gè)像素,就可以判斷是否為關(guān)鍵點(diǎn)。

跟Harris檢測(cè)器的情況一樣, FAST算法源于對(duì)構(gòu)成角點(diǎn)的定義。FAST對(duì)角點(diǎn)的定義基于候選特征點(diǎn)周圍的圖像強(qiáng)度值。 以某個(gè)點(diǎn)為中心作一個(gè)圓, 根據(jù)圓上的像素值判斷該點(diǎn)是否為關(guān)鍵點(diǎn)。 如果存在這樣一段圓弧, 它的連續(xù)長(zhǎng)度超過周長(zhǎng)的3/4, 并且它上面所有像素的強(qiáng)度值都與圓心的強(qiáng)度值明顯不同(全部更黑或更亮) , 那么就認(rèn)定這是一個(gè)關(guān)鍵點(diǎn)。

用這個(gè)算法檢測(cè)興趣點(diǎn)的速度非???, 因此十分適合需要優(yōu)先考慮速度的應(yīng)用。 這些應(yīng)用包括實(shí)時(shí)視覺跟蹤、 目標(biāo)識(shí)別等, 它們需要在實(shí)
時(shí)視頻流中跟蹤或匹配多個(gè)點(diǎn)。

我們使用FastFeatureDetector 進(jìn)行特征點(diǎn)提取,因?yàn)閛pencv沒有提供fast專用的描述子提取器,所以我們借用SiftDescriptorExtractor 來實(shí)現(xiàn)描述子的提取。

#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);

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


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

    //特征點(diǎn)描述,為下邊的特征點(diǎn)匹配做準(zhǔn)備    
    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,獲取優(yōu)秀匹配點(diǎn)
    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的組合,看看效果

可以看出,這種組合下的特征點(diǎn)匹配并不精確。

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

速度雖快,但是精度卻差強(qiáng)人意。

看到這里可能很多人會(huì)有疑惑:為什么FAST特征點(diǎn)可以用所以我們借用SiftDescriptorExtractor或者其他描述子提取器進(jìn)行提???

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

在opencv中,SURF,ORB,SIFT既包含F(xiàn)eatureDetector,又包含 DescriptorExtractor,所以我們使用上述三種算法做特征匹配時(shí),都用其自帶的方法配套使用。

除此之外,如果我們相用FAST角點(diǎn)檢測(cè)并作特征點(diǎn)匹配該怎么辦?此時(shí)可以使用上述的FastFeatureDetector + BriefDescriptorExtractor 的方式,這種組合方式其實(shí)就是著名的ORB算法。所以特征點(diǎn)檢測(cè)和特征點(diǎn)匹配是兩種不同的步驟,我們只需根據(jù)自己項(xiàng)目的需求對(duì)這兩個(gè)步驟的方法隨意組合就好。

5.Harris角點(diǎn)

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

這里僅展示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);

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


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

    //特征點(diǎn)描述,為下邊的特征點(diǎn)匹配做準(zhǔn)備    
    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,獲取優(yōu)秀匹配點(diǎn)
    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;
}

匹配相當(dāng)精準(zhǔn)

計(jì)算機(jī)視覺領(lǐng)域其實(shí)還有了很多特征檢測(cè)的方法,比如HOG、Harr、LBP等,這里就不再敘述了,因?yàn)榉椒ǘ际穷愃?,我們根?jù)自己的需求挑選相應(yīng)的方法就好了。

到此這篇關(guān)于OpenCV實(shí)現(xiàn)特征檢測(cè)和特征匹配方法匯總的文章就介紹到這了,更多相關(guān)OpenCV 特征檢測(cè)和特征匹配內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Qt實(shí)現(xiàn)拖拽功能圖文教程(支持拖放文件、拖放操作)

    Qt實(shí)現(xiàn)拖拽功能圖文教程(支持拖放文件、拖放操作)

    這篇文章主要給大家介紹了關(guān)于Qt實(shí)現(xiàn)拖拽功能(支持拖放文件、拖放操作)的相關(guān)資料,Qt是一款多平臺(tái)的C++應(yīng)用程序開發(fā)框架,它的獨(dú)特之處在于可以快速開發(fā)出拖放式的開發(fā)桌面程序,需要的朋友可以參考下
    2023-11-11
  • C語(yǔ)言中楊氏矩陣與楊輝三角的實(shí)現(xiàn)方法

    C語(yǔ)言中楊氏矩陣與楊輝三角的實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中楊氏矩陣與楊輝三角的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • c++中的string常用函數(shù)用法總結(jié)

    c++中的string常用函數(shù)用法總結(jié)

    以下是對(duì)c++中的string常用函數(shù)的用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下
    2013-09-09
  • C與C++之間相互調(diào)用實(shí)例方法講解

    C與C++之間相互調(diào)用實(shí)例方法講解

    這篇文章主要介紹了C與C++之間相互調(diào)用的實(shí)例方法,大家參考使用吧
    2013-12-12
  • C++中取余運(yùn)算的實(shí)現(xiàn)

    C++中取余運(yùn)算的實(shí)現(xiàn)

    這篇文章主要介紹了C++中取余運(yùn)算的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • 淺談C++11中=delete的巧妙用法

    淺談C++11中=delete的巧妙用法

    本文主要介紹了C++11中=delete的巧妙用法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Qt學(xué)習(xí)之容器類的使用教程詳解

    Qt學(xué)習(xí)之容器類的使用教程詳解

    Qt提供了多個(gè)基于模板的容器類,這些類可以用于存儲(chǔ)指定類型的數(shù)據(jù)項(xiàng)。本文主要介紹了Qt常用容器類的使用,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-12-12
  • 一步步從底層入手搞定C++引用與內(nèi)聯(lián)函數(shù)

    一步步從底層入手搞定C++引用與內(nèi)聯(lián)函數(shù)

    內(nèi)聯(lián)函數(shù)是代碼插入到調(diào)用者代碼處的函數(shù),內(nèi)聯(lián)函數(shù)通過避免被調(diào)用的開銷來提高執(zhí)行效率,下面這篇文章主要給大家介紹了關(guān)于如何從底層入手搞定C++引用與內(nèi)聯(lián)函數(shù)的相關(guān)資料,需要的朋友可以參考下
    2023-03-03
  • C++ decltype類型說明符

    C++ decltype類型說明符

    在C++中,decltype作為操作符,用于查詢表達(dá)式的數(shù)據(jù)類型。decltype在C++11標(biāo)準(zhǔn)制定時(shí)引入,主要是為泛型編程而設(shè)計(jì),以解決泛型編程中,由于有些類型由模板參數(shù)決定,而難以(甚至不可能)表示之的問題。
    2016-03-03
  • Qt顯示QImage圖像在label上,并保持自適應(yīng)大小問題

    Qt顯示QImage圖像在label上,并保持自適應(yīng)大小問題

    這篇文章主要介紹了Qt顯示QImage圖像在label上,并保持自適應(yīng)大小問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11

最新評(píng)論