三维重建(二)Sift特征提取与匹配

抱歉,之前代码中

//挑选匹配的最好的前100个
    nth_element(matches.begin(), matches.begin() + 99, matches.end());
    matches.erase(matches.begin() + 99, matches.end());

写错了,把99写成00了,所以看不到匹配结果,这里改过来了
————————————————————————————————
这里我主要写了用Opencv 实现Sift特征提取与匹配的代码,如果想看Sift特征的详细描述,请看原文《Distinctive Image Features》或者我最近又写了三篇关于SIFT算法的详细描述
SIFT算法详解(1)综述与尺度空间检测
SIFT算法详解(2)极值点的精确定位与特征点方向的计算
SIFT算法详解(3)特征点描述符的生成
这里面也有我参考的一些资源

下面是我的代码:

//使用Opencv实现SIFT特征点检测
//lhc 2016-07-08

#include 
#include 
#include 
#include 
#include  
#include 

using namespace std;
using namespace cv;

int main()
{
    Mat image1 = imread("G:\\ET\\et000.jpg");
    Mat image2 = imread("G:\\ET\\et001.jpg");
    //Sift检测器
    SiftFeatureDetector detector;

    //保存两幅图像得到的特征点
    vector image1KeyPoint, image2KeyPoint;

    //检测特征点
    detector.detect(image1, image1KeyPoint);
    detector.detect(image2, image2KeyPoint);

    //把特征点画在两幅新图上
    Mat imageOutput1;
    Mat imageOutput2;
    drawKeypoints(image1, image1KeyPoint, imageOutput1, Scalar(0, 255, 0));
    drawKeypoints(image2, image2KeyPoint, imageOutput2, Scalar(0, 255, 0));

    //Sift特征描述提取
    SiftDescriptorExtractor extractor;
    Mat descriptor1, descriptor2;
    //得到Sift特征描述符
    extractor.compute(imageOutput1, image1KeyPoint, descriptor1);
    extractor.compute(imageOutput2, image2KeyPoint, descriptor2);

    //暴力匹配
    BruteForceMatcherfloat>> matcher;
    vector matches;
    matcher.match(descriptor1, descriptor2, matches);

    //挑选匹配的最好的前100个
    nth_element(matches.begin(), matches.begin() + 99, matches.end());
    matches.erase(matches.begin() + 99, matches.end());

    //画出匹配线
    Mat image_matched;
    drawMatches(imageOutput1, image1KeyPoint, imageOutput2, image2KeyPoint, matches, image_matched);

    imshow("image1", imageOutput1);
    imshow("image2", imageOutput2);
    imshow("image_matched", image_matched);
    waitKey(0);
    return 0;
}


这里有另一种Opencv的实验方法,也比较详细
另外,我试验发现Opencv自带的SIFT特征检测方法运算速度比较慢,应该是用CPU跑的,而且不开源,国外有人写了一个用GPU跑的Sift特征检测,但是没有并行化,这这里SiftGPU: A GPU Implementation of Scale Invariant Feature Transform (SIFT)

你可能感兴趣的:(OpenCV,三维重建)