3. opencv提取SIFT特征

opencv中sift特征提取的步骤

  1. 使用SiftFeatureDetector 的detect方法检测特征存入一个向量里,并使用drawKeypoints在图中标识出来
  2. SiftDescriptorExtractor 的compute方法提取特征描述符,特征描述符是一个矩阵
  3. 使用匹配器matcher对描述符进行匹配,匹配结果保存由DMatch的组成的向量里
  4. 设置距离阈值,使得匹配的向量距离小于最小距离的2被才能进入最终的结果,用DrawMatch可以显示

代码


// 使用Flann进行特征点匹配.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include <opencv2/opencv.hpp>

#include <highgui/highgui.hpp>

#include <features2d/features2d.hpp>

#include <nonfree/nonfree.hpp>

#include <vector>

using namespace cv;

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

    Mat input1 = imread("E://code//test//image//box.png", 1);

    Mat input2 = imread("E://code//test//image//box_in_scene.jpg", 1);

    if (input1.empty()||input2.empty())

    {

        cout << "不能正常加载图片" << endl;

        system("pause");

        return -1;

    }

    /************************************************************************/

    /*下面进行提取特征点*/

    /************************************************************************/

    SiftFeatureDetector feature;

    vector<KeyPoint> kerpoints1;

    feature.detect(input1, kerpoints1);

    Mat output1;

    drawKeypoints(input1, kerpoints1, output1);

    vector<KeyPoint> kerpoints2;

    feature.detect(input2, kerpoints2);

    Mat output2;

    drawKeypoints(input2, kerpoints2, output2);

    imshow("提取特征点后的box.png", output1);

    imshow("提取特征点后的box_in_scene.png", output2);

    imwrite("提取特征点后的box.png", output1);

    imwrite("提取特征点后的box_in_scene.png", output2);

    cout << "box提取的特征点数为:" << kerpoints1.size() << endl;

    cout << "box_in_scene的特征点数为:" << kerpoints2.size() << endl;

    /************************************************************************/

    /* 下面进行特征向量提取 */

    /************************************************************************/

    SiftDescriptorExtractor descript;

    Mat description1;

    descript.compute(input1, kerpoints1, description1);

    Mat description2;

    descript.compute(input2, kerpoints2, description2);

    /************************************************************************/

    /* 下面进行特征向量临近匹配 */

    /************************************************************************/

    vector<DMatch> matches;

    FlannBasedMatcher matcher;

    Mat image_match;

    matcher.match(description1, description2, matches);

    /************************************************************************/

    /* 下面计算向量距离的最大值与最小值 */

    /************************************************************************/

    double max_dist = 0, min_dist = 100;

    for (int i = 0; i < description1.rows; i++)

    {

        if (matches.at(i).distance>max_dist)

        {

            max_dist = matches[i].distance;

        }

        if (matches[i].distance<min_dist)

        {

            min_dist = matches[i].distance;

        }

    }

    cout << "最小距离为" << min_dist << endl;

    cout << "最大距离为" << max_dist << endl;

    /************************************************************************/

    /* 得到距离小于而V诶最小距离的匹配 */

    /************************************************************************/

    vector<DMatch> good_matches;

    for (int i = 0; i < matches.size(); i++)

    {

        if (matches[i].distance<2*min_dist)

        {

            good_matches.push_back(matches[i]);

            cout <<"第一个图中的"<< matches[i].queryIdx<<"匹配了第二个图中的"<<matches[i].trainIdx<<endl;

        }

    }

    drawMatches(input1, kerpoints1, input2, kerpoints2, good_matches, image_match);

    imshow("匹配后的图片", image_match);

    imwrite("匹配后的图片.png", image_match);

    cout << "匹配的特征点数为:" << good_matches.size() << endl;

    waitKey(0);

    return 0;

}

程序运行前的原始图片

box.png
box_in_scene.png

提取特征点后

box提取特征后.png
box_in_scene提取特征后

进行匹配后

匹配特征后.png
Paste_Image.png
Paste_Image.png

相关代码介绍


    double max_dist = 0, min_dist = 100;

    for (int i = 0; i < description1.rows; i++)

    {

        if (matches.at(i).distance>max_dist)

        {

            max_dist = matches[i].distance;

        }

        if (matches[i].distance<min_dist)

        {

            min_dist = matches[i].distance;

        }

    }  

设置阈值,当特征向量的距离在最小距离的二倍范围内的,匹配为好的匹配;

本博文由时尚时尚最时尚的markdown编辑器编写.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 学习资料 SIFT特征提取分析-Rachel Zhang的专栏 SIFT原理与源码分析(图文并茂,最详细) Ope...
    keloli阅读 4,113评论 2 9
  • SIFT特征 SIFT(Scale-invariant feature transform)是一种检测局部特征算法...
    张博一阅读 12,483评论 0 6
  • 特征提取是计算机视觉和图像处理中的一个概念。它指的是使用计算机提取图像信息,决定每个图像的点是否属于一个图像特征。...
    mogu酱阅读 2,112评论 1 11
  • 特征提取是计算机视觉和图像处理中的一个概念。它指的是使用计算机提取图像信息,决定每个图像的点是否属于一个图像特征。...
    ChrisJO阅读 2,435评论 1 10
  • 作者:魏城 一 定居一个地方 偶尔换个地方住 叫旅游 常换地方住 偶尔定居 叫流浪 二 又要启程了 人生就是一连串...
    魏城阅读 175评论 0 3