OpenCV3中的SURF特征提取及匹配

原理不多介绍了(哈哈因为还不懂原理),直接上代码和效果吧,只是为了记录下用法省的忘了。

环境:Ubuntu14.04,Clion,OpenCV3.2

//
// Created by xiangqian on 18-2-20.
//
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/xfeatures2d.hpp"
#include "opencv2/highgui.hpp"
#include <opencv2/imgproc.hpp>

using namespace std;
using namespace cv;
using namespace cv::xfeatures2d;

int main() {

    Mat img1 = imread("../1.png", CV_LOAD_IMAGE_GRAYSCALE);
    Mat img2 = imread("../2.png", CV_LOAD_IMAGE_GRAYSCALE);

    // SURF 特征检测与匹配
    int minHessian = 700;
    Ptr<SURF> detector = SURF::create(minHessian);
    Ptr<DescriptorExtractor> descriptor = SURF::create();
    Ptr<DescriptorMatcher> matcher1 = DescriptorMatcher::create("BruteForce");
//    BFMatcher matcher1(NORM_L2);

    std::vector<KeyPoint> keyPoint1, keyPoint2;
    Mat descriptors1, descriptors2;
    std::vector<DMatch> matches;

    // 检测特征点
    detector->detect(img1, keyPoint1);
    detector->detect(img2, keyPoint2);
    // 提取特征点描述子
    descriptor->compute(img1, keyPoint1, descriptors1);
    descriptor->compute(img2, keyPoint2, descriptors2);
    // 匹配图像中的描述子
    matcher1->match(descriptors1, descriptors2, matches);

    Mat img_keyPoint1, img_keyPoint2;
    drawKeypoints(img1, keyPoint1, img_keyPoint1, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
    imshow("keyPoint1 SURF", img_keyPoint1);
    drawKeypoints(img2, keyPoint2, img_keyPoint2, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
    imshow("keyPoint2 SURF", img_keyPoint2);

    Mat img_matches;
    drawMatches(img1, keyPoint1, img2, keyPoint2, matches, img_matches);
    imshow("img_matches", img_matches);

    cout << "keyPoint1.size = " << keyPoint1.size() << endl;
    cout << "keyPoint2.size = " << keyPoint2.size() << endl;
    cout << "descriptors1.size = " << descriptors1.size() << endl;
    cout << "descriptors1.size = " << descriptors2.size() << endl;
    cout << "matches.size = " << matches.size() << endl;
//    for (int i = 0; i < matches.size(); i++)
//        cout << matches[i].distance << ' ';
//    cout << endl;
    waitKey(0);

    return 0;
}

程序输出:

keyPoint1.size = 742
keyPoint2.size = 771
descriptors1.size = [64 x 742]
descriptors1.size = [64 x 771]

matches.size = 742

可以看出单个特征点的描述子为64维。匹配向量的大小为图像中较少的特征点的个数。

效果图




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值