本文主要是介绍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维。匹配向量的大小为图像中较少的特征点的个数。
效果图
这篇关于OpenCV3中的SURF特征提取及匹配的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!